Test Instructions: Within a s*s square, there are two operations
1 X y A is at (x, y) this point plus A
2 X1 Y1 X2 Y2 query (X1,X2) to (Y1,y2) the number of phones in this rectangular range
And the boundary of the data is also the beginning of the 0 with a tree array to add a processing
For the rectangular area, a small rectangle with a large rectangle to cut three boundaries
Ans=query (x2,y2)-query (x2,y1-1)-query (x1-1,y2) +query (x1-1,y1-1);
4352k547ms#include<cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; #define M 1050int tree[m][m],lim;int lowbit (int x) {return x&-x;} void Add (int x,int y,int val) {int tmp; while (X<=lim) {tmp=y; while (Tmp<=lim) {tree[x][tmp]+=val; Tmp+=lowbit (TMP); } x+=lowbit (x); }}int query (int x,int y) {int s=0; while (x>0) {int tmp=y; while (tmp>0) {s+=tree[x][tmp]; Tmp-=lowbit (TMP); } x-=lowbit (x); } return s;} int main () {int op; while (scanf ("%d", &op) &&op!=3) {if (op==0) {scanf ("%d", &lim); lim++; } if (op==1) {int a,b,val; scanf ("%d%d%d", &a,&b,&val); Add (A+1,b+1,val); } if (op==2) {int x1,y1,x2,y2; scanf ("%d%d%d%d", &x1,&y1,&x2,&y2); x1++; y1++; x2++; y2++; int Ans=query (X2,Y2)-query (x2,y1-1)-query (x1-1,y2) +query (x1-1,y1-1); printf ("%d\n", ans); }} return 0;}
POJ 1195 Mobile Phones (two-dimensional tree-like array)