#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 100000 + 10;
const int M = (1 << 17);
int n, m, q;
int T;
int sz[N];
int tin[N];
int tout[N];
int depth[N];
int par[N];
int head[N];
int heavy[N];
vector<int> g[N];
int sum_cnt[2 * M];
bool have[2 * M];
int color[2 * M];
void upd(int pos, int d) {
for (sum_cnt[pos += M] += d; pos > 1; pos >>= 1)
sum_cnt[pos >> 1] = sum_cnt[pos] + sum_cnt[pos ^ 1];
}
int get(int ql, int qr) {
int res = 0;
for (ql += M, qr += M + 1; ql < qr; ql >>= 1, qr >>= 1) {
if (ql & 1)
res += sum_cnt[ql++];
if (qr & 1)
res += sum_cnt[--qr];
}
return res;
}
void precalc(int s, int p = 0) {
sz[s] = 1;
par[s] = p;
depth[s] = depth[p] + (p > 0);
for (int &to : g[s]) {
if (to == p)
continue;
precalc(to, s);
sz[s] += sz[to];
if (sz[heavy[s]] > sz[to])
heavy[s] = to;
}
}
void decompose(int s, int h) {
tin[s] = T++;
head[s] = h;
if(heavy[s]) decompose(heavy[s], h);
for(int to : g[s]) {
if(to == par[s] || to == heavy[s]) continue;
decompose(to, to);
}
tout[s] = T;
}
void recalc(int v) {
have[v] = (color[v] > 0);
if(2 * v < 2 * M && have[2 * v]) have[v] = true;
if(2 * v + 1 < 2 * M && have[2 * v + 1]) have[v] = true;
}
void push(int v) {
if(!color[v] || v >= M) return;
color[2 * v] = color[v];
recalc(2 * v);
color[2 * v + 1] = color[v];
recalc(2 * v + 1);
color[v] = 0;
}
void set_color(int v, int d, int cl) {
if(color[v])
upd(color[v], -d);
color[v] = cl;
if(color[v])
upd(color[v], d);
recalc(v);
}
void clear(int l, int r, int v) {
set_color(v, r - l + 1, 0);
if(!have[v]) return;
int mid = (l + r) / 2;
clear(l, mid, 2 * v);
clear(mid + 1, r, 2 * v + 1);
have[v] = false;
}
void update(int ql, int qr, int cl, int l, int r, int v, bool type = 0) {
if(qr < l || ql > r) return;
if(ql <= l && r <= qr) {
clear(l, r, v);
set_color(v, r - l + 1, cl);
return;
}
push(v);
int mid = (l + r) / 2;
update(ql, qr, cl, l, mid, 2 * v, 0);
update(ql, qr, cl, mid + 1, r, 2 * v + 1, 0);
recalc(v);
}
void update(int u, int v, int cl) {
for(; head[u] != head[v]; v = par[head[v]]) {
if(depth[head[u]] > depth[head[v]]) swap(u, v);
update(tin[head[v]], tin[v], cl, 0, M - 1, 1);
}
if(depth[u] > depth[v]) swap(u, v);
update(tin[u], tin[v], cl, 0, M - 1, 1);
}
struct query {
int l, r, i;
};
vector<query> qr[N];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> q;
for(int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
int c[m + 1];
for(int i = 1; i <= m; i++)
cin >> c[i];
precalc(1);
decompose(1, 1);
for(int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
qr[r].push_back({l, r, i});
}
int res[q] = {};
for(int i = 1; i <= m; i++) {
if(i > 1)
update(c[i], c[i - 1], i - 1);
for(auto t : qr[i]) {
if(t.l == t.r) res[t.i] = 1;
else res[t.i] = get(t.l, t.r);
}
}
for(int i = 0; i < q; i++)
cout << res[i] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5036 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5036 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5040 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
8 ms |
5040 KB |
Output is correct |
16 |
Correct |
8 ms |
5164 KB |
Output is correct |
17 |
Correct |
8 ms |
5164 KB |
Output is correct |
18 |
Correct |
6 ms |
5076 KB |
Output is correct |
19 |
Correct |
9 ms |
5040 KB |
Output is correct |
20 |
Correct |
7 ms |
5044 KB |
Output is correct |
21 |
Correct |
3 ms |
5036 KB |
Output is correct |
22 |
Correct |
3 ms |
5036 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5036 KB |
Output is correct |
25 |
Correct |
3 ms |
5044 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5036 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5036 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5040 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
8 ms |
5040 KB |
Output is correct |
16 |
Correct |
8 ms |
5164 KB |
Output is correct |
17 |
Correct |
8 ms |
5164 KB |
Output is correct |
18 |
Correct |
6 ms |
5076 KB |
Output is correct |
19 |
Correct |
9 ms |
5040 KB |
Output is correct |
20 |
Correct |
7 ms |
5044 KB |
Output is correct |
21 |
Correct |
3 ms |
5036 KB |
Output is correct |
22 |
Correct |
3 ms |
5036 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5036 KB |
Output is correct |
25 |
Correct |
3 ms |
5044 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5040 KB |
Output is correct |
30 |
Correct |
7 ms |
5180 KB |
Output is correct |
31 |
Correct |
7 ms |
5204 KB |
Output is correct |
32 |
Correct |
10 ms |
5332 KB |
Output is correct |
33 |
Correct |
9 ms |
5300 KB |
Output is correct |
34 |
Correct |
9 ms |
5332 KB |
Output is correct |
35 |
Correct |
9 ms |
5360 KB |
Output is correct |
36 |
Correct |
9 ms |
5332 KB |
Output is correct |
37 |
Correct |
11 ms |
5360 KB |
Output is correct |
38 |
Correct |
261 ms |
5396 KB |
Output is correct |
39 |
Correct |
275 ms |
5544 KB |
Output is correct |
40 |
Correct |
271 ms |
5424 KB |
Output is correct |
41 |
Correct |
272 ms |
5440 KB |
Output is correct |
42 |
Correct |
267 ms |
5436 KB |
Output is correct |
43 |
Correct |
264 ms |
5400 KB |
Output is correct |
44 |
Correct |
127 ms |
5356 KB |
Output is correct |
45 |
Correct |
103 ms |
5356 KB |
Output is correct |
46 |
Correct |
120 ms |
5372 KB |
Output is correct |
47 |
Correct |
112 ms |
5360 KB |
Output is correct |
48 |
Correct |
88 ms |
5332 KB |
Output is correct |
49 |
Correct |
128 ms |
5332 KB |
Output is correct |
50 |
Correct |
5 ms |
5308 KB |
Output is correct |
51 |
Correct |
5 ms |
5332 KB |
Output is correct |
52 |
Correct |
5 ms |
5332 KB |
Output is correct |
53 |
Correct |
6 ms |
5300 KB |
Output is correct |
54 |
Correct |
5 ms |
5332 KB |
Output is correct |
55 |
Correct |
5 ms |
5304 KB |
Output is correct |
56 |
Correct |
4 ms |
5176 KB |
Output is correct |
57 |
Correct |
4 ms |
5204 KB |
Output is correct |
58 |
Correct |
9 ms |
5360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5036 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Execution timed out |
5033 ms |
14084 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
276 ms |
9436 KB |
Output is correct |
3 |
Correct |
485 ms |
9920 KB |
Output is correct |
4 |
Correct |
367 ms |
10164 KB |
Output is correct |
5 |
Correct |
520 ms |
12468 KB |
Output is correct |
6 |
Correct |
514 ms |
12512 KB |
Output is correct |
7 |
Correct |
533 ms |
12884 KB |
Output is correct |
8 |
Correct |
515 ms |
12516 KB |
Output is correct |
9 |
Correct |
562 ms |
12508 KB |
Output is correct |
10 |
Correct |
536 ms |
12520 KB |
Output is correct |
11 |
Correct |
514 ms |
12484 KB |
Output is correct |
12 |
Correct |
577 ms |
12564 KB |
Output is correct |
13 |
Correct |
525 ms |
12808 KB |
Output is correct |
14 |
Correct |
514 ms |
13672 KB |
Output is correct |
15 |
Correct |
558 ms |
16216 KB |
Output is correct |
16 |
Correct |
520 ms |
12804 KB |
Output is correct |
17 |
Correct |
562 ms |
12908 KB |
Output is correct |
18 |
Correct |
525 ms |
12876 KB |
Output is correct |
19 |
Execution timed out |
5047 ms |
11964 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5036 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
584 ms |
12720 KB |
Output is correct |
5 |
Correct |
609 ms |
12712 KB |
Output is correct |
6 |
Correct |
705 ms |
14680 KB |
Output is correct |
7 |
Correct |
714 ms |
15808 KB |
Output is correct |
8 |
Correct |
700 ms |
15716 KB |
Output is correct |
9 |
Correct |
714 ms |
15636 KB |
Output is correct |
10 |
Correct |
756 ms |
15672 KB |
Output is correct |
11 |
Correct |
711 ms |
15636 KB |
Output is correct |
12 |
Correct |
712 ms |
15620 KB |
Output is correct |
13 |
Correct |
720 ms |
15608 KB |
Output is correct |
14 |
Correct |
57 ms |
9176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5036 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5036 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5040 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
8 ms |
5040 KB |
Output is correct |
16 |
Correct |
8 ms |
5164 KB |
Output is correct |
17 |
Correct |
8 ms |
5164 KB |
Output is correct |
18 |
Correct |
6 ms |
5076 KB |
Output is correct |
19 |
Correct |
9 ms |
5040 KB |
Output is correct |
20 |
Correct |
7 ms |
5044 KB |
Output is correct |
21 |
Correct |
3 ms |
5036 KB |
Output is correct |
22 |
Correct |
3 ms |
5036 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5036 KB |
Output is correct |
25 |
Correct |
3 ms |
5044 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5040 KB |
Output is correct |
30 |
Correct |
7 ms |
5180 KB |
Output is correct |
31 |
Correct |
7 ms |
5204 KB |
Output is correct |
32 |
Correct |
10 ms |
5332 KB |
Output is correct |
33 |
Correct |
9 ms |
5300 KB |
Output is correct |
34 |
Correct |
9 ms |
5332 KB |
Output is correct |
35 |
Correct |
9 ms |
5360 KB |
Output is correct |
36 |
Correct |
9 ms |
5332 KB |
Output is correct |
37 |
Correct |
11 ms |
5360 KB |
Output is correct |
38 |
Correct |
261 ms |
5396 KB |
Output is correct |
39 |
Correct |
275 ms |
5544 KB |
Output is correct |
40 |
Correct |
271 ms |
5424 KB |
Output is correct |
41 |
Correct |
272 ms |
5440 KB |
Output is correct |
42 |
Correct |
267 ms |
5436 KB |
Output is correct |
43 |
Correct |
264 ms |
5400 KB |
Output is correct |
44 |
Correct |
127 ms |
5356 KB |
Output is correct |
45 |
Correct |
103 ms |
5356 KB |
Output is correct |
46 |
Correct |
120 ms |
5372 KB |
Output is correct |
47 |
Correct |
112 ms |
5360 KB |
Output is correct |
48 |
Correct |
88 ms |
5332 KB |
Output is correct |
49 |
Correct |
128 ms |
5332 KB |
Output is correct |
50 |
Correct |
5 ms |
5308 KB |
Output is correct |
51 |
Correct |
5 ms |
5332 KB |
Output is correct |
52 |
Correct |
5 ms |
5332 KB |
Output is correct |
53 |
Correct |
6 ms |
5300 KB |
Output is correct |
54 |
Correct |
5 ms |
5332 KB |
Output is correct |
55 |
Correct |
5 ms |
5304 KB |
Output is correct |
56 |
Correct |
4 ms |
5176 KB |
Output is correct |
57 |
Correct |
4 ms |
5204 KB |
Output is correct |
58 |
Correct |
9 ms |
5360 KB |
Output is correct |
59 |
Correct |
2 ms |
5076 KB |
Output is correct |
60 |
Correct |
2 ms |
5036 KB |
Output is correct |
61 |
Correct |
3 ms |
5204 KB |
Output is correct |
62 |
Execution timed out |
5033 ms |
14084 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |