Stars
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others)
Total Submission(s): 785 Accepted Submission(s): 335
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
5 B 581 145 B 581 145 Q 0 600 0 200 D 581 145 Q 0 600 0 200
Sample Output
Author
teddy
Source
Recommend
teddy
题意: 有一个地图 每个点都是一个灯 输入B 之后输入坐标 表示对应的点的灯变亮 输入D 输入坐标 表示变暗 输入Q 输入一个矩形的左下角和右上角 问矩形内的亮着的灯的个数
思路:
很明显的 二维树状数组 注意 灯亮过了就不能再亮了 而且灯关了就不能再关了 所以可以用一个flag数组标记是否可以进行关灯开灯操作
注意下标要从1开始 题目从0开始 所以要加1
#include<stdio.h>
#include<string.h>
const int N=1003;
int c[N+5][N+5],flag[N+5][N+5],n,m;
int mmax(int a,int b)
{
return a>b?a:b;
}
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int y,int delta )
{
int i, j;
for(i=x;i<=N;i+=lowbit(i))
{
for(j=y; j<=N; j+=lowbit(j))
{
c[i][j] += delta;
}
}
}
int sum( int x, int y )
{
int res=0,i,j;
for(i=x;i>0;i-=lowbit(i))
{
for(j=y; j>0; j-=lowbit(j))
{
res += c[i][j];
}
}
return res;
}
int main()
{
int x1,x2,y1,y2;
while(scanf("%d",&m)!=EOF)
{
memset(c,0,sizeof(c));
memset(flag,0,sizeof(flag));
while(m--)
{
char s[2];
scanf("%s",s);
if(s[0]=='Q')
{
scanf("%d %d %d %d",&x1,&x2,&y1,&y2);
x1++;y1++;x2++;y2++;
if(x1>x2) {int temp=x1;x1=x2;x2=temp;}
if(y1>y2) {int temp=y1;y1=y2;y2=temp;}
int ans=sum(x2,y2)-sum(x2,y1-1)-sum(x1-1,y2)+sum(x1-1,y1-1);
printf("%d\n",ans);
}
else if(s[0]=='B')
{
scanf("%d %d",&x1,&y1);
x1++;y1++;
if(flag[x1][y1]==1) continue;
flag[x1][y1]=1;
update(x1,y1,1);
}
else
{
scanf("%d %d",&x1,&y1);
x1++;y1++;
if(flag[x1][y1]==0) continue;
flag[x1][y1]=0;
update(x1,y1,-1);
}
}
}
return 0;
}