#include <bits/stdc++.h>
using namespace std;
#define int long long
#define f first
#define s second
const int N = 1e5 + 69;
int n, m, q, c[N], ans[N], sz[N], top[N], pos[N], fenw[N];
int node, par[N], dep[N];
vector<int> g[N];
vector<pair<int, int>> qs[N];
set<tuple<int, int, int>> range;
void modify(int v, int val) {
for (int i = v;i < N;i += (i & -i))
fenw[i] += val;
}
int Get(int v) {
int r = 0;
for (int i = v;i > 0;i -= (i & -i))
r += fenw[i];
return r;
}
void dfs(int v, int p) {
sz[v] = 1;
par[v] = p;
dep[v] = dep[p] + 1;
for (auto u : g[v]) {
if (u == p)
continue;
dfs(u, v);
sz[v] += sz[u];
}
}
void hld(int v, int p, int tp) {
pos[v] = ++node;
top[v] = tp;
int s = -1, mx = 0;
for (auto u : g[v]) {
if (u == p)
continue;
if (sz[u] > mx) {
mx = sz[u];
s = u;
}
}
if (s == -1)
return;
hld(s, v, tp);
for (auto u : g[v]) {
if (u == s || u == p)
continue;
hld(u, v, u);
}
}
void Modify(int l, int r, int c) {
if (l > r)
swap(l, r);
int x = l, y = r;
while (l <= r) {
auto u = range.upper_bound({l, N, N});
--u;
int levo, desno, clr;
tie(levo, desno, clr) = *u;
assert(l >= levo && l <= desno);
int cnt = 0;
range.erase(u);
if (l > levo)
range.insert({levo, l - 1, clr});
if (desno > r)
range.insert({r + 1, desno, clr});
cnt += min(desno, r) - l + 1;
modify(clr, -cnt);
modify(c, min(desno, r) - l + 1);
l = desno + 1;
}
range.insert({x, y, c});
}
void Change(int a, int b, int c) {
while (top[a] != top[b]) {
if (dep[top[a]] < dep[top[b]])
swap(a, b);
Modify(pos[top[a]], pos[a], c);
a = par[top[a]];
}
Modify(pos[b], pos[a], c);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n >> m >> q;
for (int i = 1;i <= n - 1;i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
for (int i = 1;i <= m;i++ )
cin >> c[i];
for (int i = 1;i <= q;i++) {
int l, r;
cin >> l >> r;
qs[r].push_back({l, i});
}
dfs(1, 0);
hld(1, 1, 1);
range.insert({1, n, N});
for (int i = 1;i <= m;i++) {
if (i > 1)
Change(c[i], c[i - 1], i - 1);
for (auto u : qs[i]) {
int l, ind;
tie(l, ind) = u;
if (l == i)
ans[ind] = 1;
else
ans[ind] = Get(N - 1) - Get(l - 1);
}
}
for (int i = 1;i <= q;i++)
cout << ans[i] << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
3 ms |
10840 KB |
Output is correct |
3 |
Correct |
2 ms |
10840 KB |
Output is correct |
4 |
Correct |
3 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10840 KB |
Output is correct |
6 |
Correct |
2 ms |
10840 KB |
Output is correct |
7 |
Correct |
2 ms |
10840 KB |
Output is correct |
8 |
Correct |
2 ms |
10840 KB |
Output is correct |
9 |
Correct |
3 ms |
10840 KB |
Output is correct |
10 |
Correct |
3 ms |
10840 KB |
Output is correct |
11 |
Correct |
3 ms |
10840 KB |
Output is correct |
12 |
Correct |
3 ms |
11096 KB |
Output is correct |
13 |
Correct |
2 ms |
10840 KB |
Output is correct |
14 |
Correct |
3 ms |
10840 KB |
Output is correct |
15 |
Correct |
2 ms |
10840 KB |
Output is correct |
16 |
Correct |
3 ms |
10844 KB |
Output is correct |
17 |
Correct |
3 ms |
10840 KB |
Output is correct |
18 |
Correct |
3 ms |
10844 KB |
Output is correct |
19 |
Correct |
3 ms |
10844 KB |
Output is correct |
20 |
Correct |
2 ms |
10840 KB |
Output is correct |
21 |
Correct |
2 ms |
10840 KB |
Output is correct |
22 |
Correct |
2 ms |
10840 KB |
Output is correct |
23 |
Correct |
2 ms |
10844 KB |
Output is correct |
24 |
Correct |
3 ms |
10840 KB |
Output is correct |
25 |
Correct |
2 ms |
10840 KB |
Output is correct |
26 |
Correct |
2 ms |
10840 KB |
Output is correct |
27 |
Correct |
2 ms |
10844 KB |
Output is correct |
28 |
Correct |
2 ms |
10844 KB |
Output is correct |
29 |
Correct |
3 ms |
10844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
3 ms |
10840 KB |
Output is correct |
3 |
Correct |
2 ms |
10840 KB |
Output is correct |
4 |
Correct |
3 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10840 KB |
Output is correct |
6 |
Correct |
2 ms |
10840 KB |
Output is correct |
7 |
Correct |
2 ms |
10840 KB |
Output is correct |
8 |
Correct |
2 ms |
10840 KB |
Output is correct |
9 |
Correct |
3 ms |
10840 KB |
Output is correct |
10 |
Correct |
3 ms |
10840 KB |
Output is correct |
11 |
Correct |
3 ms |
10840 KB |
Output is correct |
12 |
Correct |
3 ms |
11096 KB |
Output is correct |
13 |
Correct |
2 ms |
10840 KB |
Output is correct |
14 |
Correct |
3 ms |
10840 KB |
Output is correct |
15 |
Correct |
2 ms |
10840 KB |
Output is correct |
16 |
Correct |
3 ms |
10844 KB |
Output is correct |
17 |
Correct |
3 ms |
10840 KB |
Output is correct |
18 |
Correct |
3 ms |
10844 KB |
Output is correct |
19 |
Correct |
3 ms |
10844 KB |
Output is correct |
20 |
Correct |
2 ms |
10840 KB |
Output is correct |
21 |
Correct |
2 ms |
10840 KB |
Output is correct |
22 |
Correct |
2 ms |
10840 KB |
Output is correct |
23 |
Correct |
2 ms |
10844 KB |
Output is correct |
24 |
Correct |
3 ms |
10840 KB |
Output is correct |
25 |
Correct |
2 ms |
10840 KB |
Output is correct |
26 |
Correct |
2 ms |
10840 KB |
Output is correct |
27 |
Correct |
2 ms |
10844 KB |
Output is correct |
28 |
Correct |
2 ms |
10844 KB |
Output is correct |
29 |
Correct |
3 ms |
10844 KB |
Output is correct |
30 |
Correct |
5 ms |
10844 KB |
Output is correct |
31 |
Correct |
6 ms |
10844 KB |
Output is correct |
32 |
Correct |
6 ms |
11100 KB |
Output is correct |
33 |
Correct |
6 ms |
11096 KB |
Output is correct |
34 |
Correct |
6 ms |
11100 KB |
Output is correct |
35 |
Correct |
7 ms |
11100 KB |
Output is correct |
36 |
Correct |
6 ms |
11352 KB |
Output is correct |
37 |
Correct |
6 ms |
10968 KB |
Output is correct |
38 |
Correct |
4 ms |
11096 KB |
Output is correct |
39 |
Correct |
4 ms |
11100 KB |
Output is correct |
40 |
Correct |
4 ms |
11104 KB |
Output is correct |
41 |
Correct |
4 ms |
11112 KB |
Output is correct |
42 |
Correct |
4 ms |
11112 KB |
Output is correct |
43 |
Correct |
5 ms |
11100 KB |
Output is correct |
44 |
Correct |
4 ms |
10844 KB |
Output is correct |
45 |
Correct |
5 ms |
10840 KB |
Output is correct |
46 |
Correct |
5 ms |
10840 KB |
Output is correct |
47 |
Correct |
4 ms |
10840 KB |
Output is correct |
48 |
Correct |
5 ms |
10960 KB |
Output is correct |
49 |
Correct |
6 ms |
10840 KB |
Output is correct |
50 |
Correct |
6 ms |
11096 KB |
Output is correct |
51 |
Correct |
5 ms |
11100 KB |
Output is correct |
52 |
Correct |
5 ms |
11096 KB |
Output is correct |
53 |
Correct |
5 ms |
11096 KB |
Output is correct |
54 |
Correct |
6 ms |
11096 KB |
Output is correct |
55 |
Correct |
5 ms |
11096 KB |
Output is correct |
56 |
Correct |
3 ms |
10840 KB |
Output is correct |
57 |
Correct |
4 ms |
10840 KB |
Output is correct |
58 |
Correct |
6 ms |
11096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
4 ms |
10840 KB |
Output is correct |
3 |
Correct |
3 ms |
10844 KB |
Output is correct |
4 |
Correct |
77 ms |
18944 KB |
Output is correct |
5 |
Correct |
63 ms |
22616 KB |
Output is correct |
6 |
Correct |
78 ms |
23868 KB |
Output is correct |
7 |
Correct |
91 ms |
26740 KB |
Output is correct |
8 |
Correct |
101 ms |
26752 KB |
Output is correct |
9 |
Correct |
99 ms |
26956 KB |
Output is correct |
10 |
Correct |
102 ms |
26708 KB |
Output is correct |
11 |
Correct |
108 ms |
26760 KB |
Output is correct |
12 |
Correct |
85 ms |
26796 KB |
Output is correct |
13 |
Correct |
87 ms |
26708 KB |
Output is correct |
14 |
Correct |
94 ms |
26616 KB |
Output is correct |
15 |
Correct |
40 ms |
23488 KB |
Output is correct |
16 |
Correct |
92 ms |
26348 KB |
Output is correct |
17 |
Correct |
38 ms |
15508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
172 ms |
15444 KB |
Output is correct |
3 |
Correct |
293 ms |
16212 KB |
Output is correct |
4 |
Correct |
257 ms |
16464 KB |
Output is correct |
5 |
Correct |
423 ms |
19284 KB |
Output is correct |
6 |
Correct |
366 ms |
19280 KB |
Output is correct |
7 |
Correct |
406 ms |
19152 KB |
Output is correct |
8 |
Correct |
346 ms |
19296 KB |
Output is correct |
9 |
Correct |
347 ms |
19368 KB |
Output is correct |
10 |
Correct |
344 ms |
19360 KB |
Output is correct |
11 |
Correct |
350 ms |
19616 KB |
Output is correct |
12 |
Correct |
368 ms |
19280 KB |
Output is correct |
13 |
Correct |
353 ms |
19544 KB |
Output is correct |
14 |
Correct |
360 ms |
20176 KB |
Output is correct |
15 |
Correct |
382 ms |
22668 KB |
Output is correct |
16 |
Correct |
348 ms |
19536 KB |
Output is correct |
17 |
Correct |
384 ms |
19684 KB |
Output is correct |
18 |
Correct |
365 ms |
19848 KB |
Output is correct |
19 |
Correct |
207 ms |
14928 KB |
Output is correct |
20 |
Correct |
206 ms |
16656 KB |
Output is correct |
21 |
Correct |
219 ms |
16648 KB |
Output is correct |
22 |
Correct |
207 ms |
16464 KB |
Output is correct |
23 |
Correct |
201 ms |
16464 KB |
Output is correct |
24 |
Correct |
207 ms |
16464 KB |
Output is correct |
25 |
Correct |
206 ms |
16616 KB |
Output is correct |
26 |
Correct |
206 ms |
16464 KB |
Output is correct |
27 |
Correct |
215 ms |
16696 KB |
Output is correct |
28 |
Correct |
214 ms |
16560 KB |
Output is correct |
29 |
Correct |
225 ms |
16720 KB |
Output is correct |
30 |
Correct |
215 ms |
16900 KB |
Output is correct |
31 |
Correct |
228 ms |
17356 KB |
Output is correct |
32 |
Correct |
220 ms |
17736 KB |
Output is correct |
33 |
Correct |
234 ms |
18788 KB |
Output is correct |
34 |
Correct |
234 ms |
21072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10848 KB |
Output is correct |
3 |
Correct |
4 ms |
10840 KB |
Output is correct |
4 |
Correct |
473 ms |
19280 KB |
Output is correct |
5 |
Correct |
456 ms |
19372 KB |
Output is correct |
6 |
Correct |
562 ms |
21864 KB |
Output is correct |
7 |
Correct |
567 ms |
23116 KB |
Output is correct |
8 |
Correct |
603 ms |
23172 KB |
Output is correct |
9 |
Correct |
634 ms |
23120 KB |
Output is correct |
10 |
Correct |
646 ms |
23052 KB |
Output is correct |
11 |
Correct |
645 ms |
23168 KB |
Output is correct |
12 |
Correct |
579 ms |
23256 KB |
Output is correct |
13 |
Correct |
564 ms |
23200 KB |
Output is correct |
14 |
Correct |
35 ms |
14028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
3 ms |
10840 KB |
Output is correct |
3 |
Correct |
2 ms |
10840 KB |
Output is correct |
4 |
Correct |
3 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10840 KB |
Output is correct |
6 |
Correct |
2 ms |
10840 KB |
Output is correct |
7 |
Correct |
2 ms |
10840 KB |
Output is correct |
8 |
Correct |
2 ms |
10840 KB |
Output is correct |
9 |
Correct |
3 ms |
10840 KB |
Output is correct |
10 |
Correct |
3 ms |
10840 KB |
Output is correct |
11 |
Correct |
3 ms |
10840 KB |
Output is correct |
12 |
Correct |
3 ms |
11096 KB |
Output is correct |
13 |
Correct |
2 ms |
10840 KB |
Output is correct |
14 |
Correct |
3 ms |
10840 KB |
Output is correct |
15 |
Correct |
2 ms |
10840 KB |
Output is correct |
16 |
Correct |
3 ms |
10844 KB |
Output is correct |
17 |
Correct |
3 ms |
10840 KB |
Output is correct |
18 |
Correct |
3 ms |
10844 KB |
Output is correct |
19 |
Correct |
3 ms |
10844 KB |
Output is correct |
20 |
Correct |
2 ms |
10840 KB |
Output is correct |
21 |
Correct |
2 ms |
10840 KB |
Output is correct |
22 |
Correct |
2 ms |
10840 KB |
Output is correct |
23 |
Correct |
2 ms |
10844 KB |
Output is correct |
24 |
Correct |
3 ms |
10840 KB |
Output is correct |
25 |
Correct |
2 ms |
10840 KB |
Output is correct |
26 |
Correct |
2 ms |
10840 KB |
Output is correct |
27 |
Correct |
2 ms |
10844 KB |
Output is correct |
28 |
Correct |
2 ms |
10844 KB |
Output is correct |
29 |
Correct |
3 ms |
10844 KB |
Output is correct |
30 |
Correct |
5 ms |
10844 KB |
Output is correct |
31 |
Correct |
6 ms |
10844 KB |
Output is correct |
32 |
Correct |
6 ms |
11100 KB |
Output is correct |
33 |
Correct |
6 ms |
11096 KB |
Output is correct |
34 |
Correct |
6 ms |
11100 KB |
Output is correct |
35 |
Correct |
7 ms |
11100 KB |
Output is correct |
36 |
Correct |
6 ms |
11352 KB |
Output is correct |
37 |
Correct |
6 ms |
10968 KB |
Output is correct |
38 |
Correct |
4 ms |
11096 KB |
Output is correct |
39 |
Correct |
4 ms |
11100 KB |
Output is correct |
40 |
Correct |
4 ms |
11104 KB |
Output is correct |
41 |
Correct |
4 ms |
11112 KB |
Output is correct |
42 |
Correct |
4 ms |
11112 KB |
Output is correct |
43 |
Correct |
5 ms |
11100 KB |
Output is correct |
44 |
Correct |
4 ms |
10844 KB |
Output is correct |
45 |
Correct |
5 ms |
10840 KB |
Output is correct |
46 |
Correct |
5 ms |
10840 KB |
Output is correct |
47 |
Correct |
4 ms |
10840 KB |
Output is correct |
48 |
Correct |
5 ms |
10960 KB |
Output is correct |
49 |
Correct |
6 ms |
10840 KB |
Output is correct |
50 |
Correct |
6 ms |
11096 KB |
Output is correct |
51 |
Correct |
5 ms |
11100 KB |
Output is correct |
52 |
Correct |
5 ms |
11096 KB |
Output is correct |
53 |
Correct |
5 ms |
11096 KB |
Output is correct |
54 |
Correct |
6 ms |
11096 KB |
Output is correct |
55 |
Correct |
5 ms |
11096 KB |
Output is correct |
56 |
Correct |
3 ms |
10840 KB |
Output is correct |
57 |
Correct |
4 ms |
10840 KB |
Output is correct |
58 |
Correct |
6 ms |
11096 KB |
Output is correct |
59 |
Correct |
2 ms |
10844 KB |
Output is correct |
60 |
Correct |
4 ms |
10840 KB |
Output is correct |
61 |
Correct |
3 ms |
10844 KB |
Output is correct |
62 |
Correct |
77 ms |
18944 KB |
Output is correct |
63 |
Correct |
63 ms |
22616 KB |
Output is correct |
64 |
Correct |
78 ms |
23868 KB |
Output is correct |
65 |
Correct |
91 ms |
26740 KB |
Output is correct |
66 |
Correct |
101 ms |
26752 KB |
Output is correct |
67 |
Correct |
99 ms |
26956 KB |
Output is correct |
68 |
Correct |
102 ms |
26708 KB |
Output is correct |
69 |
Correct |
108 ms |
26760 KB |
Output is correct |
70 |
Correct |
85 ms |
26796 KB |
Output is correct |
71 |
Correct |
87 ms |
26708 KB |
Output is correct |
72 |
Correct |
94 ms |
26616 KB |
Output is correct |
73 |
Correct |
40 ms |
23488 KB |
Output is correct |
74 |
Correct |
92 ms |
26348 KB |
Output is correct |
75 |
Correct |
38 ms |
15508 KB |
Output is correct |
76 |
Correct |
2 ms |
10840 KB |
Output is correct |
77 |
Correct |
172 ms |
15444 KB |
Output is correct |
78 |
Correct |
293 ms |
16212 KB |
Output is correct |
79 |
Correct |
257 ms |
16464 KB |
Output is correct |
80 |
Correct |
423 ms |
19284 KB |
Output is correct |
81 |
Correct |
366 ms |
19280 KB |
Output is correct |
82 |
Correct |
406 ms |
19152 KB |
Output is correct |
83 |
Correct |
346 ms |
19296 KB |
Output is correct |
84 |
Correct |
347 ms |
19368 KB |
Output is correct |
85 |
Correct |
344 ms |
19360 KB |
Output is correct |
86 |
Correct |
350 ms |
19616 KB |
Output is correct |
87 |
Correct |
368 ms |
19280 KB |
Output is correct |
88 |
Correct |
353 ms |
19544 KB |
Output is correct |
89 |
Correct |
360 ms |
20176 KB |
Output is correct |
90 |
Correct |
382 ms |
22668 KB |
Output is correct |
91 |
Correct |
348 ms |
19536 KB |
Output is correct |
92 |
Correct |
384 ms |
19684 KB |
Output is correct |
93 |
Correct |
365 ms |
19848 KB |
Output is correct |
94 |
Correct |
207 ms |
14928 KB |
Output is correct |
95 |
Correct |
206 ms |
16656 KB |
Output is correct |
96 |
Correct |
219 ms |
16648 KB |
Output is correct |
97 |
Correct |
207 ms |
16464 KB |
Output is correct |
98 |
Correct |
201 ms |
16464 KB |
Output is correct |
99 |
Correct |
207 ms |
16464 KB |
Output is correct |
100 |
Correct |
206 ms |
16616 KB |
Output is correct |
101 |
Correct |
206 ms |
16464 KB |
Output is correct |
102 |
Correct |
215 ms |
16696 KB |
Output is correct |
103 |
Correct |
214 ms |
16560 KB |
Output is correct |
104 |
Correct |
225 ms |
16720 KB |
Output is correct |
105 |
Correct |
215 ms |
16900 KB |
Output is correct |
106 |
Correct |
228 ms |
17356 KB |
Output is correct |
107 |
Correct |
220 ms |
17736 KB |
Output is correct |
108 |
Correct |
234 ms |
18788 KB |
Output is correct |
109 |
Correct |
234 ms |
21072 KB |
Output is correct |
110 |
Correct |
2 ms |
10844 KB |
Output is correct |
111 |
Correct |
2 ms |
10848 KB |
Output is correct |
112 |
Correct |
4 ms |
10840 KB |
Output is correct |
113 |
Correct |
473 ms |
19280 KB |
Output is correct |
114 |
Correct |
456 ms |
19372 KB |
Output is correct |
115 |
Correct |
562 ms |
21864 KB |
Output is correct |
116 |
Correct |
567 ms |
23116 KB |
Output is correct |
117 |
Correct |
603 ms |
23172 KB |
Output is correct |
118 |
Correct |
634 ms |
23120 KB |
Output is correct |
119 |
Correct |
646 ms |
23052 KB |
Output is correct |
120 |
Correct |
645 ms |
23168 KB |
Output is correct |
121 |
Correct |
579 ms |
23256 KB |
Output is correct |
122 |
Correct |
564 ms |
23200 KB |
Output is correct |
123 |
Correct |
35 ms |
14028 KB |
Output is correct |
124 |
Correct |
345 ms |
24912 KB |
Output is correct |
125 |
Correct |
254 ms |
22324 KB |
Output is correct |
126 |
Correct |
392 ms |
25680 KB |
Output is correct |
127 |
Correct |
399 ms |
25424 KB |
Output is correct |
128 |
Correct |
402 ms |
25424 KB |
Output is correct |
129 |
Correct |
403 ms |
25440 KB |
Output is correct |
130 |
Correct |
386 ms |
25844 KB |
Output is correct |
131 |
Correct |
103 ms |
25936 KB |
Output is correct |
132 |
Correct |
103 ms |
26708 KB |
Output is correct |
133 |
Correct |
110 ms |
23724 KB |
Output is correct |
134 |
Correct |
243 ms |
21228 KB |
Output is correct |
135 |
Correct |
243 ms |
20976 KB |
Output is correct |
136 |
Correct |
246 ms |
21076 KB |
Output is correct |
137 |
Correct |
166 ms |
26736 KB |
Output is correct |
138 |
Correct |
154 ms |
26700 KB |
Output is correct |
139 |
Correct |
154 ms |
26588 KB |
Output is correct |
140 |
Correct |
165 ms |
26564 KB |
Output is correct |
141 |
Correct |
158 ms |
26812 KB |
Output is correct |
142 |
Correct |
165 ms |
26804 KB |
Output is correct |
143 |
Correct |
47 ms |
17856 KB |
Output is correct |
144 |
Correct |
399 ms |
24964 KB |
Output is correct |