#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[N];
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 |
25 ms |
81424 KB |
Output is correct |
2 |
Correct |
22 ms |
81496 KB |
Output is correct |
3 |
Correct |
22 ms |
81668 KB |
Output is correct |
4 |
Correct |
22 ms |
81500 KB |
Output is correct |
5 |
Correct |
22 ms |
81556 KB |
Output is correct |
6 |
Correct |
24 ms |
81916 KB |
Output is correct |
7 |
Correct |
22 ms |
81496 KB |
Output is correct |
8 |
Correct |
22 ms |
81500 KB |
Output is correct |
9 |
Correct |
23 ms |
81500 KB |
Output is correct |
10 |
Correct |
23 ms |
81500 KB |
Output is correct |
11 |
Correct |
23 ms |
81668 KB |
Output is correct |
12 |
Correct |
22 ms |
81952 KB |
Output is correct |
13 |
Correct |
22 ms |
81496 KB |
Output is correct |
14 |
Correct |
24 ms |
81544 KB |
Output is correct |
15 |
Correct |
23 ms |
81636 KB |
Output is correct |
16 |
Correct |
24 ms |
81500 KB |
Output is correct |
17 |
Correct |
24 ms |
81492 KB |
Output is correct |
18 |
Correct |
23 ms |
81668 KB |
Output is correct |
19 |
Correct |
23 ms |
81496 KB |
Output is correct |
20 |
Correct |
23 ms |
81700 KB |
Output is correct |
21 |
Correct |
22 ms |
81492 KB |
Output is correct |
22 |
Correct |
24 ms |
81756 KB |
Output is correct |
23 |
Correct |
22 ms |
81672 KB |
Output is correct |
24 |
Correct |
22 ms |
81700 KB |
Output is correct |
25 |
Correct |
22 ms |
81492 KB |
Output is correct |
26 |
Correct |
22 ms |
81500 KB |
Output is correct |
27 |
Correct |
24 ms |
81496 KB |
Output is correct |
28 |
Correct |
22 ms |
81496 KB |
Output is correct |
29 |
Correct |
22 ms |
81496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
81424 KB |
Output is correct |
2 |
Correct |
22 ms |
81496 KB |
Output is correct |
3 |
Correct |
22 ms |
81668 KB |
Output is correct |
4 |
Correct |
22 ms |
81500 KB |
Output is correct |
5 |
Correct |
22 ms |
81556 KB |
Output is correct |
6 |
Correct |
24 ms |
81916 KB |
Output is correct |
7 |
Correct |
22 ms |
81496 KB |
Output is correct |
8 |
Correct |
22 ms |
81500 KB |
Output is correct |
9 |
Correct |
23 ms |
81500 KB |
Output is correct |
10 |
Correct |
23 ms |
81500 KB |
Output is correct |
11 |
Correct |
23 ms |
81668 KB |
Output is correct |
12 |
Correct |
22 ms |
81952 KB |
Output is correct |
13 |
Correct |
22 ms |
81496 KB |
Output is correct |
14 |
Correct |
24 ms |
81544 KB |
Output is correct |
15 |
Correct |
23 ms |
81636 KB |
Output is correct |
16 |
Correct |
24 ms |
81500 KB |
Output is correct |
17 |
Correct |
24 ms |
81492 KB |
Output is correct |
18 |
Correct |
23 ms |
81668 KB |
Output is correct |
19 |
Correct |
23 ms |
81496 KB |
Output is correct |
20 |
Correct |
23 ms |
81700 KB |
Output is correct |
21 |
Correct |
22 ms |
81492 KB |
Output is correct |
22 |
Correct |
24 ms |
81756 KB |
Output is correct |
23 |
Correct |
22 ms |
81672 KB |
Output is correct |
24 |
Correct |
22 ms |
81700 KB |
Output is correct |
25 |
Correct |
22 ms |
81492 KB |
Output is correct |
26 |
Correct |
22 ms |
81500 KB |
Output is correct |
27 |
Correct |
24 ms |
81496 KB |
Output is correct |
28 |
Correct |
22 ms |
81496 KB |
Output is correct |
29 |
Correct |
22 ms |
81496 KB |
Output is correct |
30 |
Correct |
23 ms |
81796 KB |
Output is correct |
31 |
Correct |
24 ms |
81756 KB |
Output is correct |
32 |
Correct |
25 ms |
81744 KB |
Output is correct |
33 |
Correct |
25 ms |
81824 KB |
Output is correct |
34 |
Correct |
25 ms |
81748 KB |
Output is correct |
35 |
Correct |
27 ms |
81748 KB |
Output is correct |
36 |
Correct |
24 ms |
81772 KB |
Output is correct |
37 |
Correct |
25 ms |
81748 KB |
Output is correct |
38 |
Correct |
78 ms |
82196 KB |
Output is correct |
39 |
Correct |
76 ms |
82008 KB |
Output is correct |
40 |
Correct |
82 ms |
82020 KB |
Output is correct |
41 |
Correct |
94 ms |
81812 KB |
Output is correct |
42 |
Correct |
106 ms |
82000 KB |
Output is correct |
43 |
Correct |
68 ms |
81744 KB |
Output is correct |
44 |
Correct |
53 ms |
81880 KB |
Output is correct |
45 |
Correct |
38 ms |
81808 KB |
Output is correct |
46 |
Correct |
64 ms |
81756 KB |
Output is correct |
47 |
Correct |
52 ms |
81924 KB |
Output is correct |
48 |
Correct |
41 ms |
81784 KB |
Output is correct |
49 |
Correct |
49 ms |
81912 KB |
Output is correct |
50 |
Correct |
23 ms |
81824 KB |
Output is correct |
51 |
Correct |
23 ms |
81744 KB |
Output is correct |
52 |
Correct |
24 ms |
81812 KB |
Output is correct |
53 |
Correct |
24 ms |
81760 KB |
Output is correct |
54 |
Correct |
24 ms |
81828 KB |
Output is correct |
55 |
Correct |
24 ms |
82008 KB |
Output is correct |
56 |
Correct |
27 ms |
81688 KB |
Output is correct |
57 |
Correct |
23 ms |
81748 KB |
Output is correct |
58 |
Correct |
25 ms |
81824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
81500 KB |
Output is correct |
2 |
Correct |
22 ms |
81500 KB |
Output is correct |
3 |
Correct |
24 ms |
81732 KB |
Output is correct |
4 |
Execution timed out |
5020 ms |
93416 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
81500 KB |
Output is correct |
2 |
Incorrect |
103 ms |
84656 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
81488 KB |
Output is correct |
2 |
Correct |
22 ms |
81500 KB |
Output is correct |
3 |
Correct |
23 ms |
81756 KB |
Output is correct |
4 |
Runtime error |
173 ms |
177412 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
81424 KB |
Output is correct |
2 |
Correct |
22 ms |
81496 KB |
Output is correct |
3 |
Correct |
22 ms |
81668 KB |
Output is correct |
4 |
Correct |
22 ms |
81500 KB |
Output is correct |
5 |
Correct |
22 ms |
81556 KB |
Output is correct |
6 |
Correct |
24 ms |
81916 KB |
Output is correct |
7 |
Correct |
22 ms |
81496 KB |
Output is correct |
8 |
Correct |
22 ms |
81500 KB |
Output is correct |
9 |
Correct |
23 ms |
81500 KB |
Output is correct |
10 |
Correct |
23 ms |
81500 KB |
Output is correct |
11 |
Correct |
23 ms |
81668 KB |
Output is correct |
12 |
Correct |
22 ms |
81952 KB |
Output is correct |
13 |
Correct |
22 ms |
81496 KB |
Output is correct |
14 |
Correct |
24 ms |
81544 KB |
Output is correct |
15 |
Correct |
23 ms |
81636 KB |
Output is correct |
16 |
Correct |
24 ms |
81500 KB |
Output is correct |
17 |
Correct |
24 ms |
81492 KB |
Output is correct |
18 |
Correct |
23 ms |
81668 KB |
Output is correct |
19 |
Correct |
23 ms |
81496 KB |
Output is correct |
20 |
Correct |
23 ms |
81700 KB |
Output is correct |
21 |
Correct |
22 ms |
81492 KB |
Output is correct |
22 |
Correct |
24 ms |
81756 KB |
Output is correct |
23 |
Correct |
22 ms |
81672 KB |
Output is correct |
24 |
Correct |
22 ms |
81700 KB |
Output is correct |
25 |
Correct |
22 ms |
81492 KB |
Output is correct |
26 |
Correct |
22 ms |
81500 KB |
Output is correct |
27 |
Correct |
24 ms |
81496 KB |
Output is correct |
28 |
Correct |
22 ms |
81496 KB |
Output is correct |
29 |
Correct |
22 ms |
81496 KB |
Output is correct |
30 |
Correct |
23 ms |
81796 KB |
Output is correct |
31 |
Correct |
24 ms |
81756 KB |
Output is correct |
32 |
Correct |
25 ms |
81744 KB |
Output is correct |
33 |
Correct |
25 ms |
81824 KB |
Output is correct |
34 |
Correct |
25 ms |
81748 KB |
Output is correct |
35 |
Correct |
27 ms |
81748 KB |
Output is correct |
36 |
Correct |
24 ms |
81772 KB |
Output is correct |
37 |
Correct |
25 ms |
81748 KB |
Output is correct |
38 |
Correct |
78 ms |
82196 KB |
Output is correct |
39 |
Correct |
76 ms |
82008 KB |
Output is correct |
40 |
Correct |
82 ms |
82020 KB |
Output is correct |
41 |
Correct |
94 ms |
81812 KB |
Output is correct |
42 |
Correct |
106 ms |
82000 KB |
Output is correct |
43 |
Correct |
68 ms |
81744 KB |
Output is correct |
44 |
Correct |
53 ms |
81880 KB |
Output is correct |
45 |
Correct |
38 ms |
81808 KB |
Output is correct |
46 |
Correct |
64 ms |
81756 KB |
Output is correct |
47 |
Correct |
52 ms |
81924 KB |
Output is correct |
48 |
Correct |
41 ms |
81784 KB |
Output is correct |
49 |
Correct |
49 ms |
81912 KB |
Output is correct |
50 |
Correct |
23 ms |
81824 KB |
Output is correct |
51 |
Correct |
23 ms |
81744 KB |
Output is correct |
52 |
Correct |
24 ms |
81812 KB |
Output is correct |
53 |
Correct |
24 ms |
81760 KB |
Output is correct |
54 |
Correct |
24 ms |
81828 KB |
Output is correct |
55 |
Correct |
24 ms |
82008 KB |
Output is correct |
56 |
Correct |
27 ms |
81688 KB |
Output is correct |
57 |
Correct |
23 ms |
81748 KB |
Output is correct |
58 |
Correct |
25 ms |
81824 KB |
Output is correct |
59 |
Correct |
22 ms |
81500 KB |
Output is correct |
60 |
Correct |
22 ms |
81500 KB |
Output is correct |
61 |
Correct |
24 ms |
81732 KB |
Output is correct |
62 |
Execution timed out |
5020 ms |
93416 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |