#include<bits/stdc++.h>
#include<queue>
using namespace std;
const int N=1e5+5;
const int M=3e5+5;
const int LG=20;
int n,m,q;
int c[N];
vector<int> adj[N];
int sz[N],hv[N],hd[N],pa[N],lv[N],tin[N],tout[N],pos[M];
int timer;
vector<pair<int,int>> qr[N],chain[N];
int ans[N];
struct Fenwick{
int t[N];
void update(int i,int v){
for(;i<N;i+=i&-i)t[i]+=v;
}
int query(int i){
int res=0;
for(;i;i-=i&-i)res+=t[i];
return res;
}
}f;
template<class T,class cmp>
struct RMQ{
T t[LG][M];
T get_min(T a,T b){
return cmp()(a,b)?a:b;
}
void build(){
for(int i=0;i<LG-1;i++){
for(int j=1;j+(2<<i)-1<M;j++){
t[i+1][j]=get_min(t[i][j],t[i][j+(1<<i)]);
}
}
}
T query(int l,int r){
int k=31-__builtin_clz(r-l+1);
return get_min(t[k][l],t[k][r-(1<<k)+1]);
}
};
RMQ<int,less<int>> rtin,rdep;
RMQ<int,greater<int>> rtout;
void dfs(int u,int p=0){
tin[u]=++timer;
pos[timer]=u;
pa[u]=p;
sz[u]=1;
for(auto v:adj[u])if(v!=p){
lv[v]=lv[u]+1;
dfs(v,u);
sz[u]+=sz[v];
if(sz[v]>sz[hv[u]])hv[u]=v;
pos[++timer]=u;
}
tout[u]=timer;
}
void hld(int u,int p=0){
if(!hd[u])hd[u]=u;
if(hv[u])hd[hv[u]]=hd[u],hld(hv[u],u);
for(auto v:adj[u])if(v!=p&&v!=hv[u])hld(v,u);
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m >> q;
for(int i=1;i<n;i++){
int u,v;
cin >> u >> v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
for(int i=1;i<=m;i++)cin >> c[i];
for(int i=1;i<=q;i++){
int l,r;
cin >> l >> r;
qr[r].emplace_back(l,i);
}
dfs(1);
hld(1);
for(int i=1;i<=timer;i++)rdep.t[0][i]=lv[pos[i]];
for(int i=1;i<=m;i++){
rtin.t[0][i]=tin[c[i]];
rtout.t[0][i]=tout[c[i]];
}
rdep.build();
rtin.build();
rtout.build();
for(int r=1;r<=m;r++){
for(int u=c[r];u;u=pa[u]){
int h=hd[u];
auto &ch=chain[h];
while(!ch.empty()&&lv[ch.back().first]<=lv[u]){
auto [v,i]=ch.back();
ch.pop_back();
int val=lv[v]-lv[h]+1;
f.update(i,-val);
if(!ch.empty())f.update(ch.back().second,val);
}
int val=lv[u]-lv[h]+1;
if(!ch.empty())f.update(ch.back().second,-val);
ch.emplace_back(u,r);
f.update(r,val);
}
for(auto [l,i]:qr[r]){
int u=rtin.query(l,r),v=rtout.query(l,r);
ans[i]=f.query(r)-f.query(l-1)-rdep.query(u,v);
}
}
for(int i=1;i<=q;i++)cout << ans[i] << "\n";
}
Compilation message
tourism.cpp: In function 'int main()':
tourism.cpp:103:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
103 | auto [v,i]=ch.back();
| ^
tourism.cpp:114:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
114 | for(auto [l,i]:qr[r]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
82516 KB |
Output is correct |
2 |
Correct |
29 ms |
82432 KB |
Output is correct |
3 |
Correct |
25 ms |
82524 KB |
Output is correct |
4 |
Correct |
23 ms |
82512 KB |
Output is correct |
5 |
Correct |
23 ms |
82524 KB |
Output is correct |
6 |
Correct |
24 ms |
82520 KB |
Output is correct |
7 |
Correct |
23 ms |
82496 KB |
Output is correct |
8 |
Correct |
25 ms |
82480 KB |
Output is correct |
9 |
Correct |
22 ms |
82780 KB |
Output is correct |
10 |
Correct |
23 ms |
82516 KB |
Output is correct |
11 |
Correct |
22 ms |
82524 KB |
Output is correct |
12 |
Correct |
22 ms |
82512 KB |
Output is correct |
13 |
Correct |
22 ms |
82488 KB |
Output is correct |
14 |
Correct |
24 ms |
82532 KB |
Output is correct |
15 |
Correct |
26 ms |
82540 KB |
Output is correct |
16 |
Correct |
27 ms |
82736 KB |
Output is correct |
17 |
Correct |
24 ms |
82576 KB |
Output is correct |
18 |
Correct |
23 ms |
82524 KB |
Output is correct |
19 |
Correct |
25 ms |
82524 KB |
Output is correct |
20 |
Correct |
23 ms |
82524 KB |
Output is correct |
21 |
Correct |
22 ms |
82536 KB |
Output is correct |
22 |
Correct |
22 ms |
82512 KB |
Output is correct |
23 |
Correct |
22 ms |
82524 KB |
Output is correct |
24 |
Correct |
22 ms |
82524 KB |
Output is correct |
25 |
Correct |
23 ms |
82532 KB |
Output is correct |
26 |
Correct |
23 ms |
82540 KB |
Output is correct |
27 |
Correct |
22 ms |
82524 KB |
Output is correct |
28 |
Correct |
22 ms |
82524 KB |
Output is correct |
29 |
Correct |
22 ms |
82524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
82516 KB |
Output is correct |
2 |
Correct |
29 ms |
82432 KB |
Output is correct |
3 |
Correct |
25 ms |
82524 KB |
Output is correct |
4 |
Correct |
23 ms |
82512 KB |
Output is correct |
5 |
Correct |
23 ms |
82524 KB |
Output is correct |
6 |
Correct |
24 ms |
82520 KB |
Output is correct |
7 |
Correct |
23 ms |
82496 KB |
Output is correct |
8 |
Correct |
25 ms |
82480 KB |
Output is correct |
9 |
Correct |
22 ms |
82780 KB |
Output is correct |
10 |
Correct |
23 ms |
82516 KB |
Output is correct |
11 |
Correct |
22 ms |
82524 KB |
Output is correct |
12 |
Correct |
22 ms |
82512 KB |
Output is correct |
13 |
Correct |
22 ms |
82488 KB |
Output is correct |
14 |
Correct |
24 ms |
82532 KB |
Output is correct |
15 |
Correct |
26 ms |
82540 KB |
Output is correct |
16 |
Correct |
27 ms |
82736 KB |
Output is correct |
17 |
Correct |
24 ms |
82576 KB |
Output is correct |
18 |
Correct |
23 ms |
82524 KB |
Output is correct |
19 |
Correct |
25 ms |
82524 KB |
Output is correct |
20 |
Correct |
23 ms |
82524 KB |
Output is correct |
21 |
Correct |
22 ms |
82536 KB |
Output is correct |
22 |
Correct |
22 ms |
82512 KB |
Output is correct |
23 |
Correct |
22 ms |
82524 KB |
Output is correct |
24 |
Correct |
22 ms |
82524 KB |
Output is correct |
25 |
Correct |
23 ms |
82532 KB |
Output is correct |
26 |
Correct |
23 ms |
82540 KB |
Output is correct |
27 |
Correct |
22 ms |
82524 KB |
Output is correct |
28 |
Correct |
22 ms |
82524 KB |
Output is correct |
29 |
Correct |
22 ms |
82524 KB |
Output is correct |
30 |
Correct |
25 ms |
82520 KB |
Output is correct |
31 |
Correct |
24 ms |
82512 KB |
Output is correct |
32 |
Correct |
25 ms |
82512 KB |
Output is correct |
33 |
Correct |
25 ms |
82772 KB |
Output is correct |
34 |
Correct |
25 ms |
82524 KB |
Output is correct |
35 |
Correct |
25 ms |
82712 KB |
Output is correct |
36 |
Correct |
25 ms |
82532 KB |
Output is correct |
37 |
Correct |
25 ms |
82620 KB |
Output is correct |
38 |
Correct |
73 ms |
82780 KB |
Output is correct |
39 |
Correct |
81 ms |
82836 KB |
Output is correct |
40 |
Correct |
81 ms |
82780 KB |
Output is correct |
41 |
Correct |
95 ms |
82852 KB |
Output is correct |
42 |
Correct |
100 ms |
83032 KB |
Output is correct |
43 |
Correct |
69 ms |
82816 KB |
Output is correct |
44 |
Correct |
52 ms |
82780 KB |
Output is correct |
45 |
Correct |
38 ms |
82512 KB |
Output is correct |
46 |
Correct |
62 ms |
82780 KB |
Output is correct |
47 |
Correct |
49 ms |
82664 KB |
Output is correct |
48 |
Correct |
41 ms |
82524 KB |
Output is correct |
49 |
Correct |
47 ms |
82516 KB |
Output is correct |
50 |
Correct |
23 ms |
82524 KB |
Output is correct |
51 |
Correct |
23 ms |
82520 KB |
Output is correct |
52 |
Correct |
23 ms |
82692 KB |
Output is correct |
53 |
Correct |
24 ms |
82564 KB |
Output is correct |
54 |
Correct |
23 ms |
82512 KB |
Output is correct |
55 |
Correct |
24 ms |
82524 KB |
Output is correct |
56 |
Correct |
22 ms |
82524 KB |
Output is correct |
57 |
Correct |
23 ms |
82524 KB |
Output is correct |
58 |
Correct |
24 ms |
82524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
82520 KB |
Output is correct |
2 |
Correct |
22 ms |
82524 KB |
Output is correct |
3 |
Correct |
23 ms |
82516 KB |
Output is correct |
4 |
Execution timed out |
5055 ms |
93400 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
82528 KB |
Output is correct |
2 |
Correct |
104 ms |
86096 KB |
Output is correct |
3 |
Correct |
134 ms |
87152 KB |
Output is correct |
4 |
Correct |
121 ms |
87288 KB |
Output is correct |
5 |
Correct |
169 ms |
89452 KB |
Output is correct |
6 |
Correct |
186 ms |
89424 KB |
Output is correct |
7 |
Correct |
170 ms |
89256 KB |
Output is correct |
8 |
Correct |
190 ms |
89412 KB |
Output is correct |
9 |
Correct |
168 ms |
89404 KB |
Output is correct |
10 |
Correct |
176 ms |
89308 KB |
Output is correct |
11 |
Correct |
175 ms |
89452 KB |
Output is correct |
12 |
Correct |
184 ms |
89428 KB |
Output is correct |
13 |
Correct |
174 ms |
89936 KB |
Output is correct |
14 |
Correct |
185 ms |
90704 KB |
Output is correct |
15 |
Correct |
217 ms |
93864 KB |
Output is correct |
16 |
Correct |
186 ms |
89940 KB |
Output is correct |
17 |
Correct |
184 ms |
89824 KB |
Output is correct |
18 |
Correct |
177 ms |
89932 KB |
Output is correct |
19 |
Correct |
4340 ms |
89280 KB |
Output is correct |
20 |
Correct |
4555 ms |
89168 KB |
Output is correct |
21 |
Correct |
3041 ms |
89412 KB |
Output is correct |
22 |
Correct |
4162 ms |
89436 KB |
Output is correct |
23 |
Correct |
3860 ms |
89184 KB |
Output is correct |
24 |
Correct |
3442 ms |
89192 KB |
Output is correct |
25 |
Correct |
3431 ms |
89664 KB |
Output is correct |
26 |
Execution timed out |
5034 ms |
89468 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
82512 KB |
Output is correct |
2 |
Correct |
22 ms |
82524 KB |
Output is correct |
3 |
Correct |
24 ms |
82504 KB |
Output is correct |
4 |
Correct |
168 ms |
88288 KB |
Output is correct |
5 |
Correct |
167 ms |
90124 KB |
Output is correct |
6 |
Correct |
187 ms |
91988 KB |
Output is correct |
7 |
Correct |
206 ms |
93008 KB |
Output is correct |
8 |
Correct |
214 ms |
93004 KB |
Output is correct |
9 |
Correct |
213 ms |
92864 KB |
Output is correct |
10 |
Correct |
219 ms |
92976 KB |
Output is correct |
11 |
Correct |
203 ms |
93112 KB |
Output is correct |
12 |
Correct |
219 ms |
93116 KB |
Output is correct |
13 |
Correct |
215 ms |
93004 KB |
Output is correct |
14 |
Correct |
58 ms |
86356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
82516 KB |
Output is correct |
2 |
Correct |
29 ms |
82432 KB |
Output is correct |
3 |
Correct |
25 ms |
82524 KB |
Output is correct |
4 |
Correct |
23 ms |
82512 KB |
Output is correct |
5 |
Correct |
23 ms |
82524 KB |
Output is correct |
6 |
Correct |
24 ms |
82520 KB |
Output is correct |
7 |
Correct |
23 ms |
82496 KB |
Output is correct |
8 |
Correct |
25 ms |
82480 KB |
Output is correct |
9 |
Correct |
22 ms |
82780 KB |
Output is correct |
10 |
Correct |
23 ms |
82516 KB |
Output is correct |
11 |
Correct |
22 ms |
82524 KB |
Output is correct |
12 |
Correct |
22 ms |
82512 KB |
Output is correct |
13 |
Correct |
22 ms |
82488 KB |
Output is correct |
14 |
Correct |
24 ms |
82532 KB |
Output is correct |
15 |
Correct |
26 ms |
82540 KB |
Output is correct |
16 |
Correct |
27 ms |
82736 KB |
Output is correct |
17 |
Correct |
24 ms |
82576 KB |
Output is correct |
18 |
Correct |
23 ms |
82524 KB |
Output is correct |
19 |
Correct |
25 ms |
82524 KB |
Output is correct |
20 |
Correct |
23 ms |
82524 KB |
Output is correct |
21 |
Correct |
22 ms |
82536 KB |
Output is correct |
22 |
Correct |
22 ms |
82512 KB |
Output is correct |
23 |
Correct |
22 ms |
82524 KB |
Output is correct |
24 |
Correct |
22 ms |
82524 KB |
Output is correct |
25 |
Correct |
23 ms |
82532 KB |
Output is correct |
26 |
Correct |
23 ms |
82540 KB |
Output is correct |
27 |
Correct |
22 ms |
82524 KB |
Output is correct |
28 |
Correct |
22 ms |
82524 KB |
Output is correct |
29 |
Correct |
22 ms |
82524 KB |
Output is correct |
30 |
Correct |
25 ms |
82520 KB |
Output is correct |
31 |
Correct |
24 ms |
82512 KB |
Output is correct |
32 |
Correct |
25 ms |
82512 KB |
Output is correct |
33 |
Correct |
25 ms |
82772 KB |
Output is correct |
34 |
Correct |
25 ms |
82524 KB |
Output is correct |
35 |
Correct |
25 ms |
82712 KB |
Output is correct |
36 |
Correct |
25 ms |
82532 KB |
Output is correct |
37 |
Correct |
25 ms |
82620 KB |
Output is correct |
38 |
Correct |
73 ms |
82780 KB |
Output is correct |
39 |
Correct |
81 ms |
82836 KB |
Output is correct |
40 |
Correct |
81 ms |
82780 KB |
Output is correct |
41 |
Correct |
95 ms |
82852 KB |
Output is correct |
42 |
Correct |
100 ms |
83032 KB |
Output is correct |
43 |
Correct |
69 ms |
82816 KB |
Output is correct |
44 |
Correct |
52 ms |
82780 KB |
Output is correct |
45 |
Correct |
38 ms |
82512 KB |
Output is correct |
46 |
Correct |
62 ms |
82780 KB |
Output is correct |
47 |
Correct |
49 ms |
82664 KB |
Output is correct |
48 |
Correct |
41 ms |
82524 KB |
Output is correct |
49 |
Correct |
47 ms |
82516 KB |
Output is correct |
50 |
Correct |
23 ms |
82524 KB |
Output is correct |
51 |
Correct |
23 ms |
82520 KB |
Output is correct |
52 |
Correct |
23 ms |
82692 KB |
Output is correct |
53 |
Correct |
24 ms |
82564 KB |
Output is correct |
54 |
Correct |
23 ms |
82512 KB |
Output is correct |
55 |
Correct |
24 ms |
82524 KB |
Output is correct |
56 |
Correct |
22 ms |
82524 KB |
Output is correct |
57 |
Correct |
23 ms |
82524 KB |
Output is correct |
58 |
Correct |
24 ms |
82524 KB |
Output is correct |
59 |
Correct |
23 ms |
82520 KB |
Output is correct |
60 |
Correct |
22 ms |
82524 KB |
Output is correct |
61 |
Correct |
23 ms |
82516 KB |
Output is correct |
62 |
Execution timed out |
5055 ms |
93400 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |