Stars
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others)
Total Submission(s): 975 Accepted Submission(s): 420
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.
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
1 0
Author
teddy
Source
树状数组,用到二维,当时总的的来说,还算简单。。。
题目要求求矩形里星星中的个数//
给定某两个坐标对角坐标,球该矩形的星星个数..
对于x1,x2,y1,y2.。我们不知道其大小,所以需要进行比较
得到大小之后,我们就可以求该巨型的了,像下面的图一样..
由此贴出代码吧:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxn 1005
#define lowbit(x) ((x)&(-x))
int aa[maxn][maxn];
bool bb[maxn][maxn]; void ope(int x,int y,int val)
{
int j;
if(val==)
{
if(bb[x][y]) return ;
bb[x][y]=true;
}
else
{
if(bb[x][y]==false)
return ;
bb[x][y]=false;
}
while(x<maxn){
j=y;
while(j<maxn){
aa[x][j]+=val;
j+=lowbit(j);
}
x+=lowbit(x);
}
}
int sum(int x,int y)
{
int ans= ,j;
while(x>){
j=y;
while(j>){
ans+=aa[x][j];
j-=lowbit(j);
}
x-=lowbit(x);
}
return ans;
}
struct node
{
int x;
int y;
};
int main()
{
int test,res;
char str[];
node a,b;
memset(aa,,sizeof(aa));
memset(bb,,sizeof(bb));
scanf("%d",&test);
while(test--)
{
scanf("%s",str);
if(str[]=='Q')
{
scanf("%d%d%d%d",&a.x,&b.x,&a.y,&b.y);
if(a.x>b.x){
a.x^=b.x;
b.x^=a.x;
a.x^=b.x;
}
if(a.y>b.y){
a.y^=b.y;
b.y^=a.y;
a.y^=b.y;
}
b.x++;
b.y++;
res=sum(b.x,b.y)-sum(a.x,b.y)+sum(a.x,a.y)-sum(b.x,a.y);
printf("%d\n",res);
}
else
{
scanf("%d%d",&a.x,&a.y);
a.x++; //ÓÒÒÆһλ
a.y++;
if(str[]=='B')
ope(a.x,a.y,);
else
ope(a.x,a.y,-);
}
}
return ;
}