pku 1195 Mobile phones(二维树状数组)

极其标准的二维树状数组

#include <iostream> using namespace std; const int maxn=1050; int tree[maxn][maxn]; int n; inline int lowbit(int x) { return x&(x^(x-1)); } void insert(int x,int y,int t) { while(x<=n) { int i=y; while(i<=n) { tree[x][i]+=t; i+=lowbit(i); } x+=lowbit(x); } } int query(int x,int y) { int ans=0; while(x>0) { int i=y; while(i>0) { ans+=tree[x][i]; i-=lowbit(i); } x-=lowbit(x); } return ans; } int main() { freopen("e://1.in","r",stdin); memset(tree,0,sizeof(tree)); int flag,x1,y1,x2,y2,t; scanf("%d%d",&flag,&n); while(scanf("%d",&flag)&&flag!=3) { if(flag==1) { scanf("%d%d%d",&x1,&y1,&t); insert(x1+1,y1+1,t); } else { scanf("%d%d%d%d",&x1,&y1,&x2,&y2); printf("%d/n",query(x2+1,y2+1)+query(x1,y1)-query(x2+1,y1)-query(x1,y2+1)); } } return 0; }

你可能感兴趣的:(tree,query,mobile,insert)