这题应该就是标准的二维树状数组,应该没什么难度

处理一下x,y等于0的情况就过了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
const int maxn = 1e3+;
int c[maxn][maxn];
bool map[maxn][maxn];
void add(int x,int y,int d);
int getsum(int x,int y);
int main()
{
int t,x1,y1,x2,y2;
while(scanf("%d",&t) != EOF)
{
memset(c, , sizeof(c));
memset(map, false, sizeof(map));
while(t--)
{
string a;
cin >> a;
if(a == "B")
{
scanf("%d%d",&x1,&y1);
x1 ++; y1 ++;
if(map[x1][y1])
continue;
add(x1, y1, );
map[x1][y1] = true;
}
else if(a == "D")
{
scanf("%d%d",&x1,&y1);
x1 ++; y1 ++;
if(!map[x1][y1])
continue;
add(x1, y1, -);
map[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);
int k = getsum(x2, y2) + getsum(x1-, y1-) - getsum(x1-,y2) - getsum(x2, y1-);
printf("%d\n",k);
}
}
} }
int lowbit(int k)
{
return k&(-k);
}
void add(int x,int y,int d)
{
int i,j;
for(i=x;i<maxn;i+=lowbit(i))
{
for(j=y;j<maxn;j+=lowbit(j))
{
c[i][j] += d;
}
}
}
int getsum(int x,int y)
{
int i,j;
int sum = ;
for(i=x;i>;i-=lowbit(i))
for(j=y;j>;j-=lowbit(j))
sum += c[i][j];
return sum;
}
05-11 21:43