#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
const int maxN=1e5+5,LG=__lg(maxN)+1,BLOCK=321;
const ll inf=2e18;
int n,m,q,h[maxN+1],up[maxN+1][LG+1],num[maxN+1],timer=0,sl=0,a[maxN+1],tour[maxN+1],cnt[maxN+1];
vector<int> adj[maxN+1];
void dfs(ll u)
{
tour[++timer]=u;
num[u]=timer;
for(auto v:adj[u])
{
if(v==up[u][0])
{
continue;
}
h[v]=h[u]+1;
up[v][0]=u;
for(int j=1;j<=LG;j++)
{
up[v][j]=up[up[v][j-1]][j-1];
}
dfs(v);
}
}
int lca(int u,int v)
{
if(h[u]<h[v])
{
swap(u,v);
}
for(int j=LG;j>=0;j--)
{
if(h[up[u][j]]>=h[v])
{
u=up[u][j];
}
}
for(int j=LG;j>=0;j--)
{
if(up[u][j]!=up[v][j])
{
u=up[u][j];
v=up[v][j];
}
}
if(u==v)
{
return u;
}
else return up[u][0];
}
int dist(int u,int v)
{
int p=lca(u,v);
return h[u]+h[v]-2*h[p];
}
struct fenwick
{
int bit[maxN+1];
fenwick()
{
memset(bit,0,sizeof(bit));
}
void update(int u,int val)
{
for(int i=u;i<=n;i+=(i&(-i)))
{
bit[i]+=val;
}
}
int get(int u)
{
int ans=0;
for(int i=u;i>0;i-=(i&(-i)))
{
ans+=bit[i];
}
return ans;
}
int get_first()
{
int i=0,sum=0;
for(int j=LG;j>=0;j--)
{
if(i+(1LL<<j)<n&&sum+bit[i+(1LL<<j)]==0)
{
i+=(1LL<<j);
sum+=bit[i];
}
}
return i+1;
}
int get_last()
{
int i=0,tmp=get(n),sum=0;
for(int j=LG;j>=0;j--)
{
if(i+(1LL<<j)<=n&&tmp-sum-bit[i+(1LL<<j)]+(cnt[tour[i+(1LL<<j)]]>0)>0)
{
i+=(1LL<<j);
sum+=bit[i];
}
}
return i;
}
int lower(int u)
{
int pos=num[u],i=0;
int tmp=get(pos-1),sum=0;
for(int j=LG;j>=0;j--)
{
if(i+(1LL<<j)<pos&&tmp-sum-bit[i+(1LL<<j)]+(cnt[tour[i+(1LL<<j)]]>0)>0)
{
i+=(1LL<<j);
sum+=bit[i];
}
}
if(i==0)
{
return get_last();
}
return i;
}
int upper(int u)
{
int pos=num[u],i=0;
int tmp=get(pos),sum=0;
for(int j=LG;j>=0;j--)
{
if(i+(1LL<<j)<=n&&sum+bit[i+(1LL<<j)]-tmp<1)
{
i+=(1LL<<j);
sum+=bit[i];
}
}
if(i==n)
{
return get_first();
}
return i+1;
}
}f;
void add(int u)
{
if(f.get(n)==0)
{
f.update(num[u],1);
return;
}
int l=tour[f.lower(u)],r=tour[f.upper(u)];
sl-=dist(l,r);
sl+=(dist(l,u)+dist(u,r));
//cout<<u<<" "<<l<<" "<<r<<" "<<sl<<'\n';
f.update(num[u],1);
}
void del(int u)
{
if(f.get(n)==1)
{
f.update(num[u],-1);
return;
}
int l=tour[f.lower(u)],r=tour[f.upper(u)];
//cout<<sl<<" ";
sl+=dist(l,r);
sl-=(dist(l,u)+dist(u,r));
//cout<<u<<" "<<l<<" "<<r<<" "<<sl<<'\n';
f.update(num[u],-1);
}
struct query
{
int l,r,id;
}p[maxN+1];
int ans[maxN+1];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>q;
for(int i=1;i<n;i++)
{
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
h[1]=1;
dfs(1);
for(int i=1;i<=m;i++)
{
cin>>a[i];
}
for(int i=1;i<=q;i++)
{
cin>>p[i].l>>p[i].r;
p[i].id=i;
}
sort(p+1,p+q+1,[&](query x,query y)
{
if(x.r/BLOCK==y.r/BLOCK)
{
return x.l<y.l;
}
return x.r/BLOCK<y.r/BLOCK;
});
int lc=1,rc=0;
for(int i=1;i<=q;i++)
{
int l=p[i].l,r=p[i].r,id=p[i].id;
while(rc<r)
{
rc++;
if(!cnt[a[rc]])
{
add(a[rc]);
}
cnt[a[rc]]++;
}
while(lc>l)
{
lc--;
if(!cnt[a[lc]])
{
add(a[lc]);
}
cnt[a[lc]]++;
}
while(rc>r)
{
cnt[a[rc]]--;
if(!cnt[a[rc]])
{
del(a[rc]);
}
rc--;
}
while(lc<l)
{
cnt[a[lc]]--;
if(!cnt[a[lc]])
{
del(a[lc]);
}
lc++;
}
ans[id]=sl/2;
}
for(int i=1;i<=q;i++)
{
cout<<ans[i]+1<<'\n';
}
}
/*7 6 2
6 7
5 7
4 6
3 7
2 5
1 5
4 3 5 2 4 6
1 3
3 6*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
3164 KB |
Output is correct |
4 |
Correct |
4 ms |
3264 KB |
Output is correct |
5 |
Correct |
5 ms |
3164 KB |
Output is correct |
6 |
Correct |
4 ms |
3160 KB |
Output is correct |
7 |
Correct |
5 ms |
3268 KB |
Output is correct |
8 |
Correct |
4 ms |
4444 KB |
Output is correct |
9 |
Correct |
8 ms |
3164 KB |
Output is correct |
10 |
Correct |
8 ms |
3164 KB |
Output is correct |
11 |
Correct |
9 ms |
4444 KB |
Output is correct |
12 |
Correct |
2 ms |
3164 KB |
Output is correct |
13 |
Correct |
2 ms |
3164 KB |
Output is correct |
14 |
Correct |
2 ms |
3164 KB |
Output is correct |
15 |
Correct |
9 ms |
3288 KB |
Output is correct |
16 |
Correct |
8 ms |
3164 KB |
Output is correct |
17 |
Correct |
7 ms |
3164 KB |
Output is correct |
18 |
Correct |
8 ms |
3288 KB |
Output is correct |
19 |
Correct |
9 ms |
3164 KB |
Output is correct |
20 |
Correct |
8 ms |
3160 KB |
Output is correct |
21 |
Correct |
10 ms |
3160 KB |
Output is correct |
22 |
Correct |
8 ms |
3164 KB |
Output is correct |
23 |
Correct |
9 ms |
3164 KB |
Output is correct |
24 |
Correct |
9 ms |
3164 KB |
Output is correct |
25 |
Correct |
8 ms |
3164 KB |
Output is correct |
26 |
Correct |
8 ms |
3284 KB |
Output is correct |
27 |
Correct |
2 ms |
3164 KB |
Output is correct |
28 |
Correct |
1 ms |
3164 KB |
Output is correct |
29 |
Correct |
2 ms |
3164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
3164 KB |
Output is correct |
4 |
Correct |
4 ms |
3264 KB |
Output is correct |
5 |
Correct |
5 ms |
3164 KB |
Output is correct |
6 |
Correct |
4 ms |
3160 KB |
Output is correct |
7 |
Correct |
5 ms |
3268 KB |
Output is correct |
8 |
Correct |
4 ms |
4444 KB |
Output is correct |
9 |
Correct |
8 ms |
3164 KB |
Output is correct |
10 |
Correct |
8 ms |
3164 KB |
Output is correct |
11 |
Correct |
9 ms |
4444 KB |
Output is correct |
12 |
Correct |
2 ms |
3164 KB |
Output is correct |
13 |
Correct |
2 ms |
3164 KB |
Output is correct |
14 |
Correct |
2 ms |
3164 KB |
Output is correct |
15 |
Correct |
9 ms |
3288 KB |
Output is correct |
16 |
Correct |
8 ms |
3164 KB |
Output is correct |
17 |
Correct |
7 ms |
3164 KB |
Output is correct |
18 |
Correct |
8 ms |
3288 KB |
Output is correct |
19 |
Correct |
9 ms |
3164 KB |
Output is correct |
20 |
Correct |
8 ms |
3160 KB |
Output is correct |
21 |
Correct |
10 ms |
3160 KB |
Output is correct |
22 |
Correct |
8 ms |
3164 KB |
Output is correct |
23 |
Correct |
9 ms |
3164 KB |
Output is correct |
24 |
Correct |
9 ms |
3164 KB |
Output is correct |
25 |
Correct |
8 ms |
3164 KB |
Output is correct |
26 |
Correct |
8 ms |
3284 KB |
Output is correct |
27 |
Correct |
2 ms |
3164 KB |
Output is correct |
28 |
Correct |
1 ms |
3164 KB |
Output is correct |
29 |
Correct |
2 ms |
3164 KB |
Output is correct |
30 |
Correct |
51 ms |
3420 KB |
Output is correct |
31 |
Correct |
68 ms |
3420 KB |
Output is correct |
32 |
Correct |
82 ms |
4896 KB |
Output is correct |
33 |
Correct |
85 ms |
3532 KB |
Output is correct |
34 |
Correct |
83 ms |
3416 KB |
Output is correct |
35 |
Correct |
8 ms |
3416 KB |
Output is correct |
36 |
Correct |
9 ms |
3416 KB |
Output is correct |
37 |
Correct |
8 ms |
3420 KB |
Output is correct |
38 |
Correct |
88 ms |
4956 KB |
Output is correct |
39 |
Correct |
79 ms |
4952 KB |
Output is correct |
40 |
Correct |
81 ms |
3416 KB |
Output is correct |
41 |
Correct |
8 ms |
3676 KB |
Output is correct |
42 |
Correct |
9 ms |
3696 KB |
Output is correct |
43 |
Correct |
9 ms |
3420 KB |
Output is correct |
44 |
Correct |
82 ms |
3420 KB |
Output is correct |
45 |
Correct |
80 ms |
3420 KB |
Output is correct |
46 |
Correct |
88 ms |
3420 KB |
Output is correct |
47 |
Correct |
8 ms |
3416 KB |
Output is correct |
48 |
Correct |
9 ms |
4700 KB |
Output is correct |
49 |
Correct |
8 ms |
3600 KB |
Output is correct |
50 |
Correct |
79 ms |
3540 KB |
Output is correct |
51 |
Correct |
79 ms |
3416 KB |
Output is correct |
52 |
Correct |
78 ms |
4700 KB |
Output is correct |
53 |
Correct |
88 ms |
4700 KB |
Output is correct |
54 |
Correct |
81 ms |
3420 KB |
Output is correct |
55 |
Correct |
79 ms |
3416 KB |
Output is correct |
56 |
Correct |
2 ms |
3164 KB |
Output is correct |
57 |
Correct |
2 ms |
4700 KB |
Output is correct |
58 |
Correct |
5 ms |
3420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3164 KB |
Output is correct |
2 |
Correct |
2 ms |
3040 KB |
Output is correct |
3 |
Correct |
2 ms |
3160 KB |
Output is correct |
4 |
Execution timed out |
5094 ms |
17324 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3160 KB |
Output is correct |
2 |
Correct |
116 ms |
10796 KB |
Output is correct |
3 |
Correct |
178 ms |
11608 KB |
Output is correct |
4 |
Correct |
150 ms |
12688 KB |
Output is correct |
5 |
Correct |
97 ms |
18508 KB |
Output is correct |
6 |
Correct |
142 ms |
18512 KB |
Output is correct |
7 |
Correct |
188 ms |
18512 KB |
Output is correct |
8 |
Correct |
231 ms |
18544 KB |
Output is correct |
9 |
Correct |
234 ms |
18520 KB |
Output is correct |
10 |
Correct |
256 ms |
18552 KB |
Output is correct |
11 |
Correct |
246 ms |
18560 KB |
Output is correct |
12 |
Correct |
231 ms |
18572 KB |
Output is correct |
13 |
Correct |
236 ms |
18652 KB |
Output is correct |
14 |
Correct |
221 ms |
19024 KB |
Output is correct |
15 |
Correct |
240 ms |
19868 KB |
Output is correct |
16 |
Correct |
211 ms |
18720 KB |
Output is correct |
17 |
Correct |
205 ms |
18516 KB |
Output is correct |
18 |
Correct |
200 ms |
18512 KB |
Output is correct |
19 |
Correct |
102 ms |
18512 KB |
Output is correct |
20 |
Correct |
122 ms |
18512 KB |
Output is correct |
21 |
Correct |
174 ms |
18516 KB |
Output is correct |
22 |
Correct |
215 ms |
18584 KB |
Output is correct |
23 |
Correct |
242 ms |
18572 KB |
Output is correct |
24 |
Correct |
268 ms |
18428 KB |
Output is correct |
25 |
Correct |
290 ms |
18516 KB |
Output is correct |
26 |
Correct |
312 ms |
18516 KB |
Output is correct |
27 |
Correct |
323 ms |
18516 KB |
Output is correct |
28 |
Correct |
313 ms |
18524 KB |
Output is correct |
29 |
Correct |
309 ms |
18556 KB |
Output is correct |
30 |
Correct |
304 ms |
18672 KB |
Output is correct |
31 |
Correct |
284 ms |
18768 KB |
Output is correct |
32 |
Correct |
297 ms |
18808 KB |
Output is correct |
33 |
Correct |
289 ms |
19284 KB |
Output is correct |
34 |
Correct |
269 ms |
20072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
2 ms |
3164 KB |
Output is correct |
4 |
Execution timed out |
5093 ms |
13912 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
3164 KB |
Output is correct |
4 |
Correct |
4 ms |
3264 KB |
Output is correct |
5 |
Correct |
5 ms |
3164 KB |
Output is correct |
6 |
Correct |
4 ms |
3160 KB |
Output is correct |
7 |
Correct |
5 ms |
3268 KB |
Output is correct |
8 |
Correct |
4 ms |
4444 KB |
Output is correct |
9 |
Correct |
8 ms |
3164 KB |
Output is correct |
10 |
Correct |
8 ms |
3164 KB |
Output is correct |
11 |
Correct |
9 ms |
4444 KB |
Output is correct |
12 |
Correct |
2 ms |
3164 KB |
Output is correct |
13 |
Correct |
2 ms |
3164 KB |
Output is correct |
14 |
Correct |
2 ms |
3164 KB |
Output is correct |
15 |
Correct |
9 ms |
3288 KB |
Output is correct |
16 |
Correct |
8 ms |
3164 KB |
Output is correct |
17 |
Correct |
7 ms |
3164 KB |
Output is correct |
18 |
Correct |
8 ms |
3288 KB |
Output is correct |
19 |
Correct |
9 ms |
3164 KB |
Output is correct |
20 |
Correct |
8 ms |
3160 KB |
Output is correct |
21 |
Correct |
10 ms |
3160 KB |
Output is correct |
22 |
Correct |
8 ms |
3164 KB |
Output is correct |
23 |
Correct |
9 ms |
3164 KB |
Output is correct |
24 |
Correct |
9 ms |
3164 KB |
Output is correct |
25 |
Correct |
8 ms |
3164 KB |
Output is correct |
26 |
Correct |
8 ms |
3284 KB |
Output is correct |
27 |
Correct |
2 ms |
3164 KB |
Output is correct |
28 |
Correct |
1 ms |
3164 KB |
Output is correct |
29 |
Correct |
2 ms |
3164 KB |
Output is correct |
30 |
Correct |
51 ms |
3420 KB |
Output is correct |
31 |
Correct |
68 ms |
3420 KB |
Output is correct |
32 |
Correct |
82 ms |
4896 KB |
Output is correct |
33 |
Correct |
85 ms |
3532 KB |
Output is correct |
34 |
Correct |
83 ms |
3416 KB |
Output is correct |
35 |
Correct |
8 ms |
3416 KB |
Output is correct |
36 |
Correct |
9 ms |
3416 KB |
Output is correct |
37 |
Correct |
8 ms |
3420 KB |
Output is correct |
38 |
Correct |
88 ms |
4956 KB |
Output is correct |
39 |
Correct |
79 ms |
4952 KB |
Output is correct |
40 |
Correct |
81 ms |
3416 KB |
Output is correct |
41 |
Correct |
8 ms |
3676 KB |
Output is correct |
42 |
Correct |
9 ms |
3696 KB |
Output is correct |
43 |
Correct |
9 ms |
3420 KB |
Output is correct |
44 |
Correct |
82 ms |
3420 KB |
Output is correct |
45 |
Correct |
80 ms |
3420 KB |
Output is correct |
46 |
Correct |
88 ms |
3420 KB |
Output is correct |
47 |
Correct |
8 ms |
3416 KB |
Output is correct |
48 |
Correct |
9 ms |
4700 KB |
Output is correct |
49 |
Correct |
8 ms |
3600 KB |
Output is correct |
50 |
Correct |
79 ms |
3540 KB |
Output is correct |
51 |
Correct |
79 ms |
3416 KB |
Output is correct |
52 |
Correct |
78 ms |
4700 KB |
Output is correct |
53 |
Correct |
88 ms |
4700 KB |
Output is correct |
54 |
Correct |
81 ms |
3420 KB |
Output is correct |
55 |
Correct |
79 ms |
3416 KB |
Output is correct |
56 |
Correct |
2 ms |
3164 KB |
Output is correct |
57 |
Correct |
2 ms |
4700 KB |
Output is correct |
58 |
Correct |
5 ms |
3420 KB |
Output is correct |
59 |
Correct |
1 ms |
3164 KB |
Output is correct |
60 |
Correct |
2 ms |
3040 KB |
Output is correct |
61 |
Correct |
2 ms |
3160 KB |
Output is correct |
62 |
Execution timed out |
5094 ms |
17324 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |