Problem’s Website
- 色板游戏
Solution
- 首先这道题要维护区间内的颜色种类,那么显然要用线段树。
- 对于30种颜色,我们可以用状压来记录。
Code
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
using namespace std;
int sc() {
int xx = 0, ff = 1; char cch = gc;
while(cch < '0' || cch > '9') {
if(cch == '-') ff = -1; cch = gc;
}
while(cch >= '0' && cch <='9') {
xx = (xx << 1) + (xx << 3) + (cch ^ '0'); cch = gc;
}
return xx * ff;
}
struct LST {
int data, tag;
}lst[100010 << 2];
int n, m, o;
void build(int k, int l, int r) {
if(l == r) {
lst[k].data = (1 << 1);
return ;
}
int mid = l + r >> 1;
build(k << 1, l, mid);
build(k << 1 | 1, mid + 1, r);
lst[k].data = lst[k << 1].data | lst[k << 1 | 1].data;
}
void pushdown(int k, int l, int r ) {
if(lst[k].tag == 0) return ;
lst[k << 1].tag = lst[k << 1 | 1].tag = lst[k].tag;
lst[k << 1].data = lst[k << 1 | 1].data = (1 << lst[k].tag);
lst[k].tag = 0;
}
void modify(int k, int l, int r, int x, int y, int num) {
if(l >= x&& r <= y) {
lst[k].tag = num;
lst[k].data = (1 << num);
return ;
}
pushdown(k, l, r);
int mid = l + r >> 1;
if(x <= mid) modify(k << 1, l, mid, x, y, num);
if(y > mid) modify(k << 1 | 1, mid + 1, r, x, y, num);
lst[k].data = lst[k << 1].data | lst[k << 1 | 1].data;
}
int query(int k, int l, int r, int x, int y) {
if(l >= x && r <= y)
return lst[k].data;
pushdown(k, l, r);
int mid = l + r >> 1;
int res = 0;
if(x <= mid) res |= query(k << 1, l, mid, x, y);
if(y > mid) res |= query(k << 1 | 1, mid + 1, r, x, y);
return res;
}
int main() {
// freopen("AK.txt", "w", stdout);/s
n = sc(); m = sc(); o = sc();
build(1, 1, n);
for(int i = 1; i <= o; i++) {
char c[2];
cin >> c[0];
int from = sc(), to = sc();
if(c[0] == 'C') {
int num = sc();
if(from > to) swap(from, to);
modify(1, 1, n, from, to, num);
}
else {
int ans = 0;
if(from > to) swap(from, to);
int res = query(1, 1, n, from, to);
for(int i = 1; i <= m; i++)
if(res & (1 << i)) ans++;
printf("%d\n", ans);
}
}
return 0;
}
rp++