/* 二维线段树 */ #include <iostream> #include <cstdio> #include <algorithm> #define LL(x) ((x) << 1) #define RR(x) ((x) << 1 | 1) using namespace std; const int N = 1005; struct Sub_tree{ int ly, ry; int val; int mid(){ return (ly + ry) >> 1; } }; struct Main_tree{ int lx, rx; Sub_tree sub[4 * N]; int mid(){ return (lx + rx) >> 1; } }tree[4 * N]; bool hash[N][N]; bool readint(int &ret){ int sgn; char c; c = getchar(); if(c == EOF ) return true; while(c != '-' && c < '0' || c > '9') c = getchar(); sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while((c = getchar()) >= '0' && c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return false; } void Sub_Build(int fa, int node, int ly, int ry){ tree[fa].sub[node].ly = ly; tree[fa].sub[node].ry = ry; tree[fa].sub[node].val = 0; if(ly == ry) return ; int mid = (ly + ry) >> 1; Sub_Build(fa, LL(node), ly, mid); Sub_Build(fa, RR(node), mid + 1, ry); } void Build(int node, int lx, int rx, int ly, int ry){ tree[node].lx = lx; tree[node].rx = rx; Sub_Build(node, 1, ly, ry); if(lx == rx){ return ; } int mid = (lx + rx) >> 1; Build(LL(node), lx, mid, ly, ry); Build(RR(node), mid + 1, rx, ly, ry); } void Sub_Update(int fa, int node, int dy, int val){ tree[fa].sub[node].val += val; if(tree[fa].sub[node].ly == tree[fa].sub[node].ry){ /* // 这样处理就wrong if(val == 1 && tree[fa].sub[node].val == 0){ tree[fa].sub[node].val += val; } if(val == -1 && tree[fa].sub[node].val == 1){ tree[fa].sub[node].val += val; } */ return ; } int mid = tree[fa].sub[node].mid(); if(dy <= mid) Sub_Update(fa, LL(node), dy, val); else Sub_Update(fa, RR(node), dy, val); tree[fa].sub[node].val = tree[fa].sub[LL(node)].val + tree[fa].sub[RR(node)].val; } void Update(int node, int dx, int dy, int val){ Sub_Update(node, 1, dy, val); if(tree[node].lx == tree[node].rx){ return ; } int mid = tree[node].mid(); if(dx <= mid) Update(LL(node), dx, dy, val); else Update(RR(node), dx, dy, val); } int Sub_Query(int fa, int node, int ly, int ry){ if(tree[fa].sub[node].ly == ly && tree[fa].sub[node].ry == ry){ return tree[fa].sub[node].val; } int mid = tree[fa].sub[node].mid(); if(ry <= mid){ return Sub_Query(fa, LL(node), ly, ry); } else if(ly > mid){ return Sub_Query(fa, RR(node), ly, ry); } else { return Sub_Query(fa, LL(node), ly, mid) + Sub_Query(fa, RR(node), mid + 1, ry); } } int Query(int node, int lx, int rx, int ly, int ry){ if(tree[node].lx == lx && tree[node].rx == rx){ return Sub_Query(node, 1, ly, ry); } int mid = tree[node].mid(); if(rx <= mid){ return Query(LL(node), lx, rx, ly, ry); } else if(lx > mid){ return Query(RR(node), lx, rx, ly, ry); } else { return Query(LL(node), lx, mid, ly, ry) + Query(RR(node), mid + 1, rx, ly, ry); } } int main(){ int m; while(scanf("%d", &m) != EOF){ char str[2]; Build(1, 0, 1000, 0, 1000); memset(hash, false, sizeof(hash)); int x, y; int x1, x2, y1, y2; while(m--){ scanf("%s", str); if(str[0] == 'B'){ readint(x); readint(y); if(!hash[x][y]){ // 用了这个hash 才过的 Update(1, x, y, 1); hash[x][y] = true; } } else if(str[0] == 'D'){ readint(x); readint(y); if(hash[x][y]){ Update(1, x, y, -1); hash[x][y] = false; } }else{ readint(x1); readint(x2); if(x1 > x2){ // 吃了好几次RE int temp = x1; x1 = x2; x2 = temp; } readint(y1); readint(y2); if(y1 > y2){ int temp = y1; y1 = y2; y2 = temp; } printf("%d/n", Query(1, x1, x2, y1, y2)); } } } } #include <iostream> #include <cstdio> #define lowbit(x) (x & (-x)) using namespace std; const int N = 1002; bool hash[N][N]; int tree[N][N]; bool readint(int &ret) { int sgn; char c; c = getchar(); if (c == EOF) return true; while (c != '-' && c < '0' || c > '9') c = getchar(); sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while ((c = getchar()) >= '0' && c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return false; } inline void Add(int x, int y, int num) { int temp; while (x < N) { temp = y; while (temp < N) { tree[x][temp] += num; temp += lowbit(temp); } x += lowbit(x); } } inline int Query(int x, int y) { int sum = 0; int temp; while (x > 0) { temp = y; while (temp > 0) { sum += tree[x][temp]; temp -= lowbit(temp); } x -= lowbit(x); } return sum; } int main() { int m; while (scanf("%d", &m) != EOF) { char str[2]; int x, y; int x1, x2, y1, y2; memset(hash, false, sizeof(hash)); memset(tree, 0, sizeof(tree)); while (m--) { scanf("%s", str); if (str[0] == 'B') { readint(x); readint(y); x++; y++; if (!hash[x][y]) { Add(x, y, 1); hash[x][y] = true; } } else if (str[0] == 'D') { readint(x); readint(y); x++; y++; if (hash[x][y]) { Add(x, y, -1); hash[x][y] = false; ; } } else { readint(x1); readint(x2); readint(y1); readint(y2); x1++; x2++; y1++; y2++; if (x1 > x2) { int temp = x1; x1 = x2; x2 = temp; } if (y1 > y2) { int temp = y1; y1 = y2; y2 = temp; } int ans = 0; ans += Query(x2, y2); ans -= Query(x1 - 1, y2); ans -= Query(x2, y1 - 1); ans += Query(x1 - 1, y1 - 1); printf("%d/n", ans); } } } }