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
94
95
96
97
98
99
100
101
102
103
104
105
|
const int MAXN = 1e3+10;
const int MAXQ = 1e5+10;
const int MAXM = (MAXN << 2) + MAXQ * 11;
int n, m, q;
bitset<MAXN> limit;
namespace HJTTree {
int idx = 0, root[MAXQ];
struct Node {
int l, r, mid, lch, rch, val;
bitset<MAXN> bit;
} tree[MAXM];
int build(int l, int r) {
int u = ++idx;
tree[u] = Node{l, r, (l+r)>>1, 0, 0, 0};
tree[u].bit.reset();
if (l < r) {
tree[u].lch = build(l, tree[u].mid);
tree[u].rch = build(tree[u].mid + 1, r);
}
return u;
}
void init() {
root[0] = build(1, n);
}
void push_up(int u) {
tree[u].val = tree[tree[u].lch].val + tree[tree[u].rch].val;
}
int update(int pre, int pos, int c, int d) {
int u = ++idx;
tree[u] = tree[pre];
if (tree[u].l == tree[u].r) {
tree[u].bit[c] = d;
tree[u].val = tree[u].bit.count();
}
else {
if (pos <= tree[u].mid)
tree[u].lch = update(tree[pre].lch, pos, c, d);
else
tree[u].rch = update(tree[pre].rch, pos, c, d);
push_up(u);
}
return u;
}
int flip(int pre, int pos) {
int u = ++idx;
tree[u] = tree[pre];
if (tree[u].l == tree[u].r) {
tree[u].bit.flip();
tree[u].bit &= limit;
tree[u].val = tree[u].bit.count();
}
else {
if (pos <= tree[u].mid)
tree[u].lch = flip(tree[pre].lch, pos);
else
tree[u].rch = flip(tree[pre].rch, pos);
push_up(u);
}
return u;
}
int query(int i) {
return tree[root[i]].val;
}
}
int main() {
scanf("%d%d%d", &n, &m, &q);
HJTTree::init();
limit.reset();
for (int i = 1; i <= m; i++)
limit[i] = 1;
for (int i = 1; i <= q; i++) {
int op, x, y;
scanf("%d", &op);
if (op == 1) {
scanf("%d%d", &x, &y);
HJTTree::root[i] = HJTTree::update(HJTTree::root[i-1], x, y, 1);
printf("%d\n", HJTTree::query(i));
}
else if (op == 2) {
scanf("%d%d", &x, &y);
HJTTree::root[i] = HJTTree::update(HJTTree::root[i-1], x, y, 0);
printf("%d\n", HJTTree::query(i));
}
else if (op == 3) {
scanf("%d", &x);
HJTTree::root[i] = HJTTree::flip(HJTTree::root[i-1], x);
printf("%d\n", HJTTree::query(i));}
else {
scanf("%d", &x);
HJTTree::root[i] = HJTTree::root[x];
printf("%d\n", HJTTree::query(i));
}
}
return 0;
}
|