ZOJ 3018 Population(二維線段樹?矩形樹?)

地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3018

題意:在平面內最多又32768個點,現在有兩種操作,1:在一些點上加上一個數n,2:詢問一個矩形區域內的點的數的和

分析:這題一看像線段樹,想離線搞,發現不好處理,也許可以,不過我是做不來了,後來自己YY了下二維的線段樹,從來沒寫過= =,一開始發現空間會爆,再一想,其實很多節點是沒必要增加的,然後就可以做了,大體思路如下:

每個節點保存一個矩形,還有這個矩形區域內的和,這個節點可以分爲四個子節點,現在對於插入操作,直接不停的將矩形範圍縮小,直到成爲一個點,這樣每個點最多帶來log(n)個節點(n爲離散化後x或y的數量),也就是總的節點個數爲32768*logn,這樣是完全可以的,而查找的話,跟線段樹差不多,每次判斷當前區域是否覆蓋整個節點,是的話就返回這塊區域的和,否則就遞歸與之有覆蓋的子矩形。。。好吧,好久沒寫,表達能力又變差了,具體看代碼,感覺練了一陣子的各種樹,再寫這樣的題還是得心應手的^_^

代碼:

/** head files*/
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cassert>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;

/** some operate*/
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define DWN(i,h,l) for(int i=(h);i>=(l);--i)
#define foreach(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e)
#define CLR(arr) memset(arr,0,sizeof(arr))
#define MAX3(a,b,c) max(a,max(b,c))
#define MAX4(a,b,c,d) max(max(a,b),max(c,d))
#define MIN3(a,b,c) min(a,min(b,c))
#define MIN4(a,b,c,d) min(min(a,b),min(c,d))

/** some const*/
#define N 800100
#define PI acos(-1.0)
#define oo 1000000000
#define loo 1000000000000000000LL
#define eps 1e-8
/** some alias*/
typedef long long LL;

int dx[]={0,0,-1,1};
int dy[]={-1,1,0,0};
/** Global variables*/

/** some template names, just push ctrl+j to get it in*/
//getint 讀入優化
//manacher 求最長迴文子串
//pqueue 優先隊列
//combk n元素序列的第m小的組合和
//pmatrix n個點的最大子矩陣
//suffixarray 後綴數組
//sbtree 平衡樹
struct retange
{
    int x1,y1,x2,y2;
    retange(){}
    retange(int x1, int y1, int x2, int y2):x1(x1),y1(y1),x2(x2),y2(y2){}
    bool cover(const retange &a)const
    {
        if(x1>a.x1||x2<a.x2||y1>a.y1||y2<a.y2)return 0;
        return 1;
    }
    bool interset(const retange &a)const
    {
        if(x1>a.x2||y1>a.y2||a.x1>x2||a.y1>y2)return 0;
        return 1;
    }
    bool ok()
    {
        return x1>=x2&&y1>=y2;
    }
    int mx()
    {
        return (x1+x2)>>1;
    }
    int my()
    {
        return (y1+y2)>>1;
    }
    void rd(char *s)
    {
        sscanf(s,"%d%d%d%d",&x1,&x2,&y1,&y2);
    }
};
struct Node
{
    retange r;
    Node *son[4];
    LL sum;
}TNode,*nil=&TNode;
Node mem[N],*C=mem;
Node* Create(retange r)
{
    C->r=r;
    C->sum=0;
    REP(i,4)C->son[i]=nil;
    return C++;
}
void Update(int x, int y, int n, Node *now)
{
    now->sum+=n;
    while(1)
    {
        int mx=now->r.mx();
        int my=now->r.my();
        if(x<=mx)
        {
            if(y<=my)
            {
                if(now->son[0]==nil)
                    now->son[0]=Create(retange(now->r.x1,now->r.y1,mx,my));
                now=now->son[0];
            }
            else
            {
                if(now->son[1]==nil)
                    now->son[1]=Create(retange(now->r.x1,my+1,mx,now->r.y2));
                now=now->son[1];
            }
        }
        else
        {
            if(y<=my)
            {
                if(now->son[2]==nil)
                    now->son[2]=Create(retange(mx+1,now->r.y1,now->r.x2,my));
                now=now->son[2];
            }
            else
            {
                if(now->son[3]==nil)
                    now->son[3]=Create(retange(mx+1,my+1,now->r.x2,now->r.y2));
                now=now->son[3];
            }
        }
        now->sum+=n;
        if(now->r.ok())break;
    }
}
LL Query(retange r, Node *now)
{
    if(r.cover(now->r))return now->sum;
    LL ret=0;
    REP(i,4)
        if(now->son[i]!=nil&&now->son[i]->r.interset(r))
            ret+=Query(r,now->son[i]);
    return ret;
}
char s[333];
int main()
{
    freopen("a","r",stdin);
    //freopen("wa","w",stdout);
    Node *root=Create(retange(0,0,20001,20001));
    retange r;
    int op,x,y,n;
    while(gets(s))
    {
        if(s[0]=='E')
        {
            C=mem;
            root=Create(retange(0,0,20001,20001));
            continue;
        }
        if(s[0]=='I')
        {
            op=1;
            continue;
        }
        if(s[0]=='Q')
        {
            op=0;
            continue;
        }
        if(op)
        {
            sscanf(s,"%d%d%d",&x,&y,&n);
            Update(x,y,n,root);
        }
        else
        {
            r.rd(s);
            printf("%lld\n",Query(r,root));
        }
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章