Stars
Time limit:5000/2000 MS (java/others) Memory limit:32768/65536 K (java/others)
Total submission (s): 1628 Accepted Submission (s): 683
Problem Descriptionyifenfei is a romantic guy and he likes to count the stars in the sky.
The problem Easier,we considerate the sky is a two-dimension plane. Sometimes the star would be bright and sometimes the star would be dim. At First,there are no bright star in the Sky,then some information would be given as "B x y" where ' B ' represent bright and x represent the X coordinate and y represent the Y coordinate means the star at (x, y) is bright,and the ' d ' in "D X y" mea n the star at (x, y) is dim. When get a query as "Q X1 X2 Y1 Y2", you should tell Yifenfei how many bright stars there is in the region correspond x1,x 2,y1,y2.
There is the only one case.
Inputthe first line contain a m (M <= 100000) and then M Line followed.
Each line start with a operational character.
If the character is B or d,then, x, y (0 <=x,y<=) followed.
If the character is a Q then four integer x1,x2,y1,y2 (0 <=x1,x2,y1,y2<=) followed.
Outputfor each query,output the number of bright stars on one line.
Sample input5b 581 145B 581 145Q 0 0 200D 581 145Q 0 600 0 200
Sample Output10 Beginner tree-like array, read some articles, is a preliminary understanding. Own understanding: The binary representation of the method to achieve the two points. A binary number C[k] represents the number of lowbit (k) from A[k] and (Lowbit (k) is the lowest bit of k, and all except the lowest bit of K is set to 0). Update:
void Add (int k,int x) { for(int i=k;i<maxn;i+= Lowbit (i)) c[i]+ =x; }
Lowbit ():
int lowbit (int x) //Take the lowest bit {return x& (-x);}
Inquire:
int get_sum (int k) { int res=0; for (int i=k;i>0; i-=lowbit (i)) res+ =c[i] ; return res; }
This problem is a two-dimensional tree-like array, which is actually the principle.
Note: The coordinates of the topic starting from 0, may do the same for a star two times.
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<queue>#include<algorithm>#include<stdlib.h>#include<stack>#include<vector>using namespacestd;Const intmaxn=1010;intA[MAXN][MAXN];BOOLB[MAXN][MAXN];intLowbit (intx) { returnx& (-x);}voidModifyintXintYintdata) { for(intI=x; i<maxn; i+=lowbit (i)) for(intJ=y; j<maxn; j+=Lowbit (j)) A[i][j]+=data;}intGetsum (intXinty) { intres=0; for(intI=x; I>0; i-=lowbit (i)) for(intJ=y; J>0; j-=Lowbit (j)) Res+=A[i][j]; returnRes;}intMain () {intn,x,y,x1,y1; Charstr[2]; Memset (A,0,sizeof(a)); memset (b,0,sizeof(b)); scanf ("%d",&N); while(n--) {scanf ("%s", str); if(str[0]=='B') {scanf ("%d%d",&x,&y); X++; Y++; if(B[x][y])Continue; Modify (x, Y,1); B[x][y]=1; } Else if(str[0]=='D') {scanf ("%d%d",&x,&y); X++; Y++; if(b[x][y]==0)Continue; Modify (x, Y,-1); B[x][y]=0; } Else{scanf ("%d%d%d%d",&x,&x1,&y,&y1); X++;x1++;y++;y1++; if(x>x1) Swap (X,X1); if(y>y1) swap (y,y1); intAns=getsum (x1,y1)-getsum (X-1, y1)-getsum (x1,y-1) +getsum (x1, Y1); printf ("%d\n", ans); } } return 0;}
Hdu_2642_ Two-dimensional tree-like array