二维树状数组,单点更新区间查询,标记一下黑暗的星星亮,亮的星星黑暗,就变成了+1-1的操作了。再就是注意x1,y1,x2,y2不能0,得先++。
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
const int maxn = 1010;
using namespace std;
int b[maxn][maxn],n,T,book[maxn][maxn],x1,x2,y1,y2,n1,bj[maxn][maxn];
int lowbit(int x)
{
return x&(-x);
}
void add(int x,int y,int dd)
{
for(int i=x; i< maxn; i+=lowbit(i))
{
for(int j=y; j< maxn; j+=lowbit(j))
{
b[i][j]+=dd;
}
}
}
int sum(int i,int j)
{
int sum=0;
for(int x=i; x>0; x-=lowbit(x))
{
for(int y=j; y>0; y-=lowbit(y))
{
sum+=b[x][y];
}
}
return sum;
}
int main()
{
scanf("%d",&T);
memset(b,0,sizeof(b));
memset(bj,0,sizeof(bj));
while(T--)
{
getchar();
char a;
scanf("%c",&a);
if(a=='B')
{
scanf("%d %d",&x1,&y1);
x1++,y1++;
if(bj[x1][y1]==0)
add(x1,y1,1),bj[x1][y1]=1;
}
else if(a=='D')
{
scanf("%d %d",&x1,&y1);
x1++,y1++;
if(bj[x1][y1]==1)
add(x1,y1,-1),bj[x1][y1]=0;
}
else if(a=='Q')
{
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);
printf("%d\n",sum(x2,y2)+sum(x1-1,y1-1)-sum(x2,y1-1)-sum(x1-1,y2));
}
}
return 0;
}