这题应该就是标准的二维树状数组,应该没什么难度
处理一下x,y等于0的情况就过了
#include <iostream> #include <cstdio> #include <cstring> #include <string> using namespace std; const int maxn = 1e3+10; 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, 0, 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, 1); map[x1][y1] = true; } else if(a == "D") { scanf("%d%d",&x1,&y1); x1 ++; y1 ++; if(!map[x1][y1]) continue; add(x1, y1, -1); 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-1, y1-1) - getsum(x1-1,y2) - getsum(x2, y1-1); printf("%d ",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 = 0; for(i=x;i>0;i-=lowbit(i)) for(j=y;j>0;j-=lowbit(j)) sum += c[i][j]; return sum; }