#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define ppb pop_back
#define ep insert
#define endl '\n'
#define elif else if
#define pow pwr
#define sqrt sqrtt
#define int long long
#define ll long long
typedef unsigned long long ull;
using namespace std;
const int N=2005;
int n,par[N],b[N],m,q,euler[2*N],c,dis[N],f[N],lg[2*N];
vector<int> adj[N];
bool vis[N];
pair<int,int> st[20][2*N];
void dfs(int x,int p){
par[x]=p;
euler[++c]=x;
f[x]=c;
dis[x]=dis[p]+1;
for (auto u:adj[x]){
if (u==p) continue;
dfs(u,x);
euler[++c]=x;
}
return;
}
void build(){
for (int i=1;i<=c;i++) st[0][i]={dis[euler[i]],euler[i]};
for (int i=1;i<20;i++){
for (int j=1;j+(1<<i)<=c+1;j++){
st[i][j]=min(st[i-1][j],st[i-1][j+(1<<(i-1))]);
}
}return;
}
int query(int l,int r){
if (l>r) swap(l,r);
int i=lg[r-l+1];
return min(st[i][l],st[i][r-(1<<i)+1]).S;
}
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
for (int i=2;i<2*N;i++) lg[i]=lg[i/2]+1;
cin>>n>>m>>q;
for (int i=1;i<n;i++){
int x,y;
cin>>x>>y;
adj[x].pb(y);
adj[y].pb(x);
}
dfs(1,0);
build();
for (int i=1;i<=m;i++) cin>>b[i];
while (q--){
int l,r;
cin>>l>>r;
int lca=b[l];
for (int i=l;i<=r;i++){
lca=query(f[lca],f[b[i]]);
}
//cout<<lca<<endl;
for (int i=l;i<=r;i++){
int x=b[i];
while (dis[x]>=dis[lca] && !vis[x]){
vis[x]=1;
x=par[x];
}
}
int ans=0;
for (int i=1;i<=n;i++) ans+=vis[i],vis[i]=0;
cout<<ans<<endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
664 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
704 KB |
Output is correct |
20 |
Correct |
1 ms |
544 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
708 KB |
Output is correct |
26 |
Correct |
2 ms |
608 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
608 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
664 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
704 KB |
Output is correct |
20 |
Correct |
1 ms |
544 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
708 KB |
Output is correct |
26 |
Correct |
2 ms |
608 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
608 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
15 ms |
1264 KB |
Output is correct |
31 |
Correct |
20 ms |
1232 KB |
Output is correct |
32 |
Correct |
30 ms |
1324 KB |
Output is correct |
33 |
Correct |
29 ms |
1436 KB |
Output is correct |
34 |
Correct |
38 ms |
1372 KB |
Output is correct |
35 |
Correct |
66 ms |
1368 KB |
Output is correct |
36 |
Correct |
64 ms |
1372 KB |
Output is correct |
37 |
Correct |
64 ms |
1320 KB |
Output is correct |
38 |
Correct |
28 ms |
1508 KB |
Output is correct |
39 |
Correct |
32 ms |
1372 KB |
Output is correct |
40 |
Correct |
28 ms |
1372 KB |
Output is correct |
41 |
Correct |
55 ms |
1524 KB |
Output is correct |
42 |
Correct |
53 ms |
1504 KB |
Output is correct |
43 |
Correct |
61 ms |
1500 KB |
Output is correct |
44 |
Correct |
27 ms |
1368 KB |
Output is correct |
45 |
Correct |
35 ms |
1876 KB |
Output is correct |
46 |
Correct |
28 ms |
1372 KB |
Output is correct |
47 |
Correct |
53 ms |
1372 KB |
Output is correct |
48 |
Correct |
54 ms |
1372 KB |
Output is correct |
49 |
Correct |
52 ms |
1372 KB |
Output is correct |
50 |
Correct |
25 ms |
1368 KB |
Output is correct |
51 |
Correct |
26 ms |
1460 KB |
Output is correct |
52 |
Correct |
30 ms |
1372 KB |
Output is correct |
53 |
Correct |
25 ms |
1372 KB |
Output is correct |
54 |
Correct |
24 ms |
1372 KB |
Output is correct |
55 |
Correct |
25 ms |
1372 KB |
Output is correct |
56 |
Correct |
15 ms |
604 KB |
Output is correct |
57 |
Correct |
3 ms |
1368 KB |
Output is correct |
58 |
Correct |
3 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
564 KB |
Output is correct |
3 |
Correct |
15 ms |
604 KB |
Output is correct |
4 |
Runtime error |
1 ms |
976 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
544 KB |
Output is correct |
3 |
Correct |
15 ms |
600 KB |
Output is correct |
4 |
Runtime error |
1 ms |
860 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
664 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
704 KB |
Output is correct |
20 |
Correct |
1 ms |
544 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
708 KB |
Output is correct |
26 |
Correct |
2 ms |
608 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
608 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
15 ms |
1264 KB |
Output is correct |
31 |
Correct |
20 ms |
1232 KB |
Output is correct |
32 |
Correct |
30 ms |
1324 KB |
Output is correct |
33 |
Correct |
29 ms |
1436 KB |
Output is correct |
34 |
Correct |
38 ms |
1372 KB |
Output is correct |
35 |
Correct |
66 ms |
1368 KB |
Output is correct |
36 |
Correct |
64 ms |
1372 KB |
Output is correct |
37 |
Correct |
64 ms |
1320 KB |
Output is correct |
38 |
Correct |
28 ms |
1508 KB |
Output is correct |
39 |
Correct |
32 ms |
1372 KB |
Output is correct |
40 |
Correct |
28 ms |
1372 KB |
Output is correct |
41 |
Correct |
55 ms |
1524 KB |
Output is correct |
42 |
Correct |
53 ms |
1504 KB |
Output is correct |
43 |
Correct |
61 ms |
1500 KB |
Output is correct |
44 |
Correct |
27 ms |
1368 KB |
Output is correct |
45 |
Correct |
35 ms |
1876 KB |
Output is correct |
46 |
Correct |
28 ms |
1372 KB |
Output is correct |
47 |
Correct |
53 ms |
1372 KB |
Output is correct |
48 |
Correct |
54 ms |
1372 KB |
Output is correct |
49 |
Correct |
52 ms |
1372 KB |
Output is correct |
50 |
Correct |
25 ms |
1368 KB |
Output is correct |
51 |
Correct |
26 ms |
1460 KB |
Output is correct |
52 |
Correct |
30 ms |
1372 KB |
Output is correct |
53 |
Correct |
25 ms |
1372 KB |
Output is correct |
54 |
Correct |
24 ms |
1372 KB |
Output is correct |
55 |
Correct |
25 ms |
1372 KB |
Output is correct |
56 |
Correct |
15 ms |
604 KB |
Output is correct |
57 |
Correct |
3 ms |
1368 KB |
Output is correct |
58 |
Correct |
3 ms |
1372 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
564 KB |
Output is correct |
61 |
Correct |
15 ms |
604 KB |
Output is correct |
62 |
Runtime error |
1 ms |
976 KB |
Execution killed with signal 11 |
63 |
Halted |
0 ms |
0 KB |
- |