#include <bits/stdc++.h>
#define int long long
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
using namespace std;
const int N=1e5+5;
int mn[N*4],mx[N*4],c[N];
void build(int v,int tl,int tr){
if(tl==tr){
mn[v]=c[tl];
mx[v]=c[tl];
}
else{
int tm=(tl+tr)/2;
build(v*2,tl,tm);
build(v*2+1,tm+1,tr);
mn[v]=min(mn[v*2],mn[v*2+1]);
mx[v]=max(mx[v*2],mx[v*2+1]);
}
}
int get_mn(int v,int tl,int tr,int l,int r){
if(r<tl or l>tr)return INT_MAX;
if(l<=tl && tr<=r)return mn[v];
int tm=(tl+tr)/2;
return min(get_mn(v*2,tl,tm,l,r),get_mn(v*2+1,tm+1,tr,l,r));
}
int get_mx(int v,int tl,int tr,int l,int r){
if(r<tl or l>tr)return INT_MIN;
if(l<=tl && tr<=r)return mx[v];
int tm=(tl+tr)/2;
return max(get_mx(v*2,tl,tm,l,r),get_mx(v*2+1,tm+1,tr,l,r));
}
signed main(){
ios_base::sync_with_stdio();
cin.tie(0);
int n,m,qq;
cin>>n>>m>>qq;
vector <int> g[n+1];
int cnt=0;
for(int i=0;i<n-1;i++){
int u,v;
cin>>u>>v;
if(u==v+1)cnt++;
g[u].pb(v);
g[v].pb(u);
}
for(int i=1;i<=m;i++){
cin>>c[i];
}
if(cnt==n-1){
build(1,1,m);
while(qq--){
int l,r;
cin>>l>>r;
cout<<get_mx(1,1,m,l,r)-get_mn(1,1,m,l,r)+1<<"\n";
}
return 0;
}
queue <int> q;
vector <vector <int> > d(n+1,vector <int> (n+1,-1));
vector <vector <int> > p(n+1,vector <int> (n+1));
for(int i=1;i<=n;i++){
q.push(i);
d[i][i]=0;
while(!q.empty()){
int v=q.front();
q.pop();
for(auto to : g[v]){
if(d[i][to]==-1){
d[i][to]=d[i][v]+1;
p[i][to]=v;
q.push(to);
}
}
}
}
while(qq--){
int l,r;
cin>>l>>r;
set <int> st;
if(l==r)st.insert(l);
for(int i=l;i<r;i++){
int x=c[i+1];
st.insert(x);st.insert(c[i]);
while(x!=c[i]){
x=p[c[i]][x];
st.insert(x);
}
}
cout<<st.size()<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
600 KB |
Output is correct |
4 |
Correct |
7 ms |
900 KB |
Output is correct |
5 |
Correct |
7 ms |
1116 KB |
Output is correct |
6 |
Correct |
6 ms |
860 KB |
Output is correct |
7 |
Correct |
7 ms |
1048 KB |
Output is correct |
8 |
Correct |
6 ms |
1628 KB |
Output is correct |
9 |
Correct |
14 ms |
1916 KB |
Output is correct |
10 |
Correct |
14 ms |
1884 KB |
Output is correct |
11 |
Correct |
14 ms |
1884 KB |
Output is correct |
12 |
Correct |
36 ms |
1880 KB |
Output is correct |
13 |
Correct |
33 ms |
1884 KB |
Output is correct |
14 |
Correct |
35 ms |
2136 KB |
Output is correct |
15 |
Correct |
67 ms |
1884 KB |
Output is correct |
16 |
Correct |
67 ms |
1880 KB |
Output is correct |
17 |
Correct |
72 ms |
1880 KB |
Output is correct |
18 |
Correct |
52 ms |
2136 KB |
Output is correct |
19 |
Correct |
56 ms |
1884 KB |
Output is correct |
20 |
Correct |
63 ms |
1880 KB |
Output is correct |
21 |
Correct |
6 ms |
1884 KB |
Output is correct |
22 |
Correct |
6 ms |
1884 KB |
Output is correct |
23 |
Correct |
6 ms |
1884 KB |
Output is correct |
24 |
Correct |
6 ms |
1884 KB |
Output is correct |
25 |
Correct |
6 ms |
1884 KB |
Output is correct |
26 |
Correct |
6 ms |
1784 KB |
Output is correct |
27 |
Correct |
1 ms |
4640 KB |
Output is correct |
28 |
Correct |
2 ms |
1884 KB |
Output is correct |
29 |
Correct |
2 ms |
1880 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 |
600 KB |
Output is correct |
4 |
Correct |
7 ms |
900 KB |
Output is correct |
5 |
Correct |
7 ms |
1116 KB |
Output is correct |
6 |
Correct |
6 ms |
860 KB |
Output is correct |
7 |
Correct |
7 ms |
1048 KB |
Output is correct |
8 |
Correct |
6 ms |
1628 KB |
Output is correct |
9 |
Correct |
14 ms |
1916 KB |
Output is correct |
10 |
Correct |
14 ms |
1884 KB |
Output is correct |
11 |
Correct |
14 ms |
1884 KB |
Output is correct |
12 |
Correct |
36 ms |
1880 KB |
Output is correct |
13 |
Correct |
33 ms |
1884 KB |
Output is correct |
14 |
Correct |
35 ms |
2136 KB |
Output is correct |
15 |
Correct |
67 ms |
1884 KB |
Output is correct |
16 |
Correct |
67 ms |
1880 KB |
Output is correct |
17 |
Correct |
72 ms |
1880 KB |
Output is correct |
18 |
Correct |
52 ms |
2136 KB |
Output is correct |
19 |
Correct |
56 ms |
1884 KB |
Output is correct |
20 |
Correct |
63 ms |
1880 KB |
Output is correct |
21 |
Correct |
6 ms |
1884 KB |
Output is correct |
22 |
Correct |
6 ms |
1884 KB |
Output is correct |
23 |
Correct |
6 ms |
1884 KB |
Output is correct |
24 |
Correct |
6 ms |
1884 KB |
Output is correct |
25 |
Correct |
6 ms |
1884 KB |
Output is correct |
26 |
Correct |
6 ms |
1784 KB |
Output is correct |
27 |
Correct |
1 ms |
4640 KB |
Output is correct |
28 |
Correct |
2 ms |
1884 KB |
Output is correct |
29 |
Correct |
2 ms |
1880 KB |
Output is correct |
30 |
Correct |
499 ms |
34704 KB |
Output is correct |
31 |
Correct |
647 ms |
32860 KB |
Output is correct |
32 |
Correct |
1091 ms |
63696 KB |
Output is correct |
33 |
Correct |
1126 ms |
63456 KB |
Output is correct |
34 |
Correct |
1079 ms |
63444 KB |
Output is correct |
35 |
Correct |
2907 ms |
63448 KB |
Output is correct |
36 |
Correct |
3073 ms |
63444 KB |
Output is correct |
37 |
Correct |
3079 ms |
63444 KB |
Output is correct |
38 |
Execution timed out |
5008 ms |
63576 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Runtime error |
584 ms |
1048576 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
427 ms |
1048576 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Runtime error |
408 ms |
1048576 KB |
Execution killed with signal 9 |
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 |
600 KB |
Output is correct |
4 |
Correct |
7 ms |
900 KB |
Output is correct |
5 |
Correct |
7 ms |
1116 KB |
Output is correct |
6 |
Correct |
6 ms |
860 KB |
Output is correct |
7 |
Correct |
7 ms |
1048 KB |
Output is correct |
8 |
Correct |
6 ms |
1628 KB |
Output is correct |
9 |
Correct |
14 ms |
1916 KB |
Output is correct |
10 |
Correct |
14 ms |
1884 KB |
Output is correct |
11 |
Correct |
14 ms |
1884 KB |
Output is correct |
12 |
Correct |
36 ms |
1880 KB |
Output is correct |
13 |
Correct |
33 ms |
1884 KB |
Output is correct |
14 |
Correct |
35 ms |
2136 KB |
Output is correct |
15 |
Correct |
67 ms |
1884 KB |
Output is correct |
16 |
Correct |
67 ms |
1880 KB |
Output is correct |
17 |
Correct |
72 ms |
1880 KB |
Output is correct |
18 |
Correct |
52 ms |
2136 KB |
Output is correct |
19 |
Correct |
56 ms |
1884 KB |
Output is correct |
20 |
Correct |
63 ms |
1880 KB |
Output is correct |
21 |
Correct |
6 ms |
1884 KB |
Output is correct |
22 |
Correct |
6 ms |
1884 KB |
Output is correct |
23 |
Correct |
6 ms |
1884 KB |
Output is correct |
24 |
Correct |
6 ms |
1884 KB |
Output is correct |
25 |
Correct |
6 ms |
1884 KB |
Output is correct |
26 |
Correct |
6 ms |
1784 KB |
Output is correct |
27 |
Correct |
1 ms |
4640 KB |
Output is correct |
28 |
Correct |
2 ms |
1884 KB |
Output is correct |
29 |
Correct |
2 ms |
1880 KB |
Output is correct |
30 |
Correct |
499 ms |
34704 KB |
Output is correct |
31 |
Correct |
647 ms |
32860 KB |
Output is correct |
32 |
Correct |
1091 ms |
63696 KB |
Output is correct |
33 |
Correct |
1126 ms |
63456 KB |
Output is correct |
34 |
Correct |
1079 ms |
63444 KB |
Output is correct |
35 |
Correct |
2907 ms |
63448 KB |
Output is correct |
36 |
Correct |
3073 ms |
63444 KB |
Output is correct |
37 |
Correct |
3079 ms |
63444 KB |
Output is correct |
38 |
Execution timed out |
5008 ms |
63576 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |