#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<cassert>
#include<set>
#include<utility>
using namespace std;
const int k = 2003;
vector<vector<int>> graph;
vector<int> in, dep, pos;
vector<vector<int>> anc;
int tst = 0;
void dfs(int node, int parent){
in[++tst] = node;
pos[node] = tst;
anc[0][node] = parent;
dep[node] = dep[parent] + 1;
for(auto x: graph[node]){
if(x == parent) continue;
dfs(x, node);
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
in.resize(n + 1);
dep.resize(n + 1);
pos.resize(n + 1);
anc.resize(18, vector<int>(n + 1));
graph.resize(n + 1);
int a, b;
for(int i = 1; i < n; i++){
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
dfs(1, 1);
vector<int> vec(m + 1);
for(int i = 1; i <= m; i++) cin >> vec[i], vec[i] = pos[vec[i]];
vector<int> ans(q);
for(int i = 1; i < 18; i++){
for(int j = 1; j <= n; j++){
anc[i][j] = anc[i - 1][anc[i - 1][j]];
}
}
auto lca = [&](int u, int v){
u = in[u], v = in[v];
if(dep[u] < dep[v]) swap(u, v);
for(int i = 17; i >= 0; i--){
if((1 << i) & (dep[u] - dep[v])) u = anc[i][u];
}
for(int i = 17; i >= 0; i--){
if(anc[i][u] != anc[i][v]) u = anc[i][u], v = anc[i][v];
}
if(u != v) u = anc[0][u], v = anc[0][v];
return pos[u];
};
auto dist = [&](int u, int v){
return dep[in[u]] + dep[in[v]] - 2 * dep[in[lca(u, v)]];
};
auto left = [&](int a, int b, int c){
return (dist(a, b) + dist(a, c) - dist(b, c)) / 2;
};
vector<vector<pair<pair<int, int>, int>>> queries(m / k + 1);
for(int i = 0; i < q; i++){
cin >> a >> b;
queries[(a - 1) / k].push_back(make_pair(make_pair(a, b), i));
}
auto cmp = [&](pair<pair<int, int>, int> c, pair<pair<int, int>, int> d){
return c.first.second < d.first.second;
};
for(int i = 0; i <= m / k; i++) sort(queries[i].begin(), queries[i].end(), cmp);
int lb = 0, now_lca = -1;
for(auto &x: queries){
lb += k;
multiset<int> s;
int cnt = 0, rb = lb, last_cnt = 0;
auto add = [&](int node){
if(s.empty()){
now_lca = node;
s.insert(node);
cnt = 1;
return;
}
auto iter = s.upper_bound(node);
if(iter != s.end() && (*iter) == node){
s.insert(node);
return;
}
if(iter == s.begin()){
cnt += left(node, (*iter), now_lca);
now_lca = lca(node, now_lca);
s.insert(node);
return;
}
if(iter == s.end()){
iter = prev(iter);
//cout << node << " " << left(node, (*iter), now_lca) << "\n";
//cout << node << " " << (*iter) << " " << now_lca << "\n";
cnt += left(node, (*iter), now_lca);
now_lca = lca(node, now_lca);
s.insert(node);
return;
}
int bf = (*prev(iter)), gf = (*iter);
cnt += left(node, bf, gf);
s.insert(node);
};
for(auto &y: x){
a = y.first.first, b = y.first.second;
while(b > rb){
rb++;
add(vec[rb]);
}
last_cnt = cnt;
for(int i = a; i <= min(b, lb); i++){
add(vec[i]);
}
ans[y.second] = cnt;
cnt = last_cnt;
for(int i = a; i <= min(b, lb); i++){
s.erase(s.find(vec[i]));
}
//cout << cnt << "\n";
}
}
for(int i = 0; i < q; i++) cout << ans[i] << "\n";
}
// g++ -std=c++17 pC.cpp -o run -fsanitize=undefined -fsanitize=address -Wall -Wextra -Wshadow
/*
7 6 2
1 2
1 3
2 4
2 5
3 6
3 7
2 3 6 4 5 7
1 3
4 6
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
9 ms |
348 KB |
Output is correct |
5 |
Correct |
7 ms |
504 KB |
Output is correct |
6 |
Correct |
6 ms |
456 KB |
Output is correct |
7 |
Correct |
6 ms |
348 KB |
Output is correct |
8 |
Correct |
4 ms |
348 KB |
Output is correct |
9 |
Correct |
12 ms |
344 KB |
Output is correct |
10 |
Correct |
11 ms |
524 KB |
Output is correct |
11 |
Correct |
18 ms |
348 KB |
Output is correct |
12 |
Correct |
30 ms |
348 KB |
Output is correct |
13 |
Correct |
31 ms |
528 KB |
Output is correct |
14 |
Correct |
39 ms |
348 KB |
Output is correct |
15 |
Correct |
12 ms |
348 KB |
Output is correct |
16 |
Correct |
11 ms |
344 KB |
Output is correct |
17 |
Correct |
12 ms |
348 KB |
Output is correct |
18 |
Correct |
12 ms |
348 KB |
Output is correct |
19 |
Correct |
11 ms |
348 KB |
Output is correct |
20 |
Correct |
12 ms |
348 KB |
Output is correct |
21 |
Correct |
10 ms |
348 KB |
Output is correct |
22 |
Correct |
10 ms |
532 KB |
Output is correct |
23 |
Correct |
11 ms |
348 KB |
Output is correct |
24 |
Correct |
10 ms |
348 KB |
Output is correct |
25 |
Correct |
14 ms |
344 KB |
Output is correct |
26 |
Correct |
11 ms |
348 KB |
Output is correct |
27 |
Correct |
9 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
9 ms |
348 KB |
Output is correct |
5 |
Correct |
7 ms |
504 KB |
Output is correct |
6 |
Correct |
6 ms |
456 KB |
Output is correct |
7 |
Correct |
6 ms |
348 KB |
Output is correct |
8 |
Correct |
4 ms |
348 KB |
Output is correct |
9 |
Correct |
12 ms |
344 KB |
Output is correct |
10 |
Correct |
11 ms |
524 KB |
Output is correct |
11 |
Correct |
18 ms |
348 KB |
Output is correct |
12 |
Correct |
30 ms |
348 KB |
Output is correct |
13 |
Correct |
31 ms |
528 KB |
Output is correct |
14 |
Correct |
39 ms |
348 KB |
Output is correct |
15 |
Correct |
12 ms |
348 KB |
Output is correct |
16 |
Correct |
11 ms |
344 KB |
Output is correct |
17 |
Correct |
12 ms |
348 KB |
Output is correct |
18 |
Correct |
12 ms |
348 KB |
Output is correct |
19 |
Correct |
11 ms |
348 KB |
Output is correct |
20 |
Correct |
12 ms |
348 KB |
Output is correct |
21 |
Correct |
10 ms |
348 KB |
Output is correct |
22 |
Correct |
10 ms |
532 KB |
Output is correct |
23 |
Correct |
11 ms |
348 KB |
Output is correct |
24 |
Correct |
10 ms |
348 KB |
Output is correct |
25 |
Correct |
14 ms |
344 KB |
Output is correct |
26 |
Correct |
11 ms |
348 KB |
Output is correct |
27 |
Correct |
9 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
288 ms |
604 KB |
Output is correct |
31 |
Correct |
440 ms |
600 KB |
Output is correct |
32 |
Correct |
622 ms |
852 KB |
Output is correct |
33 |
Correct |
621 ms |
852 KB |
Output is correct |
34 |
Correct |
615 ms |
852 KB |
Output is correct |
35 |
Correct |
1890 ms |
880 KB |
Output is correct |
36 |
Correct |
1927 ms |
856 KB |
Output is correct |
37 |
Correct |
1822 ms |
884 KB |
Output is correct |
38 |
Correct |
607 ms |
892 KB |
Output is correct |
39 |
Correct |
592 ms |
856 KB |
Output is correct |
40 |
Correct |
598 ms |
904 KB |
Output is correct |
41 |
Correct |
1807 ms |
960 KB |
Output is correct |
42 |
Correct |
1747 ms |
968 KB |
Output is correct |
43 |
Correct |
1758 ms |
860 KB |
Output is correct |
44 |
Correct |
600 ms |
860 KB |
Output is correct |
45 |
Correct |
623 ms |
860 KB |
Output is correct |
46 |
Correct |
695 ms |
856 KB |
Output is correct |
47 |
Correct |
1793 ms |
904 KB |
Output is correct |
48 |
Correct |
1788 ms |
904 KB |
Output is correct |
49 |
Correct |
1820 ms |
912 KB |
Output is correct |
50 |
Correct |
543 ms |
848 KB |
Output is correct |
51 |
Correct |
555 ms |
1116 KB |
Output is correct |
52 |
Correct |
586 ms |
840 KB |
Output is correct |
53 |
Correct |
557 ms |
852 KB |
Output is correct |
54 |
Correct |
542 ms |
848 KB |
Output is correct |
55 |
Correct |
549 ms |
924 KB |
Output is correct |
56 |
Correct |
416 ms |
580 KB |
Output is correct |
57 |
Correct |
1 ms |
600 KB |
Output is correct |
58 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
10 ms |
348 KB |
Output is correct |
3 |
Correct |
426 ms |
540 KB |
Output is correct |
4 |
Execution timed out |
5056 ms |
18400 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
53 ms |
9092 KB |
Output is correct |
3 |
Correct |
80 ms |
10100 KB |
Output is correct |
4 |
Correct |
64 ms |
11340 KB |
Output is correct |
5 |
Correct |
147 ms |
20936 KB |
Output is correct |
6 |
Correct |
140 ms |
17672 KB |
Output is correct |
7 |
Correct |
144 ms |
16460 KB |
Output is correct |
8 |
Correct |
136 ms |
16372 KB |
Output is correct |
9 |
Correct |
135 ms |
16260 KB |
Output is correct |
10 |
Correct |
116 ms |
16072 KB |
Output is correct |
11 |
Correct |
117 ms |
16084 KB |
Output is correct |
12 |
Correct |
103 ms |
16076 KB |
Output is correct |
13 |
Correct |
108 ms |
16844 KB |
Output is correct |
14 |
Correct |
92 ms |
17456 KB |
Output is correct |
15 |
Correct |
58 ms |
18892 KB |
Output is correct |
16 |
Correct |
122 ms |
18640 KB |
Output is correct |
17 |
Correct |
123 ms |
18636 KB |
Output is correct |
18 |
Correct |
116 ms |
18772 KB |
Output is correct |
19 |
Correct |
132 ms |
20764 KB |
Output is correct |
20 |
Correct |
139 ms |
18372 KB |
Output is correct |
21 |
Correct |
146 ms |
17356 KB |
Output is correct |
22 |
Correct |
183 ms |
16656 KB |
Output is correct |
23 |
Correct |
153 ms |
16336 KB |
Output is correct |
24 |
Correct |
152 ms |
16080 KB |
Output is correct |
25 |
Correct |
158 ms |
16068 KB |
Output is correct |
26 |
Correct |
157 ms |
16072 KB |
Output is correct |
27 |
Correct |
147 ms |
16084 KB |
Output is correct |
28 |
Correct |
158 ms |
16296 KB |
Output is correct |
29 |
Correct |
158 ms |
16076 KB |
Output is correct |
30 |
Correct |
136 ms |
16332 KB |
Output is correct |
31 |
Correct |
133 ms |
16528 KB |
Output is correct |
32 |
Correct |
127 ms |
16916 KB |
Output is correct |
33 |
Correct |
98 ms |
17500 KB |
Output is correct |
34 |
Correct |
52 ms |
19004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
9 ms |
476 KB |
Output is correct |
3 |
Correct |
424 ms |
540 KB |
Output is correct |
4 |
Execution timed out |
5018 ms |
16244 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
9 ms |
348 KB |
Output is correct |
5 |
Correct |
7 ms |
504 KB |
Output is correct |
6 |
Correct |
6 ms |
456 KB |
Output is correct |
7 |
Correct |
6 ms |
348 KB |
Output is correct |
8 |
Correct |
4 ms |
348 KB |
Output is correct |
9 |
Correct |
12 ms |
344 KB |
Output is correct |
10 |
Correct |
11 ms |
524 KB |
Output is correct |
11 |
Correct |
18 ms |
348 KB |
Output is correct |
12 |
Correct |
30 ms |
348 KB |
Output is correct |
13 |
Correct |
31 ms |
528 KB |
Output is correct |
14 |
Correct |
39 ms |
348 KB |
Output is correct |
15 |
Correct |
12 ms |
348 KB |
Output is correct |
16 |
Correct |
11 ms |
344 KB |
Output is correct |
17 |
Correct |
12 ms |
348 KB |
Output is correct |
18 |
Correct |
12 ms |
348 KB |
Output is correct |
19 |
Correct |
11 ms |
348 KB |
Output is correct |
20 |
Correct |
12 ms |
348 KB |
Output is correct |
21 |
Correct |
10 ms |
348 KB |
Output is correct |
22 |
Correct |
10 ms |
532 KB |
Output is correct |
23 |
Correct |
11 ms |
348 KB |
Output is correct |
24 |
Correct |
10 ms |
348 KB |
Output is correct |
25 |
Correct |
14 ms |
344 KB |
Output is correct |
26 |
Correct |
11 ms |
348 KB |
Output is correct |
27 |
Correct |
9 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
288 ms |
604 KB |
Output is correct |
31 |
Correct |
440 ms |
600 KB |
Output is correct |
32 |
Correct |
622 ms |
852 KB |
Output is correct |
33 |
Correct |
621 ms |
852 KB |
Output is correct |
34 |
Correct |
615 ms |
852 KB |
Output is correct |
35 |
Correct |
1890 ms |
880 KB |
Output is correct |
36 |
Correct |
1927 ms |
856 KB |
Output is correct |
37 |
Correct |
1822 ms |
884 KB |
Output is correct |
38 |
Correct |
607 ms |
892 KB |
Output is correct |
39 |
Correct |
592 ms |
856 KB |
Output is correct |
40 |
Correct |
598 ms |
904 KB |
Output is correct |
41 |
Correct |
1807 ms |
960 KB |
Output is correct |
42 |
Correct |
1747 ms |
968 KB |
Output is correct |
43 |
Correct |
1758 ms |
860 KB |
Output is correct |
44 |
Correct |
600 ms |
860 KB |
Output is correct |
45 |
Correct |
623 ms |
860 KB |
Output is correct |
46 |
Correct |
695 ms |
856 KB |
Output is correct |
47 |
Correct |
1793 ms |
904 KB |
Output is correct |
48 |
Correct |
1788 ms |
904 KB |
Output is correct |
49 |
Correct |
1820 ms |
912 KB |
Output is correct |
50 |
Correct |
543 ms |
848 KB |
Output is correct |
51 |
Correct |
555 ms |
1116 KB |
Output is correct |
52 |
Correct |
586 ms |
840 KB |
Output is correct |
53 |
Correct |
557 ms |
852 KB |
Output is correct |
54 |
Correct |
542 ms |
848 KB |
Output is correct |
55 |
Correct |
549 ms |
924 KB |
Output is correct |
56 |
Correct |
416 ms |
580 KB |
Output is correct |
57 |
Correct |
1 ms |
600 KB |
Output is correct |
58 |
Correct |
2 ms |
604 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
10 ms |
348 KB |
Output is correct |
61 |
Correct |
426 ms |
540 KB |
Output is correct |
62 |
Execution timed out |
5056 ms |
18400 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |