poj1195 二维线段树

单点修改,区间询问和

#include 
#include 
#include 
using namespace std;
const int N = 1100;
int Tree[N*3][N*3];
int S;
void Add_x(int rooty, int rootx, int L, int R, int x, int a)
{
    Tree[rooty][rootx] += a;
    if(L==R) return ;
    int mid = (L+R)/2;
    if(x <= mid) Add_x(rooty, (rootx << 1) + 1, L, mid, x, a);
    else Add_x(rooty, (rootx << 1) + 2, mid+1, R, x, a);
}
void Add_y(int rooty, int L, int R, int y, int x, int a)
{
    Add_x(rooty, 0, 1, S, x, a);
    if(L==R) return ;
    int mid = (L+R)/2;
    if(y <= mid) Add_y((rooty << 1) + 1, L, mid, y, x, a);
    else Add_y((rooty << 1) + 2, mid+1, R, y, x, a);
}
int QuerySum_x(int rooty, int rootx, int L, int R, int x1, int x2)
{
    if(L == x1 && R == x2) return Tree[rooty][rootx];
    int mid = (L+R)/2;
    if(x2 <= mid) return QuerySum_x(rooty, (rootx << 1) + 1, L, mid, x1, x2);
    else if(x1 > mid) return QuerySum_x(rooty, (rootx << 1) + 2, mid+1, R, x1, x2);
    else return QuerySum_x(rooty, (rootx << 1) + 1, L, mid, x1, mid) + QuerySum_x(rooty, (rootx << 1) + 2, mid+1, R, mid+1, x2);;
}
int QuerySum_y(int rooty, int L, int R, int y1, int y2, int x1, int x2)
{
    if(L == y1 && R == y2) return QuerySum_x(rooty, 0, 1, S, x1, x2);
    int mid = (L + R) / 2;
    if(y2 <= mid) return QuerySum_y((rooty << 1) + 1, L, mid, y1, y2, x1, x2);
    else if(y1 > mid) return QuerySum_y((rooty << 1) + 2, mid+1, R, y1, y2, x1, x2);
    else  return QuerySum_y((rooty << 1) + 1, L, mid, y1, mid, x1, x2) + QuerySum_y((rooty << 1) + 2, mid+1, R, mid+1, y2, x1, x2);
}

int main()
{
    int cmd, x, y, a, l, b, r, t;
    int sum = 0;
    while(1)
    {
        scanf("%d",&cmd);
        switch(cmd)
        {
        case 0:
            scanf("%d",&S);
            memset(Tree, 0, sizeof Tree);
            break;

        case 1:
            scanf("%d%d%d",&x,&y,&a);
            Add_y(0, 1, S, y+1, x+1, a);
            break;
        case 2:
            scanf("%d%d%d%d",&l, &b, &r, &t);
            l++;b++;r++;t++;
            printf("%d\n",QuerySum_y(0, 1, S, b, t, l, r));
            break;
        case 3:
            return 0;
        }

    }
    return 0;
}


你可能感兴趣的:(poj1195 二维线段树)