hdu 2642 Stars

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2642

题目大意:一开始全部的星星是暗着的.
Bxy:表示(x,y)的星星亮;
Dxy:表示(x,y)的星星暗;
Qx1x2y1y2:表示求出(x1,y1)--->(x2,y2)的亮着的星星的数量.

思路:很明显的二维树状数组.

代码:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define ull unsigned __int64
#define ll __int64
//#define ull unsigned long long
//#define ll long long
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define middle (l+r)>>1
#define MOD 9901
#define esp (1e-10)
const int INF=0x3F3F3F3F;
//const double pi=acos(-1.0);
const int N=1010;
int n,m;
int sum[N][N],vis[N][N];

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

void Add(int x,int y,int c){
	int t=y;
	while(x<N){
		while(y<N) sum[x][y]+=c,y+=lowbit(y);
		x+=lowbit(x);
		y=t;
	}
}

int Sum(int x,int y){
	int r=0,t=y;
	while(x>0){
		while(y>0) r+=sum[x][y],y-=lowbit(y);
		x-=lowbit(x);
		y=t;
	}
	return r;
}

void swap(int& x,int& y){int t=x;x=y;y=t;}

int main(){
	//freopen("1.in","r",stdin);
	//freopen("1.out","w",stdout);
	int i,j,k,x1,x2,y1,y2;
	char op[2];
	//int T,cas;scanf("%d",&T);for(cas=1;cas<=T;cas++)
	while(~scanf("%d",&n)){
		memset(sum,0,sizeof(sum));
		memset(vis,0,sizeof(vis));
		while(n--){
			scanf("%s",op);
			if(op[0]=='Q'){
				scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
				if(x1>x2) swap(x1,x2);
				if(y1>y2) swap(y1,y2);
				printf("%d\n",Sum(x2+2,y2+2)+Sum(x1+1,y1+1)-Sum(x1+1,y2+2)-Sum(x2+2,y1+1));
			}else{
				scanf("%d%d",&x1,&y1);
				if(op[0]=='B'){
					if(!vis[x1][y1]) Add(x1+2,y1+2,1),vis[x1][y1]=1;
					
				}else{
					if(vis[x1][y1]) Add(x1+2,y1+2,-1),vis[x1][y1]=0;
				}
			}
		}
	}
	return 0;
}



你可能感兴趣的:(hdu 2642 Stars)