#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];
vector<int> adj[maxN+1];
struct cmp
{
bool operator () (const int &x,const int &y)const
{
return num[x]<num[y];
}
};
set<int,cmp> se;
void dfs(ll 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];
}
pii Find(int u)
{
int l=0,r=0;
if(se.find(u)!=se.end())
{
auto it=se.find(u);
if(it==se.begin())
{
auto it1=se.rbegin();
l=(*it1);
}
else
{
it--;
l=(*it);
}
it=se.find(u);
it++;
if(it==se.end())
{
auto it1=se.begin();
r=(*it1);
}
else r=(*it);
}
else
{
auto it1=se.lower_bound(u);
if(it1==se.end())
{
auto it=se.begin();
r=(*it);
}
else r=(*it1);
if(it1!=se.begin())
{
it1--;
l=(*it1);
}
else
{
auto it=se.rbegin();
l=(*it);
}
}
return {l,r};
}
void add(int u)
{
if(se.empty())
{
se.insert(u);
return;
}
pii tmp=Find(u);
int l=tmp.fi,r=tmp.se;
sl-=dist(l,r);
sl+=(dist(l,u)+dist(u,r));
se.insert(u);
}
void del(int u)
{
if(se.size()==1)
{
se.erase(se.find(u));
return;
}
pii tmp=Find(u);
int l=tmp.fi,r=tmp.se;
sl+=dist(l,r);
sl-=(dist(l,u)+dist(u,r));
se.erase(se.find(u));
}
struct query
{
int l,r,id;
}p[maxN+1];
int ans[maxN+1],cnt[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';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
5 ms |
4652 KB |
Output is correct |
5 |
Correct |
5 ms |
4444 KB |
Output is correct |
6 |
Correct |
5 ms |
4444 KB |
Output is correct |
7 |
Correct |
6 ms |
4444 KB |
Output is correct |
8 |
Correct |
5 ms |
4444 KB |
Output is correct |
9 |
Correct |
9 ms |
4676 KB |
Output is correct |
10 |
Correct |
9 ms |
4668 KB |
Output is correct |
11 |
Correct |
9 ms |
4672 KB |
Output is correct |
12 |
Correct |
3 ms |
4440 KB |
Output is correct |
13 |
Correct |
2 ms |
4444 KB |
Output is correct |
14 |
Correct |
2 ms |
4580 KB |
Output is correct |
15 |
Correct |
10 ms |
4680 KB |
Output is correct |
16 |
Correct |
9 ms |
4444 KB |
Output is correct |
17 |
Correct |
8 ms |
4444 KB |
Output is correct |
18 |
Correct |
9 ms |
4444 KB |
Output is correct |
19 |
Correct |
10 ms |
4680 KB |
Output is correct |
20 |
Correct |
9 ms |
4444 KB |
Output is correct |
21 |
Correct |
11 ms |
4444 KB |
Output is correct |
22 |
Correct |
9 ms |
4672 KB |
Output is correct |
23 |
Correct |
10 ms |
4444 KB |
Output is correct |
24 |
Correct |
10 ms |
4444 KB |
Output is correct |
25 |
Correct |
8 ms |
4668 KB |
Output is correct |
26 |
Correct |
9 ms |
4444 KB |
Output is correct |
27 |
Correct |
3 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
2 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
5 ms |
4652 KB |
Output is correct |
5 |
Correct |
5 ms |
4444 KB |
Output is correct |
6 |
Correct |
5 ms |
4444 KB |
Output is correct |
7 |
Correct |
6 ms |
4444 KB |
Output is correct |
8 |
Correct |
5 ms |
4444 KB |
Output is correct |
9 |
Correct |
9 ms |
4676 KB |
Output is correct |
10 |
Correct |
9 ms |
4668 KB |
Output is correct |
11 |
Correct |
9 ms |
4672 KB |
Output is correct |
12 |
Correct |
3 ms |
4440 KB |
Output is correct |
13 |
Correct |
2 ms |
4444 KB |
Output is correct |
14 |
Correct |
2 ms |
4580 KB |
Output is correct |
15 |
Correct |
10 ms |
4680 KB |
Output is correct |
16 |
Correct |
9 ms |
4444 KB |
Output is correct |
17 |
Correct |
8 ms |
4444 KB |
Output is correct |
18 |
Correct |
9 ms |
4444 KB |
Output is correct |
19 |
Correct |
10 ms |
4680 KB |
Output is correct |
20 |
Correct |
9 ms |
4444 KB |
Output is correct |
21 |
Correct |
11 ms |
4444 KB |
Output is correct |
22 |
Correct |
9 ms |
4672 KB |
Output is correct |
23 |
Correct |
10 ms |
4444 KB |
Output is correct |
24 |
Correct |
10 ms |
4444 KB |
Output is correct |
25 |
Correct |
8 ms |
4668 KB |
Output is correct |
26 |
Correct |
9 ms |
4444 KB |
Output is correct |
27 |
Correct |
3 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
2 ms |
4444 KB |
Output is correct |
30 |
Correct |
58 ms |
4844 KB |
Output is correct |
31 |
Correct |
77 ms |
4700 KB |
Output is correct |
32 |
Correct |
94 ms |
4920 KB |
Output is correct |
33 |
Correct |
94 ms |
4932 KB |
Output is correct |
34 |
Correct |
94 ms |
4944 KB |
Output is correct |
35 |
Correct |
8 ms |
4956 KB |
Output is correct |
36 |
Correct |
10 ms |
4960 KB |
Output is correct |
37 |
Correct |
9 ms |
4876 KB |
Output is correct |
38 |
Correct |
91 ms |
5012 KB |
Output is correct |
39 |
Correct |
89 ms |
4952 KB |
Output is correct |
40 |
Correct |
90 ms |
4952 KB |
Output is correct |
41 |
Correct |
8 ms |
4952 KB |
Output is correct |
42 |
Correct |
10 ms |
4956 KB |
Output is correct |
43 |
Correct |
10 ms |
5048 KB |
Output is correct |
44 |
Correct |
94 ms |
4980 KB |
Output is correct |
45 |
Correct |
90 ms |
4956 KB |
Output is correct |
46 |
Correct |
90 ms |
4976 KB |
Output is correct |
47 |
Correct |
8 ms |
4952 KB |
Output is correct |
48 |
Correct |
8 ms |
4956 KB |
Output is correct |
49 |
Correct |
8 ms |
4956 KB |
Output is correct |
50 |
Correct |
89 ms |
4952 KB |
Output is correct |
51 |
Correct |
86 ms |
4924 KB |
Output is correct |
52 |
Correct |
85 ms |
4936 KB |
Output is correct |
53 |
Correct |
95 ms |
4944 KB |
Output is correct |
54 |
Correct |
95 ms |
4936 KB |
Output is correct |
55 |
Correct |
89 ms |
4940 KB |
Output is correct |
56 |
Correct |
3 ms |
4656 KB |
Output is correct |
57 |
Correct |
3 ms |
4700 KB |
Output is correct |
58 |
Correct |
5 ms |
4904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Execution timed out |
5076 ms |
18704 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
89 ms |
11992 KB |
Output is correct |
3 |
Correct |
139 ms |
12892 KB |
Output is correct |
4 |
Correct |
122 ms |
13648 KB |
Output is correct |
5 |
Correct |
104 ms |
20816 KB |
Output is correct |
6 |
Correct |
167 ms |
20052 KB |
Output is correct |
7 |
Correct |
220 ms |
18768 KB |
Output is correct |
8 |
Correct |
231 ms |
18164 KB |
Output is correct |
9 |
Correct |
220 ms |
18004 KB |
Output is correct |
10 |
Correct |
209 ms |
17964 KB |
Output is correct |
11 |
Correct |
196 ms |
18000 KB |
Output is correct |
12 |
Correct |
180 ms |
17748 KB |
Output is correct |
13 |
Correct |
169 ms |
18056 KB |
Output is correct |
14 |
Correct |
157 ms |
18188 KB |
Output is correct |
15 |
Correct |
166 ms |
19176 KB |
Output is correct |
16 |
Correct |
187 ms |
19600 KB |
Output is correct |
17 |
Correct |
193 ms |
19536 KB |
Output is correct |
18 |
Correct |
192 ms |
19572 KB |
Output is correct |
19 |
Correct |
127 ms |
20816 KB |
Output is correct |
20 |
Correct |
162 ms |
20808 KB |
Output is correct |
21 |
Correct |
221 ms |
19500 KB |
Output is correct |
22 |
Correct |
255 ms |
18768 KB |
Output is correct |
23 |
Correct |
278 ms |
18260 KB |
Output is correct |
24 |
Correct |
280 ms |
18060 KB |
Output is correct |
25 |
Correct |
300 ms |
17984 KB |
Output is correct |
26 |
Correct |
280 ms |
18004 KB |
Output is correct |
27 |
Correct |
259 ms |
17784 KB |
Output is correct |
28 |
Correct |
255 ms |
17744 KB |
Output is correct |
29 |
Correct |
253 ms |
17748 KB |
Output is correct |
30 |
Correct |
218 ms |
18000 KB |
Output is correct |
31 |
Correct |
213 ms |
18080 KB |
Output is correct |
32 |
Correct |
204 ms |
18188 KB |
Output is correct |
33 |
Correct |
186 ms |
18648 KB |
Output is correct |
34 |
Correct |
172 ms |
19284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Execution timed out |
5092 ms |
14648 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
5 ms |
4652 KB |
Output is correct |
5 |
Correct |
5 ms |
4444 KB |
Output is correct |
6 |
Correct |
5 ms |
4444 KB |
Output is correct |
7 |
Correct |
6 ms |
4444 KB |
Output is correct |
8 |
Correct |
5 ms |
4444 KB |
Output is correct |
9 |
Correct |
9 ms |
4676 KB |
Output is correct |
10 |
Correct |
9 ms |
4668 KB |
Output is correct |
11 |
Correct |
9 ms |
4672 KB |
Output is correct |
12 |
Correct |
3 ms |
4440 KB |
Output is correct |
13 |
Correct |
2 ms |
4444 KB |
Output is correct |
14 |
Correct |
2 ms |
4580 KB |
Output is correct |
15 |
Correct |
10 ms |
4680 KB |
Output is correct |
16 |
Correct |
9 ms |
4444 KB |
Output is correct |
17 |
Correct |
8 ms |
4444 KB |
Output is correct |
18 |
Correct |
9 ms |
4444 KB |
Output is correct |
19 |
Correct |
10 ms |
4680 KB |
Output is correct |
20 |
Correct |
9 ms |
4444 KB |
Output is correct |
21 |
Correct |
11 ms |
4444 KB |
Output is correct |
22 |
Correct |
9 ms |
4672 KB |
Output is correct |
23 |
Correct |
10 ms |
4444 KB |
Output is correct |
24 |
Correct |
10 ms |
4444 KB |
Output is correct |
25 |
Correct |
8 ms |
4668 KB |
Output is correct |
26 |
Correct |
9 ms |
4444 KB |
Output is correct |
27 |
Correct |
3 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
2 ms |
4444 KB |
Output is correct |
30 |
Correct |
58 ms |
4844 KB |
Output is correct |
31 |
Correct |
77 ms |
4700 KB |
Output is correct |
32 |
Correct |
94 ms |
4920 KB |
Output is correct |
33 |
Correct |
94 ms |
4932 KB |
Output is correct |
34 |
Correct |
94 ms |
4944 KB |
Output is correct |
35 |
Correct |
8 ms |
4956 KB |
Output is correct |
36 |
Correct |
10 ms |
4960 KB |
Output is correct |
37 |
Correct |
9 ms |
4876 KB |
Output is correct |
38 |
Correct |
91 ms |
5012 KB |
Output is correct |
39 |
Correct |
89 ms |
4952 KB |
Output is correct |
40 |
Correct |
90 ms |
4952 KB |
Output is correct |
41 |
Correct |
8 ms |
4952 KB |
Output is correct |
42 |
Correct |
10 ms |
4956 KB |
Output is correct |
43 |
Correct |
10 ms |
5048 KB |
Output is correct |
44 |
Correct |
94 ms |
4980 KB |
Output is correct |
45 |
Correct |
90 ms |
4956 KB |
Output is correct |
46 |
Correct |
90 ms |
4976 KB |
Output is correct |
47 |
Correct |
8 ms |
4952 KB |
Output is correct |
48 |
Correct |
8 ms |
4956 KB |
Output is correct |
49 |
Correct |
8 ms |
4956 KB |
Output is correct |
50 |
Correct |
89 ms |
4952 KB |
Output is correct |
51 |
Correct |
86 ms |
4924 KB |
Output is correct |
52 |
Correct |
85 ms |
4936 KB |
Output is correct |
53 |
Correct |
95 ms |
4944 KB |
Output is correct |
54 |
Correct |
95 ms |
4936 KB |
Output is correct |
55 |
Correct |
89 ms |
4940 KB |
Output is correct |
56 |
Correct |
3 ms |
4656 KB |
Output is correct |
57 |
Correct |
3 ms |
4700 KB |
Output is correct |
58 |
Correct |
5 ms |
4904 KB |
Output is correct |
59 |
Correct |
2 ms |
4440 KB |
Output is correct |
60 |
Correct |
1 ms |
4444 KB |
Output is correct |
61 |
Correct |
2 ms |
4444 KB |
Output is correct |
62 |
Execution timed out |
5076 ms |
18704 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |