国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

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);		}	}}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 松潘县| 淮南市| 乐安县| 百色市| 镇巴县| 通榆县| 定边县| 四平市| 胶州市| 定结县| 嘉祥县| 永川市| 千阳县| 托里县| 台北县| 财经| 辽宁省| 富阳市| 麦盖提县| 涟水县| 逊克县| 青河县| 遂昌县| 太白县| 青铜峡市| 金湖县| 杭锦后旗| 宁南县| 三原县| 绿春县| 泸水县| 西充县| 龙里县| 阳谷县| 宣城市| 毕节市| 仪征市| 晋宁县| 斗六市| 和政县| 上虞市|