首页 > 学院 > 开发设计 > 正文

HDU-2642-Stars(二维树状数组应用)

2019-11-14 09:14:20
字体:
来源:转载
供稿:网友

Stars

Time Limit: 5000/2000 MS (java/Others)    Memory Limit: 32768/65536 K (Java/Others)Total Submission(s): 1718    Accepted Submission(s): 725PRoblem DescriptionYifenfei 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. InputThe 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. OutputFor 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  题目大意:  二维空间里,初始时一片空白,有如下几种操作:  1.B x y  :将坐标为(x,y)的地方点亮  2.D x y  :将坐标为(x,y)的地方熄灭  3. Q x x1 y y1 :查询该矩形区域内点亮的星星个数 题解:树状数组模板 唯一坑点:当点(x,y)处已经点亮,则不再点亮 熄灭操作雷同。
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define maxn 1005int a[maxn][maxn],b[maxn][maxn];int lowbit(int x){	return x&-x;}void update(int x,int y,int z){	int i,j;	for(i=x;i<=maxn;i+=lowbit(i))		for(j=y;j<=maxn;j+=lowbit(j))			a[i][j]+=z;}int query(int x,int y){	int sum=0,i,j;	for(i=x;i>0;i-=lowbit(i))		for(j=y;j>0;j-=lowbit(j))			sum+=a[i][j];	return sum;}int  main(){	int n,i,x,y,xx,yy;	scanf("%d",&n);	for(i=1;i<=n;i++)	{		char c[5];		scanf("%s",c);		if(c[0]=='B')		{			scanf("%d%d",&x,&y);			x++;y++;			if(b[x][y])				continue;			update(x,y,1);			b[x][y]=1;		}		else if(c[0]=='D')		{			scanf("%d%d",&x,&y);			x++;y++;			if(!b[x][y])				continue;			update(x,y,-1);			b[x][y]=0;		}		else		{			scanf("%d%d%d%d",&x,&xx,&y,&yy);			x++;y++;			xx++;yy++;			if(x<xx)				swap(x,xx);			if(y<yy)				swap(y,yy);			int ans=query(x,y)-query(x,yy-1)-(query(xx-1,y)-query(xx-1,yy-1));			printf("%d/n",ans);		}	}}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表