#include <bits/stdc++.h>
using namespace std;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 1e5+10, MOD = 1e9+7;
const int L = 19;
int n, m, q, st[N], en[N], depth[N], tt = 0;
vector<int> adj[N];
vector<int> lca_et;
int sparse[2 * N][L], st_lca[N], en_lca[N];
int dep_min(int a, int b) {
return depth[a] < depth[b] ? a : b;
}
void build_rmq() {
int k = sz(lca_et);
for (int i = 0; i < k; i++) sparse[i][0] = lca_et[i];
for (int c = 2, l = 1; c <= k; c *= 2, l++) {
for (int i = 0; i + c <= k; i++) {
sparse[i][l] = dep_min(sparse[i][l-1], sparse[i + c/2][l-1]);
}
}
}
int rmq_qry(int l, int r) {
int lg = 31 - __builtin_clz(r - l + 1);
int len = 1 << lg;
return dep_min(sparse[l][lg], sparse[r - len + 1][lg]);
}
int lca(int a, int b) {
if (st_lca[a] > st_lca[b]) swap(a, b);
return rmq_qry(st_lca[a], en_lca[b]);
}
void dfs(int c, int p) {
if (p != -1) depth[c] = depth[p] + 1;
st[c] = tt++;
st_lca[c] = en_lca[c] = sz(lca_et);
lca_et.push_back(c);
for (int nxt : adj[c]) if (nxt != p) {
dfs(nxt, c);
en_lca[c] = sz(lca_et);
lca_et.push_back(c);
}
en[c] = tt-1;
}
int ans[N], c[N], lca_l[N], lca_r[N];
int rev[N], par[N], w[N];
pair<int, int> range[N];
vector<int> aux[N];
void dfs_aux_range(int c) {
for (int nxt : aux[c]) {
dfs_aux_range(nxt);
range[c].first = max(range[c].first, range[nxt].first);
range[c].second = min(range[c].second, range[nxt].second);
}
}
void rec(int l, int r, vector<ar<int, 3>> qs) {
if (!sz(qs)) return;
if (r <= l) return;
int m = (l + r) / 2; // [l..m], [m+1..r]
vector<ar<int, 3>> one, two, mine;
for (auto qi : qs) {
if (qi[1] <= m)
one.push_back(qi);
else if (qi[0] > m)
two.push_back(qi);
else
mine.push_back(qi);
}
rec(l, m, one), rec(m+1, r, two);
vector<int> use;
for (int i = l; i <= r; i++) use.push_back(c[i]);
sort(use.begin(), use.end(), [&](int x, int y) {
return st[x] < st[y];
});
use.resize(unique(use.begin(), use.end()) - use.begin());
int base = sz(use);
for (int i = 0; i < base; i++) {
use.push_back(lca(use[i], use[(i+1) % base]));
}
sort(use.begin(), use.end(), [&](int x, int y) {
return st[x] < st[y];
});
use.resize(unique(use.begin(), use.end()) - use.begin());
for (int i = 0; i < sz(use); i++) {
rev[use[i]] = i;
aux[i].clear();
}
for (int i = 1; i < sz(use); i++) {
par[i] = rev[lca(use[i-1], use[i])];
aux[par[i]].push_back(i);
w[i] = depth[use[i]] - depth[use[par[i]]];
}
int k = sz(use);
// max index of something on the left, min index of something on the right
for (int i = 0; i < k; i++) {
range[i] = {-1, MOD};
}
for (int i = l; i <= m; i++) {
int cur = rev[c[i]];
range[cur].first = max(range[cur].first, i);
}
for (int i = m+1; i <= r; i++) {
int cur = rev[c[i]];
range[cur].second = min(range[cur].second, i);
}
dfs_aux_range(0);
for (int i = m; i >= l; i--) {
lca_l[i] = c[i];
if (i < m) lca_l[i] = lca(lca_l[i], lca_l[i+1]);
}
for (int i = m+1; i <= r; i++) {
lca_r[i] = c[i];
if (i > m+1) lca_r[i] = lca(lca_r[i], lca_r[i-1]);
}
int sum = 0;
for (int j = 1; j < k; j++) sum += w[j];
int top = use[0];
for (auto [L, R, i] : mine) {
ans[i] += sum;
ans[i] -= depth[lca(lca_l[L], lca_r[R])] - depth[top];
for (int j = 1; j < k; j++) {
if (range[j].first < L && R < range[j].second) {
ans[i] -= w[j];
}
}
}
}
void solve() {
cin >> n >> m >> q;
for (int i = 0; i < n-1; i++) {
int a, b; cin >> a >> b, --a, --b;
adj[a].push_back(b), adj[b].push_back(a);
}
dfs(0, -1);
build_rmq();
for (int i = 0; i < m; i++) {
cin >> c[i], --c[i];
}
vector<ar<int, 3>> qs(q);
for (int i = 0; i < q; i++) {
int l, r; cin >> l >> r, --l, --r;
qs[i] = {l, r, i};
}
rec(0, m-1, qs);
for (int i = 0; i < q; i++) cout << ans[i]+1 << '\n';
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int T = 1;
// cin >> T;
while (T--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5032 KB |
Output is correct |
3 |
Correct |
3 ms |
5032 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 |
4 ms |
5160 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5168 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5164 KB |
Output is correct |
16 |
Correct |
3 ms |
5204 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
19 |
Correct |
3 ms |
5204 KB |
Output is correct |
20 |
Correct |
3 ms |
5204 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
4 ms |
5164 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
4 ms |
5076 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
2 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5028 KB |
Output is correct |
29 |
Correct |
3 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5032 KB |
Output is correct |
3 |
Correct |
3 ms |
5032 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 |
4 ms |
5160 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5168 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5164 KB |
Output is correct |
16 |
Correct |
3 ms |
5204 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
19 |
Correct |
3 ms |
5204 KB |
Output is correct |
20 |
Correct |
3 ms |
5204 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
4 ms |
5164 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
4 ms |
5076 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
2 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5028 KB |
Output is correct |
29 |
Correct |
3 ms |
5204 KB |
Output is correct |
30 |
Correct |
9 ms |
5588 KB |
Output is correct |
31 |
Correct |
11 ms |
5600 KB |
Output is correct |
32 |
Correct |
14 ms |
5684 KB |
Output is correct |
33 |
Correct |
15 ms |
5744 KB |
Output is correct |
34 |
Correct |
15 ms |
5760 KB |
Output is correct |
35 |
Correct |
10 ms |
5684 KB |
Output is correct |
36 |
Correct |
8 ms |
5692 KB |
Output is correct |
37 |
Correct |
9 ms |
5716 KB |
Output is correct |
38 |
Correct |
7 ms |
5844 KB |
Output is correct |
39 |
Correct |
8 ms |
5844 KB |
Output is correct |
40 |
Correct |
11 ms |
5812 KB |
Output is correct |
41 |
Correct |
6 ms |
5844 KB |
Output is correct |
42 |
Correct |
6 ms |
5808 KB |
Output is correct |
43 |
Correct |
8 ms |
5816 KB |
Output is correct |
44 |
Correct |
8 ms |
5716 KB |
Output is correct |
45 |
Correct |
8 ms |
5688 KB |
Output is correct |
46 |
Correct |
8 ms |
5716 KB |
Output is correct |
47 |
Correct |
6 ms |
5692 KB |
Output is correct |
48 |
Correct |
6 ms |
5716 KB |
Output is correct |
49 |
Correct |
6 ms |
5688 KB |
Output is correct |
50 |
Correct |
14 ms |
5716 KB |
Output is correct |
51 |
Correct |
13 ms |
5736 KB |
Output is correct |
52 |
Correct |
13 ms |
5684 KB |
Output is correct |
53 |
Correct |
13 ms |
5688 KB |
Output is correct |
54 |
Correct |
13 ms |
5752 KB |
Output is correct |
55 |
Correct |
13 ms |
5736 KB |
Output is correct |
56 |
Correct |
4 ms |
5204 KB |
Output is correct |
57 |
Correct |
3 ms |
5548 KB |
Output is correct |
58 |
Correct |
10 ms |
5684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5032 KB |
Output is correct |
3 |
Correct |
3 ms |
5176 KB |
Output is correct |
4 |
Correct |
2686 ms |
30228 KB |
Output is correct |
5 |
Correct |
2127 ms |
36416 KB |
Output is correct |
6 |
Correct |
2462 ms |
40044 KB |
Output is correct |
7 |
Correct |
4907 ms |
45832 KB |
Output is correct |
8 |
Correct |
4866 ms |
45740 KB |
Output is correct |
9 |
Correct |
4769 ms |
45752 KB |
Output is correct |
10 |
Correct |
4953 ms |
45748 KB |
Output is correct |
11 |
Execution timed out |
5057 ms |
45844 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
203 ms |
20368 KB |
Output is correct |
3 |
Correct |
367 ms |
22544 KB |
Output is correct |
4 |
Correct |
235 ms |
24124 KB |
Output is correct |
5 |
Correct |
92 ms |
31420 KB |
Output is correct |
6 |
Correct |
161 ms |
32192 KB |
Output is correct |
7 |
Correct |
183 ms |
32572 KB |
Output is correct |
8 |
Correct |
271 ms |
32728 KB |
Output is correct |
9 |
Correct |
273 ms |
32740 KB |
Output is correct |
10 |
Correct |
379 ms |
32820 KB |
Output is correct |
11 |
Correct |
364 ms |
32884 KB |
Output is correct |
12 |
Correct |
399 ms |
32960 KB |
Output is correct |
13 |
Correct |
449 ms |
33512 KB |
Output is correct |
14 |
Correct |
497 ms |
34896 KB |
Output is correct |
15 |
Correct |
513 ms |
40420 KB |
Output is correct |
16 |
Correct |
322 ms |
33344 KB |
Output is correct |
17 |
Correct |
329 ms |
33476 KB |
Output is correct |
18 |
Correct |
317 ms |
33392 KB |
Output is correct |
19 |
Correct |
89 ms |
32296 KB |
Output is correct |
20 |
Correct |
100 ms |
32308 KB |
Output is correct |
21 |
Correct |
136 ms |
32596 KB |
Output is correct |
22 |
Correct |
161 ms |
32588 KB |
Output is correct |
23 |
Correct |
218 ms |
32672 KB |
Output is correct |
24 |
Correct |
236 ms |
32784 KB |
Output is correct |
25 |
Correct |
272 ms |
32672 KB |
Output is correct |
26 |
Correct |
274 ms |
32680 KB |
Output is correct |
27 |
Correct |
316 ms |
32652 KB |
Output is correct |
28 |
Correct |
332 ms |
32704 KB |
Output is correct |
29 |
Correct |
390 ms |
32792 KB |
Output is correct |
30 |
Correct |
415 ms |
32948 KB |
Output is correct |
31 |
Correct |
423 ms |
33288 KB |
Output is correct |
32 |
Correct |
447 ms |
34104 KB |
Output is correct |
33 |
Correct |
488 ms |
35528 KB |
Output is correct |
34 |
Correct |
508 ms |
39616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5028 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Execution timed out |
5086 ms |
27184 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5032 KB |
Output is correct |
3 |
Correct |
3 ms |
5032 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 |
4 ms |
5160 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5168 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5164 KB |
Output is correct |
16 |
Correct |
3 ms |
5204 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
19 |
Correct |
3 ms |
5204 KB |
Output is correct |
20 |
Correct |
3 ms |
5204 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
4 ms |
5164 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
4 ms |
5076 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
2 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5028 KB |
Output is correct |
29 |
Correct |
3 ms |
5204 KB |
Output is correct |
30 |
Correct |
9 ms |
5588 KB |
Output is correct |
31 |
Correct |
11 ms |
5600 KB |
Output is correct |
32 |
Correct |
14 ms |
5684 KB |
Output is correct |
33 |
Correct |
15 ms |
5744 KB |
Output is correct |
34 |
Correct |
15 ms |
5760 KB |
Output is correct |
35 |
Correct |
10 ms |
5684 KB |
Output is correct |
36 |
Correct |
8 ms |
5692 KB |
Output is correct |
37 |
Correct |
9 ms |
5716 KB |
Output is correct |
38 |
Correct |
7 ms |
5844 KB |
Output is correct |
39 |
Correct |
8 ms |
5844 KB |
Output is correct |
40 |
Correct |
11 ms |
5812 KB |
Output is correct |
41 |
Correct |
6 ms |
5844 KB |
Output is correct |
42 |
Correct |
6 ms |
5808 KB |
Output is correct |
43 |
Correct |
8 ms |
5816 KB |
Output is correct |
44 |
Correct |
8 ms |
5716 KB |
Output is correct |
45 |
Correct |
8 ms |
5688 KB |
Output is correct |
46 |
Correct |
8 ms |
5716 KB |
Output is correct |
47 |
Correct |
6 ms |
5692 KB |
Output is correct |
48 |
Correct |
6 ms |
5716 KB |
Output is correct |
49 |
Correct |
6 ms |
5688 KB |
Output is correct |
50 |
Correct |
14 ms |
5716 KB |
Output is correct |
51 |
Correct |
13 ms |
5736 KB |
Output is correct |
52 |
Correct |
13 ms |
5684 KB |
Output is correct |
53 |
Correct |
13 ms |
5688 KB |
Output is correct |
54 |
Correct |
13 ms |
5752 KB |
Output is correct |
55 |
Correct |
13 ms |
5736 KB |
Output is correct |
56 |
Correct |
4 ms |
5204 KB |
Output is correct |
57 |
Correct |
3 ms |
5548 KB |
Output is correct |
58 |
Correct |
10 ms |
5684 KB |
Output is correct |
59 |
Correct |
2 ms |
5076 KB |
Output is correct |
60 |
Correct |
3 ms |
5032 KB |
Output is correct |
61 |
Correct |
3 ms |
5176 KB |
Output is correct |
62 |
Correct |
2686 ms |
30228 KB |
Output is correct |
63 |
Correct |
2127 ms |
36416 KB |
Output is correct |
64 |
Correct |
2462 ms |
40044 KB |
Output is correct |
65 |
Correct |
4907 ms |
45832 KB |
Output is correct |
66 |
Correct |
4866 ms |
45740 KB |
Output is correct |
67 |
Correct |
4769 ms |
45752 KB |
Output is correct |
68 |
Correct |
4953 ms |
45748 KB |
Output is correct |
69 |
Execution timed out |
5057 ms |
45844 KB |
Time limit exceeded |
70 |
Halted |
0 ms |
0 KB |
- |