这道题实在二维数组的求和问题
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.
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.
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
10题目大意:就是B是亮的。D是不亮的,输入Q就输出给定的矩形区域内的亮的星星的个数。这是一道二维数组来弄树状数组的题,但是把x和y分开当成一维就很好计算求和了
- #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){ //在(x,y)上增加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){ //因为这里x>0,y>0所以(0,0)这一点已经没有,经下面推知,此点已变成(1,1)目的是好计算
- 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;
- }
本文介绍了一种使用树状数组解决二维数组中特定区域亮星数量统计的问题。通过将二维坐标转换为一维处理,实现了高效求和。文章提供了完整的C++代码实现,并解释了关键步骤。
6264

被折叠的 条评论
为什么被折叠?



