#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e18;
struct segtree{
vector<int> tree;
segtree():tree(262144){}
void update(int k,int x){
k+=131072;
tree[k]=x;
while(k/=2){
tree[k]=min(tree[2*k],tree[2*k+1]);
}
}
int get(int a,int b){
a+=131072;b+=131072;
int ans = INF;
while(a<=b){
if(a&1)ans=min(ans,tree[a++]);
if(b%2 == 0)ans=min(ans,tree[b--]);
a/=2;b/=2;
}
return ans;
}
};
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,m,q;
cin >> n >> m >> q;
vector<vector<int>> adj(n+1);
for(int i=1;i<n;i++){
int a,b;cin>>a>>b;
adj[a].emplace_back(b);
adj[b].emplace_back(a);
}
int tim = 0;
vector<int> newidx(n+1);
function<void(int,int)> dfs = [&](int x,int par){
newidx[x]=++tim;
for(int&i:adj[x])if(i!=par)dfs(i,x);
};
dfs(1,0);
vector<vector<int>> newadj(n+1);
for(int i=1;i<=n;i++)newadj[newidx[i]]=adj[i];
for(int i=1;i<=n;i++){
for(int&x:newadj[i])x=newidx[x];
}
swap(newadj,adj);
vector<vector<int>> lift(n+1,vector<int>(17));
vector<int> depth(n+1);
function<void(int,int,int)> dfs2 = [&](int x,int par,int dep){
lift[x][0]=par;
depth[x]=dep;
for(int&i:adj[x])if(i!=par)dfs2(i,x,dep+1);
};
dfs2(1,0,1);
for(int bit=1;bit<17;bit++){
for(int i=1;i<=n;i++){
lift[i][bit] = lift[lift[i][bit-1]][bit-1];
}
}
auto lca = [&](int a,int b){
if(depth[a]>depth[b])swap(a,b);
int tar = depth[b]-depth[a];
for(int bit=0;bit<17;bit++)if(tar&(1<<bit))b=lift[b][bit];
if(a==b)return a;
for(int bit=16;bit>=0;bit--){
if(lift[a][bit]!=lift[b][bit]){
a = lift[a][bit];
b = lift[b][bit];
}
}
return lift[a][0];
};
vector<int> checkpoints(m+1);
for(int i=1;i<=m;i++){
int x;cin>>x;
checkpoints[i]=newidx[x];
}
for(int i=1;i<=q;i++){
int l,r;
cin >> l >> r;
vector<int> buffer;
for(int x=l;x<=r;x++)buffer.emplace_back(checkpoints[x]);
sort(buffer.begin(),buffer.end());
int lcaall = buffer[0];
for(int&x:buffer)lcaall=lca(lcaall,x);
int ans = depth[buffer[0]]-depth[lcaall]+1;
for(int x=1;x<buffer.size();x++){
ans+=depth[buffer[x]]-depth[lca(buffer[x],buffer[x-1])];
}
cout << ans << '\n';
}
}
Compilation message
tourism.cpp: In function 'int32_t main()':
tourism.cpp:94:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for(int x=1;x<buffer.size();x++){
| ~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
456 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
452 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
7 ms |
560 KB |
Output is correct |
13 |
Correct |
7 ms |
348 KB |
Output is correct |
14 |
Correct |
7 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
580 KB |
Output is correct |
18 |
Correct |
3 ms |
348 KB |
Output is correct |
19 |
Correct |
3 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
3 ms |
348 KB |
Output is correct |
24 |
Correct |
3 ms |
568 KB |
Output is correct |
25 |
Correct |
3 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
568 KB |
Output is correct |
27 |
Correct |
2 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
456 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
452 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
7 ms |
560 KB |
Output is correct |
13 |
Correct |
7 ms |
348 KB |
Output is correct |
14 |
Correct |
7 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
580 KB |
Output is correct |
18 |
Correct |
3 ms |
348 KB |
Output is correct |
19 |
Correct |
3 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
3 ms |
348 KB |
Output is correct |
24 |
Correct |
3 ms |
568 KB |
Output is correct |
25 |
Correct |
3 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
568 KB |
Output is correct |
27 |
Correct |
2 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
59 ms |
860 KB |
Output is correct |
31 |
Correct |
85 ms |
960 KB |
Output is correct |
32 |
Correct |
124 ms |
1116 KB |
Output is correct |
33 |
Correct |
120 ms |
1116 KB |
Output is correct |
34 |
Correct |
124 ms |
1116 KB |
Output is correct |
35 |
Correct |
340 ms |
1120 KB |
Output is correct |
36 |
Correct |
326 ms |
1116 KB |
Output is correct |
37 |
Correct |
313 ms |
1116 KB |
Output is correct |
38 |
Correct |
137 ms |
1116 KB |
Output is correct |
39 |
Correct |
135 ms |
1116 KB |
Output is correct |
40 |
Correct |
139 ms |
1116 KB |
Output is correct |
41 |
Correct |
395 ms |
1232 KB |
Output is correct |
42 |
Correct |
399 ms |
1368 KB |
Output is correct |
43 |
Correct |
392 ms |
1232 KB |
Output is correct |
44 |
Correct |
128 ms |
1116 KB |
Output is correct |
45 |
Correct |
131 ms |
1116 KB |
Output is correct |
46 |
Correct |
134 ms |
1180 KB |
Output is correct |
47 |
Correct |
354 ms |
1116 KB |
Output is correct |
48 |
Correct |
359 ms |
1116 KB |
Output is correct |
49 |
Correct |
351 ms |
1116 KB |
Output is correct |
50 |
Correct |
106 ms |
1144 KB |
Output is correct |
51 |
Correct |
104 ms |
1116 KB |
Output is correct |
52 |
Correct |
129 ms |
1116 KB |
Output is correct |
53 |
Correct |
106 ms |
1116 KB |
Output is correct |
54 |
Correct |
109 ms |
1132 KB |
Output is correct |
55 |
Correct |
104 ms |
1132 KB |
Output is correct |
56 |
Correct |
45 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
860 KB |
Output is correct |
58 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
44 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
5042 ms |
25444 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
38 ms |
18160 KB |
Output is correct |
3 |
Correct |
48 ms |
20040 KB |
Output is correct |
4 |
Correct |
48 ms |
22108 KB |
Output is correct |
5 |
Correct |
76 ms |
33740 KB |
Output is correct |
6 |
Correct |
70 ms |
33476 KB |
Output is correct |
7 |
Correct |
77 ms |
32984 KB |
Output is correct |
8 |
Correct |
76 ms |
32708 KB |
Output is correct |
9 |
Correct |
76 ms |
32596 KB |
Output is correct |
10 |
Correct |
78 ms |
32596 KB |
Output is correct |
11 |
Correct |
75 ms |
32596 KB |
Output is correct |
12 |
Correct |
76 ms |
32596 KB |
Output is correct |
13 |
Correct |
78 ms |
32852 KB |
Output is correct |
14 |
Correct |
89 ms |
33108 KB |
Output is correct |
15 |
Correct |
77 ms |
34132 KB |
Output is correct |
16 |
Correct |
75 ms |
33488 KB |
Output is correct |
17 |
Correct |
73 ms |
33488 KB |
Output is correct |
18 |
Correct |
73 ms |
33488 KB |
Output is correct |
19 |
Correct |
78 ms |
33236 KB |
Output is correct |
20 |
Correct |
71 ms |
32840 KB |
Output is correct |
21 |
Correct |
72 ms |
32516 KB |
Output is correct |
22 |
Correct |
70 ms |
32292 KB |
Output is correct |
23 |
Correct |
77 ms |
32084 KB |
Output is correct |
24 |
Correct |
100 ms |
32160 KB |
Output is correct |
25 |
Correct |
85 ms |
32124 KB |
Output is correct |
26 |
Correct |
95 ms |
32084 KB |
Output is correct |
27 |
Correct |
94 ms |
32084 KB |
Output is correct |
28 |
Correct |
110 ms |
32084 KB |
Output is correct |
29 |
Correct |
96 ms |
32084 KB |
Output is correct |
30 |
Correct |
96 ms |
32084 KB |
Output is correct |
31 |
Correct |
95 ms |
32084 KB |
Output is correct |
32 |
Correct |
96 ms |
32344 KB |
Output is correct |
33 |
Correct |
86 ms |
32596 KB |
Output is correct |
34 |
Correct |
70 ms |
33360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
45 ms |
536 KB |
Output is correct |
4 |
Execution timed out |
5039 ms |
20864 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
456 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
452 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
7 ms |
560 KB |
Output is correct |
13 |
Correct |
7 ms |
348 KB |
Output is correct |
14 |
Correct |
7 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
580 KB |
Output is correct |
18 |
Correct |
3 ms |
348 KB |
Output is correct |
19 |
Correct |
3 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
3 ms |
348 KB |
Output is correct |
24 |
Correct |
3 ms |
568 KB |
Output is correct |
25 |
Correct |
3 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
568 KB |
Output is correct |
27 |
Correct |
2 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
59 ms |
860 KB |
Output is correct |
31 |
Correct |
85 ms |
960 KB |
Output is correct |
32 |
Correct |
124 ms |
1116 KB |
Output is correct |
33 |
Correct |
120 ms |
1116 KB |
Output is correct |
34 |
Correct |
124 ms |
1116 KB |
Output is correct |
35 |
Correct |
340 ms |
1120 KB |
Output is correct |
36 |
Correct |
326 ms |
1116 KB |
Output is correct |
37 |
Correct |
313 ms |
1116 KB |
Output is correct |
38 |
Correct |
137 ms |
1116 KB |
Output is correct |
39 |
Correct |
135 ms |
1116 KB |
Output is correct |
40 |
Correct |
139 ms |
1116 KB |
Output is correct |
41 |
Correct |
395 ms |
1232 KB |
Output is correct |
42 |
Correct |
399 ms |
1368 KB |
Output is correct |
43 |
Correct |
392 ms |
1232 KB |
Output is correct |
44 |
Correct |
128 ms |
1116 KB |
Output is correct |
45 |
Correct |
131 ms |
1116 KB |
Output is correct |
46 |
Correct |
134 ms |
1180 KB |
Output is correct |
47 |
Correct |
354 ms |
1116 KB |
Output is correct |
48 |
Correct |
359 ms |
1116 KB |
Output is correct |
49 |
Correct |
351 ms |
1116 KB |
Output is correct |
50 |
Correct |
106 ms |
1144 KB |
Output is correct |
51 |
Correct |
104 ms |
1116 KB |
Output is correct |
52 |
Correct |
129 ms |
1116 KB |
Output is correct |
53 |
Correct |
106 ms |
1116 KB |
Output is correct |
54 |
Correct |
109 ms |
1132 KB |
Output is correct |
55 |
Correct |
104 ms |
1132 KB |
Output is correct |
56 |
Correct |
45 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
860 KB |
Output is correct |
58 |
Correct |
2 ms |
860 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
2 ms |
348 KB |
Output is correct |
61 |
Correct |
44 ms |
348 KB |
Output is correct |
62 |
Execution timed out |
5042 ms |
25444 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |