0%

HDU 2642 Stars

Stars

Two dimensional binary indexed tree.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <bits/stdc++.h>
#define DBG(x) cerr << #x << " = " << x << endl

using namespace std;
typedef long long LL;

const int N = 1002;

struct Bit {
int c[N][N];
int n, m;

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

void init(int n, int m) {
this->n = n;
this->m = m;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
c[i][j] = 0;
}
}
}

void add(int x, int y, int v) {
for (int i = x; i <= n; i += lowbit(i)) {
for (int j = y; j <= m; j += lowbit(j)) {
c[i][j] += v;
}
}
}

int get(int x, int y) {
int s = 0;
for (int i = x; i > 0; i -= lowbit(i)) {
for (int j = y; j > 0; j -= lowbit(j)) {
s += c[i][j];
}
}
return s;
}
} bit;

bool star[N][N];

int main(int argc, char **argv) {
int n;
while (scanf("%d", &n) != EOF) {
int m = 1001;
bit.init(m, m);
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= m; j++) {
star[i][j] = false;
}
}
for (int i = 0; i < n; i++) {
char op[2];
scanf("%s", op);
if (op[0] == 'B') {
int x, y;
scanf("%d%d", &x, &y);
x++, y++;
if (!star[x][y]) {
star[x][y] = true;
bit.add(x, y, 1);
}
} else if (op[0] == 'D') {
int x, y;
scanf("%d%d", &x, &y);
x++, y++;
if (star[x][y]) {
star[x][y] = false;
bit.add(x, y, -1);
}
} else {
int x1, x2, y1, y2;
scanf("%d%d%d%d", &x1, &x2, &y1, &y2);
if (x1 > x2) {
swap(x1, x2);
}
if (y1 > y2) {
swap(y1, y2);
}
x1++, x2++, y1++, y2++;
int res = bit.get(x2, y2) - bit.get(x1 - 1, y2) - bit.get(x2, y1 - 1) + bit.get(x1 - 1, y1 - 1);
printf("%d\n", res);
}
}
}
return 0;
}