首页 > 其他 > 详细

hdu2642之二维树状数组

时间:2014-03-12 23:00:53      阅读:650      评论:0      收藏:0      [点我收藏+]

Stars

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others)
Total Submission(s): 920    Accepted Submission(s): 400


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
1 0
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std;

const int MAX=1000+10;
int c[MAX][MAX];
bool mark[MAX][MAX];

int lowbit(int x){
	return x&(-x);
}

void Update(int x,int y,int d){
	while(x<MAX){
		int y1=y;
		while(y1<MAX){
			c[x][y1]+=d;
			y1+=lowbit(y1);
		}
		x+=lowbit(x);
	}
}

int Query(int x,int y){
	int sum=0;
	while(x>0){
		int y1=y;
		while(y1>0){
			sum+=c[x][y1];
			y1-=lowbit(y1);
		}
		x-=lowbit(x);
	}
	return sum;
}

int main(){
	int q,x1,y1,x2,y2;
	char op[3];
	while(~scanf("%d",&q)){
		memset(c,0,sizeof c);
		memset(mark,false,sizeof mark);
		for(int i=1;i<=q;++i){
			scanf("%s",op);
			if(op[0] == ‘B‘){
				scanf("%d%d",&x1,&y1);
				//cout<<x1<<‘ ‘<<y1<<"hello"<<endl;
				++x1,++y1;
				if(mark[x1][y1])continue;
				Update(x1,y1,1);
				mark[x1][y1]=true;
			}else if(op[0] == ‘D‘){
				scanf("%d%d",&x1,&y1);
				//cout<<x1<<‘ ‘<<y1<<"hello"<<endl;
				++x1,++y1;
				if(!mark[x1][y1])continue;
				Update(x1,y1,-1);
				mark[x1][y1]=false;
			}else{
				scanf("%d%d%d%d",&x1,&x2,&y1,&y2);	
				++x1,++y1;
				++x2,++y2;
				if(x1>x2)swap(x1,x2);
				if(y1>y2)swap(y1,y2);
				//cout<<x1<<‘ ‘<<y1<<"hello"<<x2<<‘ ‘<<y2<<endl;
				printf("%d\n",Query(x2,y2)-Query(x2,y1-1)-Query(x1-1,y2)+Query(x1-1,y1-1));
			}
		}
	}
	return 0;
}

hdu2642之二维树状数组,布布扣,bubuko.com

hdu2642之二维树状数组

原文:http://blog.csdn.net/xingyeyongheng/article/details/21112533

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!