hduoj Stars 二维树状数组

来源:互联网 发布:the thick of it 编辑:程序博客网 时间:2024/05/17 22:41

             话说这道题是一道不折不扣的二维树状数组的水题,可是对于我这种菜鸟中的vip来说,却还是纠结了很久很久。。留下做个模板吧。。。。。

题目:

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
 

ac代码:

#include <iostream>#include <string.h>#include <string>#include <cmath>#include <cstdio>int aa[1005][1005];bool visit[1005][1005];using namespace std;#define min(a,b)(a)<(b)?(a):(b);#define max(a,b)(a)>(b)?(a):(b);int lowbit(int x){  return x&(-x);}void update(int x,int y,int num){int t=y;while(x<=1003){y=t;  while(y<=1003){      aa[x][y]+=num;  y+=lowbit(y);  }  x+=lowbit(x);}}int find(int x,int y){int s=0,t=y;while(x>0){y=t;while(y>0){  s+=aa[x][y];  y-=lowbit(y);}x-=lowbit(x);}return s;}int main(){  memset(aa,0,sizeof(aa));  memset(visit,0,sizeof(visit));  int n;  scanf("%d",&n);  getchar();  char chh;  int x1,y1,x2,y2;  while(n--){    scanf("%c",&chh);    if(chh=='B'){scanf("%d%d",&x1,&y1);if(visit[x1+1][y1+1]==0){visit[x1+1][y1+1]=1;update(x1+1,y1+1,1);}}if(chh=='Q'){  scanf("%d%d%d%d",&x1,&x2,&y1,&y2);  int maxx,maxy,minx,miny;  maxx=max(x1,x2);  minx=min(x1,x2);  maxy=max(y1,y2);  miny=min(y1,y2);  int a=find(maxx+1,maxy+1);  int b=find(minx,miny);  int c=find(maxx+1,miny);  int d=find(minx,maxy+1);  printf("%d\n",a+b-c-d);}if(chh=='D'){  scanf("%d%d",&x1,&y1);  if(visit[x1+1][y1+1]==1)  {visit[x1+1][y1+1]=0;update(x1+1,y1+1,-1);}}if(n>=1)getchar();  }  return 0;}


原创粉丝点击