HDU 2642 Stars 二维树状数组应用

来源:互联网 发布:实况巅峰数据图拉姆 编辑:程序博客网 时间:2024/05/17 01:21

Stars

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others)
Total Submission(s): 1203    Accepted Submission(s): 489

Problem Description
Yifenfei is a romantic guy and he likes to count the stars in the sky.
To make the problem easier,we considerate the sky is a two-dimension plane.Sometimes the star will be bright and sometimes the star will be dim.At first,there is no bright star in the sky,then some information will 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" mean 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 are in the region correspond X1,X2,Y1,Y2.
There is only one case.

Input
The first line contain a M(M <= 100000), then M line followed.
each line start with a operational character.
if the character is B or D,then two integer X,Y (0 <=X,Y<= 1000)followed.
if the character is Q then four integer X1,X2,Y1,Y2(0 <=X1,X2,Y1,Y2<= 1000) followed.
 
Output
For each query,output the number of bright stars in one line.

Sample Input
5B 581 145B 581 145Q 0 600 0 200D 581 145Q 0 600 0 200

Sample Output
10
/*HDU 2624 二维树状数组应用 */#include<iostream>#include<stdio.h>using namespace std;#define N 1001int map[N][N],vis[N][N];void add(int x,int y,int d){int i,j;for(i=x;i<=N;i+=(i&-i))for(j=y;j<=N;j+=(j&-j))map[i][j]+=d;}int sum(int x,int y){int i,j,ans=0;for(i=x;i>0;i-=(i&-i))for(j=y;j>0;j-=(j&-j))ans+=map[i][j];return ans;}int main(){int m,x,y,x1,x2,y1,y2,tmp,ans;char str[3];while(scanf("%d",&m)!=EOF){memset(vis,0,sizeof(map));memset(vis,0,sizeof(vis));while(m--){scanf("%s",str);if(str[0]=='B'){scanf("%d%d",&x,&y);if(!vis[x+1][y+1]){add(x+1,y+1,1);vis[x+1][y+1]=1;}}else if(str[0]=='Q'){scanf("%d%d%d%d", &x1, &x2, &y1, &y2);                  if(x1>x2) {                      tmp = x1;                      x1 = x2;                      x2 = tmp;                  }                  if(y1>y2) {                      tmp = y1;                      y1 = y2;                      y2 = tmp;                  }                  ans=sum(x2+1,y2+1)-sum(x2+1,y1)-sum(x1,y2+1)+sum(x1,y1);                printf("%d\n",ans);}else if(str[0]=='D'){scanf("%d%d",&x,&y);if(vis[x+1][y+1]){add(x+1,y+1,-1);vis[x+1][y+1]=0;}}}}return 0;}



0 0