#include<bits/stdc++.h>
#define pii pair<pair<int,int>,int>
#define F first
#define S second
using namespace std;
int n,m,q,bit[100010],v[100010],c[100010],dp[25][100010],depth[100010],u;
vector<int> e[100010];
vector<pii> qu;
bool cmp(pii a, pii b){
return a.S<b.S;
}
void modify(int x,int p){
while(p<=m){
bit[p]+=x;
p+=p&(-p);
}
return ;
}
int que(int p){
int ans=0;
while(p){
ans+=bit[p];
p-=p&(-p);
}
return ans;
}
void dfs(int now,int last){
dp[0][now]=last;
depth[now]=depth[last]+1;
for(auto x:e[now]){
if(x==last)continue;
dfs(x,now);
}
return;
}
int lca(int a,int b){
if(depth[a]>depth[b])swap(a,b);
for(int i=19;i>=0;i--){
if(depth[a]+(1<<i)<=depth[b]){
b=dp[i][b];
}
}
if(a==b)return a;
for(int i=19;i>=0;i--){
if(dp[i][a]!=dp[i][b]){
a=dp[i][a];
b=dp[i][b];
}
}
return dp[0][a];
}
int main(){
u=0;
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m>>q;
for(int i=1;i<n;i++){
int a,b;
cin>>a>>b;
e[a].push_back(b);
e[b].push_back(a);
}
for(int i=1;i<=m;i++){
cin>>v[i];
}
for(int i=0;i<q;i++){
int a,b;
cin>>a>>b;
if(a==m)b=1,u++;
qu.push_back({{a,b},i});
}
dfs(1,1);
for(int i=1;i<=19;i++){
for(int j=1;j<=n;j++){
dp[i][j]=dp[i-1][dp[i-1][j]];
}
}
sort(qu.begin(),qu.end());
int now=q-1-u;
for(int i=m-1;i>0;i--){
int p=lca(v[i],v[i+1]);
int nw=v[i];
while(nw!=p){
if(c[nw]>0)modify(-1,c[nw]);
c[nw]=i;
modify(1,i);
nw=dp[0][nw];
}
nw=v[i+1];
while(nw!=p){
if(c[nw]>0)modify(-1,c[nw]);
c[nw]=i;
modify(1,i);
nw=dp[0][nw];
}
if(c[p]>0)modify(-1,c[p]);
c[p]=i;
modify(1,i);
while(now>=0&&qu[now].F.F==i){
if(qu[now].F.F==qu[now].F.S){
qu[now].F.S=1;
}else{
qu[now].F.S=que(qu[now].F.S-1);
}
now--;
}
}
sort(qu.begin(),qu.end(),cmp);
for(auto x:qu){
cout<<x.F.S<<"\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
2 ms |
10840 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
3 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10840 KB |
Output is correct |
9 |
Correct |
2 ms |
10840 KB |
Output is correct |
10 |
Correct |
3 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10920 KB |
Output is correct |
12 |
Correct |
2 ms |
10840 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10844 KB |
Output is correct |
17 |
Correct |
2 ms |
10932 KB |
Output is correct |
18 |
Correct |
3 ms |
10844 KB |
Output is correct |
19 |
Correct |
2 ms |
10844 KB |
Output is correct |
20 |
Correct |
3 ms |
10844 KB |
Output is correct |
21 |
Correct |
2 ms |
10844 KB |
Output is correct |
22 |
Correct |
2 ms |
10844 KB |
Output is correct |
23 |
Correct |
2 ms |
11096 KB |
Output is correct |
24 |
Correct |
2 ms |
10844 KB |
Output is correct |
25 |
Correct |
2 ms |
10844 KB |
Output is correct |
26 |
Correct |
2 ms |
10928 KB |
Output is correct |
27 |
Correct |
2 ms |
10844 KB |
Output is correct |
28 |
Correct |
2 ms |
10892 KB |
Output is correct |
29 |
Correct |
2 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
2 ms |
10840 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
3 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10840 KB |
Output is correct |
9 |
Correct |
2 ms |
10840 KB |
Output is correct |
10 |
Correct |
3 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10920 KB |
Output is correct |
12 |
Correct |
2 ms |
10840 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10844 KB |
Output is correct |
17 |
Correct |
2 ms |
10932 KB |
Output is correct |
18 |
Correct |
3 ms |
10844 KB |
Output is correct |
19 |
Correct |
2 ms |
10844 KB |
Output is correct |
20 |
Correct |
3 ms |
10844 KB |
Output is correct |
21 |
Correct |
2 ms |
10844 KB |
Output is correct |
22 |
Correct |
2 ms |
10844 KB |
Output is correct |
23 |
Correct |
2 ms |
11096 KB |
Output is correct |
24 |
Correct |
2 ms |
10844 KB |
Output is correct |
25 |
Correct |
2 ms |
10844 KB |
Output is correct |
26 |
Correct |
2 ms |
10928 KB |
Output is correct |
27 |
Correct |
2 ms |
10844 KB |
Output is correct |
28 |
Correct |
2 ms |
10892 KB |
Output is correct |
29 |
Correct |
2 ms |
10844 KB |
Output is correct |
30 |
Correct |
3 ms |
10844 KB |
Output is correct |
31 |
Correct |
3 ms |
10844 KB |
Output is correct |
32 |
Correct |
3 ms |
11100 KB |
Output is correct |
33 |
Correct |
4 ms |
10844 KB |
Output is correct |
34 |
Correct |
4 ms |
11100 KB |
Output is correct |
35 |
Correct |
4 ms |
11100 KB |
Output is correct |
36 |
Correct |
4 ms |
11100 KB |
Output is correct |
37 |
Correct |
3 ms |
11100 KB |
Output is correct |
38 |
Correct |
15 ms |
11100 KB |
Output is correct |
39 |
Correct |
15 ms |
11096 KB |
Output is correct |
40 |
Correct |
16 ms |
11152 KB |
Output is correct |
41 |
Correct |
15 ms |
11352 KB |
Output is correct |
42 |
Correct |
15 ms |
11168 KB |
Output is correct |
43 |
Correct |
16 ms |
11096 KB |
Output is correct |
44 |
Correct |
10 ms |
11100 KB |
Output is correct |
45 |
Correct |
8 ms |
11100 KB |
Output is correct |
46 |
Correct |
9 ms |
11100 KB |
Output is correct |
47 |
Correct |
8 ms |
11100 KB |
Output is correct |
48 |
Correct |
7 ms |
11100 KB |
Output is correct |
49 |
Correct |
9 ms |
11100 KB |
Output is correct |
50 |
Correct |
3 ms |
11100 KB |
Output is correct |
51 |
Correct |
3 ms |
11100 KB |
Output is correct |
52 |
Correct |
4 ms |
10980 KB |
Output is correct |
53 |
Correct |
3 ms |
11096 KB |
Output is correct |
54 |
Correct |
3 ms |
11100 KB |
Output is correct |
55 |
Correct |
3 ms |
11100 KB |
Output is correct |
56 |
Correct |
3 ms |
10976 KB |
Output is correct |
57 |
Correct |
3 ms |
10844 KB |
Output is correct |
58 |
Correct |
4 ms |
10840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
3 ms |
10844 KB |
Output is correct |
3 |
Correct |
3 ms |
10844 KB |
Output is correct |
4 |
Execution timed out |
5061 ms |
19228 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
55 ms |
14208 KB |
Output is correct |
3 |
Correct |
83 ms |
14808 KB |
Output is correct |
4 |
Correct |
69 ms |
16980 KB |
Output is correct |
5 |
Correct |
105 ms |
19028 KB |
Output is correct |
6 |
Correct |
99 ms |
19060 KB |
Output is correct |
7 |
Correct |
104 ms |
19028 KB |
Output is correct |
8 |
Correct |
99 ms |
19028 KB |
Output is correct |
9 |
Correct |
102 ms |
18976 KB |
Output is correct |
10 |
Correct |
110 ms |
19084 KB |
Output is correct |
11 |
Correct |
104 ms |
18944 KB |
Output is correct |
12 |
Correct |
106 ms |
19024 KB |
Output is correct |
13 |
Correct |
115 ms |
19476 KB |
Output is correct |
14 |
Correct |
119 ms |
19804 KB |
Output is correct |
15 |
Correct |
127 ms |
21696 KB |
Output is correct |
16 |
Correct |
122 ms |
19476 KB |
Output is correct |
17 |
Correct |
108 ms |
19472 KB |
Output is correct |
18 |
Correct |
107 ms |
19472 KB |
Output is correct |
19 |
Correct |
2234 ms |
19136 KB |
Output is correct |
20 |
Correct |
2432 ms |
19312 KB |
Output is correct |
21 |
Correct |
2066 ms |
19264 KB |
Output is correct |
22 |
Correct |
2003 ms |
18960 KB |
Output is correct |
23 |
Correct |
2391 ms |
19424 KB |
Output is correct |
24 |
Correct |
2257 ms |
19036 KB |
Output is correct |
25 |
Correct |
2180 ms |
19236 KB |
Output is correct |
26 |
Correct |
2127 ms |
19284 KB |
Output is correct |
27 |
Correct |
2017 ms |
19260 KB |
Output is correct |
28 |
Correct |
2102 ms |
19228 KB |
Output is correct |
29 |
Correct |
2238 ms |
19068 KB |
Output is correct |
30 |
Correct |
2102 ms |
21144 KB |
Output is correct |
31 |
Correct |
2059 ms |
19448 KB |
Output is correct |
32 |
Correct |
2140 ms |
19856 KB |
Output is correct |
33 |
Correct |
1881 ms |
20432 KB |
Output is correct |
34 |
Correct |
2516 ms |
21664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
3 ms |
10844 KB |
Output is correct |
4 |
Correct |
114 ms |
17448 KB |
Output is correct |
5 |
Correct |
115 ms |
17352 KB |
Output is correct |
6 |
Correct |
144 ms |
20952 KB |
Output is correct |
7 |
Correct |
144 ms |
21936 KB |
Output is correct |
8 |
Correct |
142 ms |
21960 KB |
Output is correct |
9 |
Correct |
142 ms |
22004 KB |
Output is correct |
10 |
Correct |
148 ms |
21956 KB |
Output is correct |
11 |
Correct |
145 ms |
21924 KB |
Output is correct |
12 |
Correct |
151 ms |
21968 KB |
Output is correct |
13 |
Correct |
142 ms |
21960 KB |
Output is correct |
14 |
Correct |
49 ms |
14280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
2 ms |
10840 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
3 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10840 KB |
Output is correct |
9 |
Correct |
2 ms |
10840 KB |
Output is correct |
10 |
Correct |
3 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10920 KB |
Output is correct |
12 |
Correct |
2 ms |
10840 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10844 KB |
Output is correct |
17 |
Correct |
2 ms |
10932 KB |
Output is correct |
18 |
Correct |
3 ms |
10844 KB |
Output is correct |
19 |
Correct |
2 ms |
10844 KB |
Output is correct |
20 |
Correct |
3 ms |
10844 KB |
Output is correct |
21 |
Correct |
2 ms |
10844 KB |
Output is correct |
22 |
Correct |
2 ms |
10844 KB |
Output is correct |
23 |
Correct |
2 ms |
11096 KB |
Output is correct |
24 |
Correct |
2 ms |
10844 KB |
Output is correct |
25 |
Correct |
2 ms |
10844 KB |
Output is correct |
26 |
Correct |
2 ms |
10928 KB |
Output is correct |
27 |
Correct |
2 ms |
10844 KB |
Output is correct |
28 |
Correct |
2 ms |
10892 KB |
Output is correct |
29 |
Correct |
2 ms |
10844 KB |
Output is correct |
30 |
Correct |
3 ms |
10844 KB |
Output is correct |
31 |
Correct |
3 ms |
10844 KB |
Output is correct |
32 |
Correct |
3 ms |
11100 KB |
Output is correct |
33 |
Correct |
4 ms |
10844 KB |
Output is correct |
34 |
Correct |
4 ms |
11100 KB |
Output is correct |
35 |
Correct |
4 ms |
11100 KB |
Output is correct |
36 |
Correct |
4 ms |
11100 KB |
Output is correct |
37 |
Correct |
3 ms |
11100 KB |
Output is correct |
38 |
Correct |
15 ms |
11100 KB |
Output is correct |
39 |
Correct |
15 ms |
11096 KB |
Output is correct |
40 |
Correct |
16 ms |
11152 KB |
Output is correct |
41 |
Correct |
15 ms |
11352 KB |
Output is correct |
42 |
Correct |
15 ms |
11168 KB |
Output is correct |
43 |
Correct |
16 ms |
11096 KB |
Output is correct |
44 |
Correct |
10 ms |
11100 KB |
Output is correct |
45 |
Correct |
8 ms |
11100 KB |
Output is correct |
46 |
Correct |
9 ms |
11100 KB |
Output is correct |
47 |
Correct |
8 ms |
11100 KB |
Output is correct |
48 |
Correct |
7 ms |
11100 KB |
Output is correct |
49 |
Correct |
9 ms |
11100 KB |
Output is correct |
50 |
Correct |
3 ms |
11100 KB |
Output is correct |
51 |
Correct |
3 ms |
11100 KB |
Output is correct |
52 |
Correct |
4 ms |
10980 KB |
Output is correct |
53 |
Correct |
3 ms |
11096 KB |
Output is correct |
54 |
Correct |
3 ms |
11100 KB |
Output is correct |
55 |
Correct |
3 ms |
11100 KB |
Output is correct |
56 |
Correct |
3 ms |
10976 KB |
Output is correct |
57 |
Correct |
3 ms |
10844 KB |
Output is correct |
58 |
Correct |
4 ms |
10840 KB |
Output is correct |
59 |
Correct |
2 ms |
10844 KB |
Output is correct |
60 |
Correct |
3 ms |
10844 KB |
Output is correct |
61 |
Correct |
3 ms |
10844 KB |
Output is correct |
62 |
Execution timed out |
5061 ms |
19228 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |