#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);
while (l <= r) {
auto u = range.upper_bound({l, N, -1});
--u;
int levo, desno, clr;
tie(levo, desno, clr) = *u;
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);
range.insert({l, min(desno, r), c});
l = desno + 1;
}
}
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 |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10840 KB |
Output is correct |
4 |
Correct |
2 ms |
10840 KB |
Output is correct |
5 |
Correct |
3 ms |
10840 KB |
Output is correct |
6 |
Correct |
2 ms |
10840 KB |
Output is correct |
7 |
Correct |
3 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 |
10840 KB |
Output is correct |
13 |
Correct |
3 ms |
10840 KB |
Output is correct |
14 |
Correct |
3 ms |
10840 KB |
Output is correct |
15 |
Correct |
4 ms |
10840 KB |
Output is correct |
16 |
Correct |
4 ms |
10840 KB |
Output is correct |
17 |
Correct |
4 ms |
10840 KB |
Output is correct |
18 |
Correct |
3 ms |
10844 KB |
Output is correct |
19 |
Correct |
4 ms |
10844 KB |
Output is correct |
20 |
Correct |
4 ms |
10840 KB |
Output is correct |
21 |
Correct |
2 ms |
10840 KB |
Output is correct |
22 |
Correct |
3 ms |
10840 KB |
Output is correct |
23 |
Correct |
2 ms |
10840 KB |
Output is correct |
24 |
Correct |
3 ms |
10996 KB |
Output is correct |
25 |
Correct |
3 ms |
10840 KB |
Output is correct |
26 |
Correct |
2 ms |
11096 KB |
Output is correct |
27 |
Correct |
2 ms |
10840 KB |
Output is correct |
28 |
Correct |
2 ms |
10844 KB |
Output is correct |
29 |
Correct |
3 ms |
10840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10840 KB |
Output is correct |
4 |
Correct |
2 ms |
10840 KB |
Output is correct |
5 |
Correct |
3 ms |
10840 KB |
Output is correct |
6 |
Correct |
2 ms |
10840 KB |
Output is correct |
7 |
Correct |
3 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 |
10840 KB |
Output is correct |
13 |
Correct |
3 ms |
10840 KB |
Output is correct |
14 |
Correct |
3 ms |
10840 KB |
Output is correct |
15 |
Correct |
4 ms |
10840 KB |
Output is correct |
16 |
Correct |
4 ms |
10840 KB |
Output is correct |
17 |
Correct |
4 ms |
10840 KB |
Output is correct |
18 |
Correct |
3 ms |
10844 KB |
Output is correct |
19 |
Correct |
4 ms |
10844 KB |
Output is correct |
20 |
Correct |
4 ms |
10840 KB |
Output is correct |
21 |
Correct |
2 ms |
10840 KB |
Output is correct |
22 |
Correct |
3 ms |
10840 KB |
Output is correct |
23 |
Correct |
2 ms |
10840 KB |
Output is correct |
24 |
Correct |
3 ms |
10996 KB |
Output is correct |
25 |
Correct |
3 ms |
10840 KB |
Output is correct |
26 |
Correct |
2 ms |
11096 KB |
Output is correct |
27 |
Correct |
2 ms |
10840 KB |
Output is correct |
28 |
Correct |
2 ms |
10844 KB |
Output is correct |
29 |
Correct |
3 ms |
10840 KB |
Output is correct |
30 |
Correct |
6 ms |
11096 KB |
Output is correct |
31 |
Correct |
6 ms |
11112 KB |
Output is correct |
32 |
Correct |
8 ms |
11100 KB |
Output is correct |
33 |
Correct |
8 ms |
11096 KB |
Output is correct |
34 |
Correct |
7 ms |
11096 KB |
Output is correct |
35 |
Correct |
7 ms |
11096 KB |
Output is correct |
36 |
Correct |
8 ms |
11608 KB |
Output is correct |
37 |
Correct |
8 ms |
11096 KB |
Output is correct |
38 |
Correct |
74 ms |
11208 KB |
Output is correct |
39 |
Correct |
75 ms |
11096 KB |
Output is correct |
40 |
Correct |
74 ms |
11096 KB |
Output is correct |
41 |
Correct |
79 ms |
11352 KB |
Output is correct |
42 |
Correct |
73 ms |
11100 KB |
Output is correct |
43 |
Correct |
73 ms |
11184 KB |
Output is correct |
44 |
Correct |
39 ms |
11148 KB |
Output is correct |
45 |
Correct |
33 ms |
11096 KB |
Output is correct |
46 |
Correct |
36 ms |
11096 KB |
Output is correct |
47 |
Correct |
32 ms |
11096 KB |
Output is correct |
48 |
Correct |
30 ms |
11096 KB |
Output is correct |
49 |
Correct |
38 ms |
11336 KB |
Output is correct |
50 |
Correct |
4 ms |
11096 KB |
Output is correct |
51 |
Correct |
4 ms |
11096 KB |
Output is correct |
52 |
Correct |
4 ms |
11100 KB |
Output is correct |
53 |
Correct |
4 ms |
11100 KB |
Output is correct |
54 |
Correct |
4 ms |
11096 KB |
Output is correct |
55 |
Correct |
5 ms |
11100 KB |
Output is correct |
56 |
Correct |
3 ms |
10840 KB |
Output is correct |
57 |
Correct |
3 ms |
10840 KB |
Output is correct |
58 |
Correct |
7 ms |
11096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10840 KB |
Output is correct |
3 |
Correct |
3 ms |
10844 KB |
Output is correct |
4 |
Execution timed out |
5051 ms |
21496 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
249 ms |
16984 KB |
Output is correct |
3 |
Correct |
434 ms |
18256 KB |
Output is correct |
4 |
Correct |
331 ms |
18524 KB |
Output is correct |
5 |
Correct |
520 ms |
22112 KB |
Output is correct |
6 |
Correct |
523 ms |
22096 KB |
Output is correct |
7 |
Correct |
534 ms |
22208 KB |
Output is correct |
8 |
Correct |
527 ms |
22376 KB |
Output is correct |
9 |
Correct |
551 ms |
22224 KB |
Output is correct |
10 |
Correct |
524 ms |
22276 KB |
Output is correct |
11 |
Correct |
522 ms |
22096 KB |
Output is correct |
12 |
Correct |
511 ms |
22104 KB |
Output is correct |
13 |
Correct |
528 ms |
22696 KB |
Output is correct |
14 |
Correct |
522 ms |
23376 KB |
Output is correct |
15 |
Correct |
553 ms |
26752 KB |
Output is correct |
16 |
Correct |
525 ms |
22744 KB |
Output is correct |
17 |
Correct |
562 ms |
22532 KB |
Output is correct |
18 |
Correct |
536 ms |
22748 KB |
Output is correct |
19 |
Execution timed out |
5053 ms |
20288 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
2 ms |
10840 KB |
Output is correct |
3 |
Correct |
3 ms |
10844 KB |
Output is correct |
4 |
Correct |
588 ms |
22216 KB |
Output is correct |
5 |
Correct |
613 ms |
22076 KB |
Output is correct |
6 |
Correct |
710 ms |
25456 KB |
Output is correct |
7 |
Correct |
767 ms |
27508 KB |
Output is correct |
8 |
Correct |
755 ms |
27128 KB |
Output is correct |
9 |
Correct |
736 ms |
27232 KB |
Output is correct |
10 |
Correct |
730 ms |
27292 KB |
Output is correct |
11 |
Correct |
770 ms |
27216 KB |
Output is correct |
12 |
Correct |
752 ms |
27228 KB |
Output is correct |
13 |
Correct |
738 ms |
27156 KB |
Output is correct |
14 |
Correct |
36 ms |
15540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10840 KB |
Output is correct |
4 |
Correct |
2 ms |
10840 KB |
Output is correct |
5 |
Correct |
3 ms |
10840 KB |
Output is correct |
6 |
Correct |
2 ms |
10840 KB |
Output is correct |
7 |
Correct |
3 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 |
10840 KB |
Output is correct |
13 |
Correct |
3 ms |
10840 KB |
Output is correct |
14 |
Correct |
3 ms |
10840 KB |
Output is correct |
15 |
Correct |
4 ms |
10840 KB |
Output is correct |
16 |
Correct |
4 ms |
10840 KB |
Output is correct |
17 |
Correct |
4 ms |
10840 KB |
Output is correct |
18 |
Correct |
3 ms |
10844 KB |
Output is correct |
19 |
Correct |
4 ms |
10844 KB |
Output is correct |
20 |
Correct |
4 ms |
10840 KB |
Output is correct |
21 |
Correct |
2 ms |
10840 KB |
Output is correct |
22 |
Correct |
3 ms |
10840 KB |
Output is correct |
23 |
Correct |
2 ms |
10840 KB |
Output is correct |
24 |
Correct |
3 ms |
10996 KB |
Output is correct |
25 |
Correct |
3 ms |
10840 KB |
Output is correct |
26 |
Correct |
2 ms |
11096 KB |
Output is correct |
27 |
Correct |
2 ms |
10840 KB |
Output is correct |
28 |
Correct |
2 ms |
10844 KB |
Output is correct |
29 |
Correct |
3 ms |
10840 KB |
Output is correct |
30 |
Correct |
6 ms |
11096 KB |
Output is correct |
31 |
Correct |
6 ms |
11112 KB |
Output is correct |
32 |
Correct |
8 ms |
11100 KB |
Output is correct |
33 |
Correct |
8 ms |
11096 KB |
Output is correct |
34 |
Correct |
7 ms |
11096 KB |
Output is correct |
35 |
Correct |
7 ms |
11096 KB |
Output is correct |
36 |
Correct |
8 ms |
11608 KB |
Output is correct |
37 |
Correct |
8 ms |
11096 KB |
Output is correct |
38 |
Correct |
74 ms |
11208 KB |
Output is correct |
39 |
Correct |
75 ms |
11096 KB |
Output is correct |
40 |
Correct |
74 ms |
11096 KB |
Output is correct |
41 |
Correct |
79 ms |
11352 KB |
Output is correct |
42 |
Correct |
73 ms |
11100 KB |
Output is correct |
43 |
Correct |
73 ms |
11184 KB |
Output is correct |
44 |
Correct |
39 ms |
11148 KB |
Output is correct |
45 |
Correct |
33 ms |
11096 KB |
Output is correct |
46 |
Correct |
36 ms |
11096 KB |
Output is correct |
47 |
Correct |
32 ms |
11096 KB |
Output is correct |
48 |
Correct |
30 ms |
11096 KB |
Output is correct |
49 |
Correct |
38 ms |
11336 KB |
Output is correct |
50 |
Correct |
4 ms |
11096 KB |
Output is correct |
51 |
Correct |
4 ms |
11096 KB |
Output is correct |
52 |
Correct |
4 ms |
11100 KB |
Output is correct |
53 |
Correct |
4 ms |
11100 KB |
Output is correct |
54 |
Correct |
4 ms |
11096 KB |
Output is correct |
55 |
Correct |
5 ms |
11100 KB |
Output is correct |
56 |
Correct |
3 ms |
10840 KB |
Output is correct |
57 |
Correct |
3 ms |
10840 KB |
Output is correct |
58 |
Correct |
7 ms |
11096 KB |
Output is correct |
59 |
Correct |
2 ms |
10844 KB |
Output is correct |
60 |
Correct |
2 ms |
10840 KB |
Output is correct |
61 |
Correct |
3 ms |
10844 KB |
Output is correct |
62 |
Execution timed out |
5051 ms |
21496 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |