#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<int,int>pii;
typedef pair<int,pii>pi2;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
vector<int>adj[100005];
int in[100005];
int out[100005];
int two[18][100005];
int d[100005];
int ptr=0;
void dfs(int index, int par){
in[index]=++ptr;
for(int x=0;x<17;x++){
two[x+1][index]=two[x][two[x][index]];
}
for(auto it:adj[index]){
if(it==par) continue;
two[0][it]=index;
d[it]=d[index]+1;
dfs(it,index);
}
out[index]=ptr;
}
int lca(int a, int b){
if(d[a]>d[b]) swap(a,b);
int diff=d[b]-d[a];
for(int x=0;x<18;x++){
if(diff&(1<<x)){
b=two[x][b];
}
}
if(a==b) return a;
for(int x=18;x>=0;x--){
if(two[x][a]!=two[x][b]){
a=two[x][a];
b=two[x][b];
}
}
return two[0][a];
}
int f(int a, int b){
int hold=lca(a,b);
return d[a]+d[b]-2*d[hold];
}
int blk=300;
bool cmp(const pair<pii,int>a, const pair<pii,int>b){
int blka=a.first.first/blk;
int blkb=b.first.first/blk;
if(blka==blkb) return a.first.second < b.first.second;
return blka < blkb;
}
multiset<pii>se;
int counter=0;
void add(int index){
auto it=se.lower_bound({in[index],index});
if(it==se.begin()&&it==se.end()){}
else if(it==se.begin()){
pii a=*it;
counter+=f(a.second,index);
}
else if(it==se.end()){
it--;
pii a=*it;
counter+=f(a.second,index);
}
else{
pii a=*it;
it--;
pii b=*it;
counter-=f(a.second,b.second);
counter+=f(a.second,index);
counter+=f(index,b.second);
}
se.insert({in[index],index});
}
void dlt(int index){
se.erase(se.find({in[index],index}));
auto it=se.lower_bound({in[index],index});
if(it==se.begin()&&it==se.end()){}
else if(it==se.begin()){
pii a=*it;
counter-=f(a.second,index);
}
else if(it==se.end()){
it--;
pii a=*it;
counter-=f(a.second,index);
}
else{
pii a=*it;
it--;
pii b=*it;
counter+=f(a.second,b.second);
counter-=f(a.second,index);
counter-=f(index,b.second);
}
}
void solve(){
int n,m,q;
cin >> n >> m >> q;
int temp,temp2;
for(int x=0;x<n-1;x++){
cin >> temp >> temp2;
adj[temp].push_back(temp2);
adj[temp2].push_back(temp);
}
dfs(1,-1);
int l=1;
int r=1;
int take[m+5];
for(int x=1;x<=m;x++) cin >> take[x];
pair<pii,int>que[q];
for(int x=0;x<q;x++){
cin >> que[x].first.first >> que[x].first.second;
que[x].second=x;
}
sort(que,que+q,cmp);
se.insert({in[take[1]],take[1]});
int ans[q];
for(int x=0;x<q;x++){
int lft=que[x].first.first;
int rgt=que[x].first.second;
int index=que[x].second;
while(l>lft){
l--;
add(take[l]);
}
while(r<rgt){
r++;
add(take[r]);
}
while(l<lft){
dlt(take[l]);
l++;
}
while(r>rgt){
dlt(take[r]);
r--;
}
ans[index]=(counter+f(se.begin()->second,prev(se.end())->second))/2+1;
}
for(int x=0;x<q;x++) cout << ans[x] << "\n";
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
//freopen("in.txt","r",stdin);
//freopen("in.txt","w",stdout);
int t=1;
//cin >> t;
while(t--){
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |