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

首頁 > 學院 > 開發(fā)設計 > 正文

HDU-2642-Stars(二維樹狀數(shù)組應用)

2019-11-11 06:40:55
字體:
來源:轉載
供稿:網(wǎng)友

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 :查詢該矩形區(qū)域內點亮的星星個數(shù) 題解:樹狀數(shù)組模板 唯一坑點:當點(x,y)處已經(jīng)點亮,則不再點亮 熄滅操作雷同。
#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);		}	}}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 冕宁县| 蒲江县| 罗定市| 元氏县| 台前县| 全椒县| 张家界市| 九龙县| 徐闻县| 临邑县| 乌拉特中旗| 泽库县| 石景山区| 岑溪市| 同仁县| 临澧县| 贵阳市| 宣化县| 巩义市| 正安县| 宜宾市| 龙游县| 会昌县| 高尔夫| 余庆县| 克拉玛依市| 长兴县| 礼泉县| 虹口区| 双牌县| 甘泉县| 陈巴尔虎旗| 雷州市| 宣城市| 大英县| 恩施市| 镇江市| 浑源县| 佛坪县| 西宁市| 文安县|