POJ 1195-Mobile phones-二维树状数组

二维树状数组裸题

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 
 5 using namespace std;
 6 const int maxn =1024+10;
 7 int c[maxn][maxn];
 8 int N;
 9 
10 int lowbit(int x)
11 {
12     return x&(-x);
13 }
14 
15 void update(int x,int y,int d)
16 {
17     for(int i=x;i<=N;i+=lowbit(i))
18         for(int j=y;j<=N;j+=lowbit(j))
19             c[i][j] += d;
20 }
21 
22 int getsum(int x,int y)
23 {
24     int res = 0;
25     for(int i=x;i>0;i -= lowbit(i))
26         for(int j=y;j>0;j -= lowbit(j))
27             res += c[i][j];
28     return res;
29 }
30 
31 int query(int l,int r,int u,int d)
32 {
33     return getsum(r,u) - getsum(r,d-1) - getsum(l-1,u) + getsum(l-1,d-1);
34 }
35 
36 int main()
37 {
38     int op;
39     int x,y,l,r,u,d;
40     while(~scanf("%d",&op))
41     {
42         if(op == 0)
43         {
44             scanf("%d",&N);
45             memset(c,0,sizeof c);
46         }
47         else if(op == 1)
48         {
49             scanf("%d%d%d",&x,&y,&u);
50             update(x+1,y+1,u);
51         }
52         else if(op == 2)
53         {
54             scanf("%d%d%d%d",&l,&d,&r,&u);
55             printf("%d\n",query(l+1,r+1,u+1,d+1));
56         }
57         else if(op == 3)
58         {
59             break;
60         }
61     }
62 }

 

posted @ 2016-03-15 22:18  Helica  阅读(201)  评论(0编辑  收藏  举报