poj 1195 Mobile phones 二维树状数组
2012-08-06 19:18
387 查看
题目分析:给你一个全为0的矩阵,操作 x,y,A,代表将矩阵arr[x][y]+=A, LBRT 代表求矩阵arr[x][y](L<=x<<R,B<=y<=L)的和,,
1. 注意树状数组的下标不能从0开始,所以矩阵的arr[1-n][1-n]
2.要求矩阵arr[x][y]=Sum(R,T)+Sum(R,B-1)+Sum(L-1,T)+Sum(L-1,B-1);
代码:
1. 注意树状数组的下标不能从0开始,所以矩阵的arr[1-n][1-n]
2.要求矩阵arr[x][y]=Sum(R,T)+Sum(R,B-1)+Sum(L-1,T)+Sum(L-1,B-1);
代码:
#include<iostream> #include<cstdio> #include<memory.h> using namespace std; const int MAX=1200; int tree[MAX][MAX]; int s; int LowBit(int x) { return x&(-x); } void UpDate(int x,int y,int A) { /*if(tree[x][y]+A<0) tree[x][y]=0; else*/ for(int i=x;i<=s;i+=LowBit(i)) for(int j=y;j<=s;j+=LowBit(j))//for(int j=x;j<=s;j+=LowBit(j)写错了!!!!!! tree[i][j]+=A; } int Sum(int x,int y) { int temp=0; for(int i=x;i>=1;i-=LowBit(i)) for(int j=y;j>=1;j-=LowBit(j)) temp+=tree[i][j]; return temp; } int GetSum(int L,int R,int B,int T) { int temp=Sum(R,T)-Sum(R,B-1)-Sum(L-1,T)+Sum(L-1,B-1); return temp; } int main() { int t,x,y,L,B,R,T,A; while(scanf("%d %d",&t,&s)!=EOF) { memset(tree,0,sizeof(tree)); while(1) { scanf("%d",&t); if(t==3) break; if(t==1) { scanf("%d %d %d",&x,&y,&A); UpDate(x+1,y+1,A); } if(t==2) { scanf("%d %d %d %d",&L,&B,&R,&T); int ans=GetSum(L+1,R+1,B+1,T+1); printf("%d\n",ans); } } } //system("pause"); return 0; }
相关文章推荐
- poj 1195 Mobile phones (二维树状数组)
- POJ 1195 Mobile phones (二维树状数组)
- poj 1195 Mobile phones (二维树状数组)
- POJ 1195 Mobile phones (二维树状数组)
- POJ 1195 Mobile phones (二维树状数组或线段树)
- poj 1195 Mobile phones (二维树状数组)
- POJ 1195 Mobile phones (二维树状数组或线段树)
- POJ 1195 Mobile phones 二维树状数组
- POJ 1195 Mobile phones (二维树状数组)
- POJ 1195--Mobile phones (二维树状数组)
- POJ 1195 Mobile phones 二维树状数组
- poj 1195 Mobile phones (二维 树状数组)
- POJ 1195 Mobile phones (二维树状数组)
- poj 1195 Mobile phones 二维树状数组
- POJ 1195 Mobile phones (二维树状数组)
- POJ 1195 Mobile phones (二维树状数组)
- POJ 1195 Mobile phones (二维树状数组)
- POJ 1195 Mobile phones (二维树状数组)
- poj 1195 Mobile phones (二维树状数组)
- POJ 1195 Mobile phones (树状数组)