-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColor subtree.cpp
More file actions
154 lines (146 loc) · 2.77 KB
/
Color subtree.cpp
File metadata and controls
154 lines (146 loc) · 2.77 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <bits/stdc++.h>
using namespace std;
const long long inf = (1LL << 62);
typedef long long ll;
typedef pair<int, int> ii;
#define fr first
#define sc second
#define mp make_pair
#define bits(x) __builtin_popcountll(x)
#define left (index << 1)
#define right ((index << 1) + 1)
#define mid ((l + r) / 2)
const int MAXN = 4*100000 + 100;
int n, q;
vector<int> adj[MAXN];
int ar[MAXN];
int conv[MAXN];
int to[MAXN];
int dfs(int u, int cur, int par)
{
conv[u] = cur;
int ret = cur;
for (auto v : adj[u]) if (v != par)
{
ret = dfs(v, ret + 1, u);
}
to[conv[u]] = ret;
return ret;
}
///Segment Tree
int cl[MAXN];
ll st[5*MAXN];
ll lazy[5*MAXN];
inline void fix(int index, int l, int r)
{
if (lazy[index] == 0)
return;
st[index] = lazy[index];
if (l != r)
{
lazy[left] = lazy[index];
lazy[right] = lazy[index];
}
lazy[index] = 0;
}
void build(int index, int l, int r)
{
if (l == r)
{
st[index] = 1LL << cl[l];
}
else
{
build(left, l, mid);
build(right, mid + 1, r);
st[index] = st[left] | st[right];
}
}
void update(int index, int l, int r, int ql, int qr, int x)
{
fix(index, l, r);
if (l > qr || r < ql)
return;
if (l >= ql && r <= qr)
{
lazy[index] = (1LL << x);
fix(index, l, r);
}
else
{
update(left, l, mid, ql, qr, x);
update(right, mid + 1, r, ql, qr, x);
st[index] = st[left] | st[right];
}
}
ll query(int index, int l, int r, int ql, int qr)
{
fix(index, l, r);
if (l > qr || r < ql)
return 0;
if (l >= ql && r <= qr)
{
return st[index];
}
ll a = query(left, l, mid, ql, qr);
ll b = query(right, mid + 1, r, ql, qr);
return a | b;
}
inline void build()
{
dfs(0, 0, -1);
for (int i = 0; i < n; i++)
{
cl[conv[i]] = ar[i];
}
build(1, 0, n - 1);
}
inline void update(int ql, int x)
{
ql = conv[ql];
int qr = to[ql];
update(1, 0, n - 1, ql, qr, x);
}
inline int ans(int ql)
{
ql = conv[ql];
int qr = to[ql];
return bits(query(1, 0, n - 1, ql, qr));
}
///
int main()
{
scanf("%d%d", &n, &q);
for (int i = 0; i < n; i++)
{
scanf("%d", ar + i);
}
for (int i = 0; i < n - 1; i++)
{
int u, v;
scanf("%d%d", &u, &v);
u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
build();
while (q--)
{
int t;
scanf("%d", &t);
if (t == 1)
{
int r, c;
scanf("%d%d", &r, &c);
r--;
update(r, c);
}
else
{
int r;
scanf("%d", &r);
r--;
printf("%d\n", ans(r));
}
}
}