最裸的二维树状数组,但是因为内存太大(c[1010][1010]),好像不能运行,结果蒙着写,写了好久。。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 1010 int c[N][N]; int lowbit(int x) { return x&(-x); } void modify(int x,int y,int val) { for(int i=x;i<=N;i+=lowbit(i)) { for(int j=y;j<=N;j+=lowbit(j)) { c[i][j] += val; } } } int sum(int x,int y) { int res = 0; for(int i=x;i>0;i-=lowbit(i)) { for(int j=y;j>0;j-=lowbit(j)) { res += c[i][j]; } } return res; } int GetIt(int x1,int y1,int x2,int y2) { int maxx = max(x1,x2); int maxy = max(y1,y2); int minx = min(x1,x2); int miny = min(y1,y2); return sum(maxx+1,maxy+1)-sum(minx,maxy+1)-sum(maxx+1,miny)+sum(minx,miny); } int main() { int t,q,i,j; int x1,x2,y1,y2,n1; char ss[4]; int cs = 1; scanf("%d",&t); while(t--) { printf("Case %d:\n",cs++); scanf("%d",&q); memset(c,0,sizeof(c)); for(i=1;i<N;i++) { for(j=1;j<N;j++) { modify(i,j,1); } } while(q--) { scanf("%s",ss); if(ss[0] == ‘S‘) { scanf("%d%d%d%d",&x1,&y1,&x2,&y2); printf("%d\n",GetIt(x1,y1,x2,y2)); } else if(ss[0] == ‘A‘) { scanf("%d%d%d",&x1,&y1,&n1); modify(x1+1,y1+1,n1); } else if(ss[0] == ‘D‘) { scanf("%d%d%d",&x1,&y1,&n1); int val = sum(x1+1,y1+1)-sum(x1,y1+1)-sum(x1+1,y1)+sum(x1,y1); int Min = min(n1,val); modify(x1+1,y1+1,-Min); } else if(ss[0] == ‘M‘) { scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&n1); int val = sum(x1+1,y1+1)-sum(x1,y1+1)-sum(x1+1,y1)+sum(x1,y1); int Min = min(n1,val); modify(x1+1,y1+1,-Min); modify(x2+1,y2+1,Min); } } } return 0; }