2017.8.20------二维树状数组的求和问题

这道题实在二维数组的求和问题

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
  
  
5 B 581 145 B 581 145 Q 0 600 0 200 D 581 145 Q 0 600 0 200
 

Sample Output
  
  
1
0
题目大意:就是B是亮的。D是不亮的,输入Q就输出给定的矩形区域内的亮的星星的个数。
 
 
 
 
 
这是一道二维数组来弄树状数组的题,但是把x和y分开当成一维就很好计算求和了
 
 
 
  1. #include <iostream>  
  2. #include <string.h>  
  3. #include <string>  
  4. #include <cmath>  
  5. #include <cstdio>  
  6. int aa[1005][1005];  
  7. bool visit[1005][1005];  
  8. using namespace std;  
  9. #define min(a,b)(a)<(b)?(a):(b);  
  10. #define max(a,b)(a)>(b)?(a):(b);  
  11. int lowbit(int x){  
  12.   return x&(-x);  
  13. }  
  14. void update(int x,int y,int num){  //在(x,y)上增加num
  15.     int t=y;  
  16.     while(x<=1003){  
  17.         y=t;  
  18.       while(y<=1003){  
  19.       aa[x][y]+=num;  
  20.       y+=lowbit(y);  
  21.       }  
  22.       x+=lowbit(x);  
  23.     }  
  24. }  
  25. int find(int x,int y){  //因为这里x>0,y>0所以(0,0)这一点已经没有,经下面推知,此点已变成(1,1)目的是好计算
  26.     int s=0,t=y;  
  27.     while(x>0){  
  28.         y=t;  
  29.         while(y>0){  
  30.           s+=aa[x][y];  
  31.           y-=lowbit(y);  
  32.         }  
  33.         x-=lowbit(x);  
  34.     }  
  35.     return s;  
  36. }  
  37. int main(){  
  38.   memset(aa,0,sizeof(aa));  
  39.   memset(visit,0,sizeof(visit));  
  40.   int n;  
  41.   scanf("%d",&n);  
  42.   getchar();  
  43.   char chh;  
  44.   int x1,y1,x2,y2;  
  45.   while(n--){  
  46.     scanf("%c",&chh);  
  47.     if(chh=='B')  
  48.     {  
  49.         scanf("%d%d",&x1,&y1);  
  50.         if(visit[x1+1][y1+1]==0)  //注意增加一
  51.         {visit[x1+1][y1+1]=1;update(x1+1,y1+1,1);}  
  52.     }  
  53.     if(chh=='Q'){  
  54.       scanf("%d%d%d%d",&x1,&x2,&y1,&y2);  
  55.       int maxx,maxy,minx,miny;  
  56.       maxx=max(x1,x2);  
  57.       minx=min(x1,x2);  
  58.       maxy=max(y1,y2);  
  59.       miny=min(y1,y2);  
  60.       int a=find(maxx+1,maxy+1);  
  61.       int b=find(minx,miny);  //相当于减一,自己画坐标系一目了然
  62.       int c=find(maxx+1,miny);  
  63.       int d=find(minx,maxy+1);  
  64.       printf("%d\n",a+b-c-d);  //减去重叠的部分,很好
  65.     }  
  66.     if(chh=='D'){  
  67.       scanf("%d%d",&x1,&y1);  
  68.       if(visit[x1+1][y1+1]==1)  
  69.       {visit[x1+1][y1+1]=0;update(x1+1,y1+1,-1);}  
  70.     }  
  71.     if(n>=1)  
  72.         getchar();  
  73.   }  
  74.   return 0;  
  75. }  
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值