poj 1195 Mobile phones 二维树状数组

题目链接:

http://poj.org/problem?id=1195

题意:

S*S的矩阵,两种操作
1 x y a, 在点(x,y)上加a
2 lx ly rx ry, 求矩形lx

题解:

二维树状数组。

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 typedef long long ll;
 6 #define MS(a) memset(a,0,sizeof(a))
 7 #define MP make_pair
 8 #define PB push_back
 9 const int INF = 0x3f3f3f3f;
10 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
11 inline ll read(){
12     ll x=0,f=1;char ch=getchar();
13     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
14     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
15     return x*f;
16 }
17 //////////////////////////////////////////////////////////////////////////
18 const int maxn = 1e3+233;
19 
20 int n,bit[maxn][maxn];
21 
22 void add(int x,int y,int a){
23     for(int i=x; i<maxn; i+=i&-i)
24         for(int j=y; j<maxn; j+=j&-j)
25             bit[i][j] += a;
26 }
27 
28 int sum(int x,int y){
29     int res = 0;
30     for(int i=x; i>0; i-=i&-i)
31         for(int j=y; j>0; j-=j&-j)
32             res += bit[i][j];
33     return res;
34 }
35 
36 int main(){
37     int op; op=read(),n=read();
38 
39     while(cin>>op && op!=3){
40         if(op==1){
41             int x=read(),y=read(),a=read();
42             ++x,++y;
43             add(x,y,a);
44         }
45         if(op == 2){
46             int lx,rx,ly,ry;
47             cin >> lx >> ly >> rx >> ry;
48             ++lx,++rx,++ly,++ry;
49             int ans = sum(rx,ry)-sum(rx,ly-1)-sum(lx-1,ry)+sum(lx-1,ly-1);
50             cout << ans << endl;
51         }
52     }
53 
54     return 0;
55 }

 

posted @ 2017-03-24 21:18  _yxg123  阅读(144)  评论(0编辑  收藏  举报