#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include <bits/stdc++.h>
#define ld double
#define ll int
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define eb emplace_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll array<ll,3>
#define pld pair<ld,ld>
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {cerr<<#a<<": ";for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define cer(a) {cerr<<#a<<": ";for(ll x_ : a) cerr<<x_<< " ";cerr<<endl;}
#define si(a) (ll)(a.size())
using namespace std;
#define maxn 100005
#define lg 21
ll n,m,q;
vector<ll> g[maxn];
ll st[lg][maxn];
ll in[maxn],out[maxn],ti = 0;
ll rev[maxn];
ll dub[maxn];
ll c[maxn];
pll a[maxn];
void dfs(ll u,ll p){
st[0][u] = p;
dub[u] = dub[p] + 1;
in[u] = ++ti;
rev[ti] = u;
for(ll s : g[u]){
if(s==p) continue;
dfs(s,u);
}
out[u] = ti-1;
}
bool intree(ll x,ll y){return in[y]<=in[x]&&out[y]>=out[x];}
ll lca(ll x,ll y){
if(intree(x,y)) return y;
if(intree(y,x)) return x;
for(ll j = lg-1;j>=0;j--){
if(!intree(x,st[j][y])) y = st[j][y];
}
return st[0][y];
}
ll dis(ll x,ll y){
return dub[x] + dub[y] - 2*dub[lca(x,y)];
}
ll d = 300;
bool cmp(pll x,pll y){
if(x[0]/d!=y[0]/d) return x[0]/d<y[0]/d;
return x[1]<y[1];
}
multiset<ll> s;
ll ans = 0;
void add(ll i){
ll x = c[i];
s.insert(in[x]);
auto it = s.lower_bound(in[x]);
ll y = -1,z = -1;
if(it!=s.begin()){
it--;
y = rev[*it];
ans+=dis(x,y);
it++;
}
it++;
if(it!=s.end()){
z = rev[*it];
ans+=dis(x,z);
}
if(z!=-1&&y!=-1) ans-=dis(z,y);
}
void del(ll i){
ll x = c[i];
auto it = s.lower_bound(in[x]);
ll y = -1,z = -1;
if(it!=s.begin()){
it--;
y = rev[*it];
ans-=dis(x,y);
it++;
}
it++;
if(it!=s.end()){
z = rev[*it];
ans-=dis(x,z);
}
if(z!=-1&&y!=-1) ans+=dis(z,y);
it--;
s.erase(it);
}
ll rez[maxn];
int main(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
cin >> n >> m >> q;
for(ll i = 1;i<=n-1;i++){
ll x,y; cin >> x >> y;
g[x].pb(y);
g[y].pb(x);
}
dfs(1,1);
for(ll j = 1;j<lg;j++) for(ll i = 1;i<=n;i++) st[j][i] = st[j-1][st[j-1][i]];
for(ll i = 1;i<=m;i++) cin >> c[i];
for(ll i = 1;i<=q;i++) cin >> a[i][0] >> a[i][1];
for(ll i = 1;i<=q;i++) a[i][2] = i;
sort(a+1,a+1+q,cmp);
ll tl = 1,tr = 1;
add(1);
for(ll i = 1;i<=q;i++){
while(tr<a[i][1]) add(++tr);
while(tl>a[i][0]) add(--tl);
while(tl<a[i][0]) del(tl++);
while(tr>a[i][1]) del(tr--);
ll y = rev[*s.begin()];
ll z = rev[*prev(s.end())];
rez[a[i][2]] = ans + dis(y,z);
}
for(ll i = 1;i<=q;i++){
cout<<rez[i]/2 + 1<<endl;
}
return (0-0);
}
/**
7 6 2
1 2
1 3
2 4
2 5
3 6
3 7
2 3 6 4 5 7
1 3
4 6
**/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Correct |
1 ms |
2772 KB |
Output is correct |
4 |
Correct |
5 ms |
2772 KB |
Output is correct |
5 |
Correct |
4 ms |
2772 KB |
Output is correct |
6 |
Correct |
4 ms |
2772 KB |
Output is correct |
7 |
Correct |
4 ms |
2772 KB |
Output is correct |
8 |
Correct |
3 ms |
2772 KB |
Output is correct |
9 |
Correct |
7 ms |
2772 KB |
Output is correct |
10 |
Correct |
7 ms |
2772 KB |
Output is correct |
11 |
Correct |
7 ms |
2844 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
4 ms |
2772 KB |
Output is correct |
16 |
Correct |
3 ms |
2772 KB |
Output is correct |
17 |
Correct |
4 ms |
2772 KB |
Output is correct |
18 |
Correct |
4 ms |
2772 KB |
Output is correct |
19 |
Correct |
5 ms |
2852 KB |
Output is correct |
20 |
Correct |
4 ms |
2772 KB |
Output is correct |
21 |
Correct |
9 ms |
2852 KB |
Output is correct |
22 |
Correct |
10 ms |
2772 KB |
Output is correct |
23 |
Correct |
7 ms |
2844 KB |
Output is correct |
24 |
Correct |
7 ms |
2812 KB |
Output is correct |
25 |
Correct |
8 ms |
2856 KB |
Output is correct |
26 |
Correct |
10 ms |
2772 KB |
Output is correct |
27 |
Correct |
3 ms |
2772 KB |
Output is correct |
28 |
Correct |
1 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Correct |
1 ms |
2772 KB |
Output is correct |
4 |
Correct |
5 ms |
2772 KB |
Output is correct |
5 |
Correct |
4 ms |
2772 KB |
Output is correct |
6 |
Correct |
4 ms |
2772 KB |
Output is correct |
7 |
Correct |
4 ms |
2772 KB |
Output is correct |
8 |
Correct |
3 ms |
2772 KB |
Output is correct |
9 |
Correct |
7 ms |
2772 KB |
Output is correct |
10 |
Correct |
7 ms |
2772 KB |
Output is correct |
11 |
Correct |
7 ms |
2844 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
4 ms |
2772 KB |
Output is correct |
16 |
Correct |
3 ms |
2772 KB |
Output is correct |
17 |
Correct |
4 ms |
2772 KB |
Output is correct |
18 |
Correct |
4 ms |
2772 KB |
Output is correct |
19 |
Correct |
5 ms |
2852 KB |
Output is correct |
20 |
Correct |
4 ms |
2772 KB |
Output is correct |
21 |
Correct |
9 ms |
2852 KB |
Output is correct |
22 |
Correct |
10 ms |
2772 KB |
Output is correct |
23 |
Correct |
7 ms |
2844 KB |
Output is correct |
24 |
Correct |
7 ms |
2812 KB |
Output is correct |
25 |
Correct |
8 ms |
2856 KB |
Output is correct |
26 |
Correct |
10 ms |
2772 KB |
Output is correct |
27 |
Correct |
3 ms |
2772 KB |
Output is correct |
28 |
Correct |
1 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
47 ms |
3100 KB |
Output is correct |
31 |
Correct |
66 ms |
3076 KB |
Output is correct |
32 |
Correct |
77 ms |
3140 KB |
Output is correct |
33 |
Correct |
77 ms |
3116 KB |
Output is correct |
34 |
Correct |
75 ms |
3180 KB |
Output is correct |
35 |
Correct |
10 ms |
3156 KB |
Output is correct |
36 |
Correct |
12 ms |
3188 KB |
Output is correct |
37 |
Correct |
10 ms |
3156 KB |
Output is correct |
38 |
Correct |
29 ms |
3144 KB |
Output is correct |
39 |
Correct |
25 ms |
3244 KB |
Output is correct |
40 |
Correct |
26 ms |
3248 KB |
Output is correct |
41 |
Correct |
5 ms |
3156 KB |
Output is correct |
42 |
Correct |
5 ms |
3156 KB |
Output is correct |
43 |
Correct |
5 ms |
3156 KB |
Output is correct |
44 |
Correct |
29 ms |
3208 KB |
Output is correct |
45 |
Correct |
30 ms |
3156 KB |
Output is correct |
46 |
Correct |
29 ms |
3216 KB |
Output is correct |
47 |
Correct |
8 ms |
3156 KB |
Output is correct |
48 |
Correct |
5 ms |
3156 KB |
Output is correct |
49 |
Correct |
5 ms |
3112 KB |
Output is correct |
50 |
Correct |
82 ms |
3136 KB |
Output is correct |
51 |
Correct |
80 ms |
3168 KB |
Output is correct |
52 |
Correct |
83 ms |
3156 KB |
Output is correct |
53 |
Correct |
92 ms |
3176 KB |
Output is correct |
54 |
Correct |
83 ms |
3172 KB |
Output is correct |
55 |
Correct |
80 ms |
3172 KB |
Output is correct |
56 |
Correct |
12 ms |
2900 KB |
Output is correct |
57 |
Correct |
2 ms |
3028 KB |
Output is correct |
58 |
Correct |
3 ms |
3028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
3 ms |
2772 KB |
Output is correct |
3 |
Correct |
12 ms |
2924 KB |
Output is correct |
4 |
Execution timed out |
5061 ms |
18176 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2772 KB |
Output is correct |
2 |
Correct |
97 ms |
10144 KB |
Output is correct |
3 |
Correct |
167 ms |
10880 KB |
Output is correct |
4 |
Correct |
111 ms |
11868 KB |
Output is correct |
5 |
Correct |
237 ms |
20660 KB |
Output is correct |
6 |
Correct |
311 ms |
19216 KB |
Output is correct |
7 |
Correct |
351 ms |
17020 KB |
Output is correct |
8 |
Correct |
282 ms |
16400 KB |
Output is correct |
9 |
Correct |
370 ms |
16172 KB |
Output is correct |
10 |
Correct |
281 ms |
16116 KB |
Output is correct |
11 |
Correct |
223 ms |
16104 KB |
Output is correct |
12 |
Correct |
214 ms |
16088 KB |
Output is correct |
13 |
Correct |
190 ms |
16348 KB |
Output is correct |
14 |
Correct |
156 ms |
16580 KB |
Output is correct |
15 |
Correct |
204 ms |
17768 KB |
Output is correct |
16 |
Correct |
255 ms |
18340 KB |
Output is correct |
17 |
Correct |
273 ms |
18328 KB |
Output is correct |
18 |
Correct |
254 ms |
18284 KB |
Output is correct |
19 |
Correct |
82 ms |
20708 KB |
Output is correct |
20 |
Correct |
104 ms |
20644 KB |
Output is correct |
21 |
Correct |
104 ms |
17816 KB |
Output is correct |
22 |
Correct |
124 ms |
17000 KB |
Output is correct |
23 |
Correct |
156 ms |
16556 KB |
Output is correct |
24 |
Correct |
178 ms |
16148 KB |
Output is correct |
25 |
Correct |
249 ms |
16076 KB |
Output is correct |
26 |
Correct |
276 ms |
16124 KB |
Output is correct |
27 |
Correct |
365 ms |
16096 KB |
Output is correct |
28 |
Correct |
229 ms |
16052 KB |
Output is correct |
29 |
Correct |
248 ms |
16092 KB |
Output is correct |
30 |
Correct |
256 ms |
16136 KB |
Output is correct |
31 |
Correct |
235 ms |
16252 KB |
Output is correct |
32 |
Correct |
208 ms |
16460 KB |
Output is correct |
33 |
Correct |
189 ms |
16976 KB |
Output is correct |
34 |
Correct |
179 ms |
17848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Correct |
12 ms |
2900 KB |
Output is correct |
4 |
Execution timed out |
5060 ms |
16016 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Correct |
1 ms |
2772 KB |
Output is correct |
4 |
Correct |
5 ms |
2772 KB |
Output is correct |
5 |
Correct |
4 ms |
2772 KB |
Output is correct |
6 |
Correct |
4 ms |
2772 KB |
Output is correct |
7 |
Correct |
4 ms |
2772 KB |
Output is correct |
8 |
Correct |
3 ms |
2772 KB |
Output is correct |
9 |
Correct |
7 ms |
2772 KB |
Output is correct |
10 |
Correct |
7 ms |
2772 KB |
Output is correct |
11 |
Correct |
7 ms |
2844 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
4 ms |
2772 KB |
Output is correct |
16 |
Correct |
3 ms |
2772 KB |
Output is correct |
17 |
Correct |
4 ms |
2772 KB |
Output is correct |
18 |
Correct |
4 ms |
2772 KB |
Output is correct |
19 |
Correct |
5 ms |
2852 KB |
Output is correct |
20 |
Correct |
4 ms |
2772 KB |
Output is correct |
21 |
Correct |
9 ms |
2852 KB |
Output is correct |
22 |
Correct |
10 ms |
2772 KB |
Output is correct |
23 |
Correct |
7 ms |
2844 KB |
Output is correct |
24 |
Correct |
7 ms |
2812 KB |
Output is correct |
25 |
Correct |
8 ms |
2856 KB |
Output is correct |
26 |
Correct |
10 ms |
2772 KB |
Output is correct |
27 |
Correct |
3 ms |
2772 KB |
Output is correct |
28 |
Correct |
1 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
47 ms |
3100 KB |
Output is correct |
31 |
Correct |
66 ms |
3076 KB |
Output is correct |
32 |
Correct |
77 ms |
3140 KB |
Output is correct |
33 |
Correct |
77 ms |
3116 KB |
Output is correct |
34 |
Correct |
75 ms |
3180 KB |
Output is correct |
35 |
Correct |
10 ms |
3156 KB |
Output is correct |
36 |
Correct |
12 ms |
3188 KB |
Output is correct |
37 |
Correct |
10 ms |
3156 KB |
Output is correct |
38 |
Correct |
29 ms |
3144 KB |
Output is correct |
39 |
Correct |
25 ms |
3244 KB |
Output is correct |
40 |
Correct |
26 ms |
3248 KB |
Output is correct |
41 |
Correct |
5 ms |
3156 KB |
Output is correct |
42 |
Correct |
5 ms |
3156 KB |
Output is correct |
43 |
Correct |
5 ms |
3156 KB |
Output is correct |
44 |
Correct |
29 ms |
3208 KB |
Output is correct |
45 |
Correct |
30 ms |
3156 KB |
Output is correct |
46 |
Correct |
29 ms |
3216 KB |
Output is correct |
47 |
Correct |
8 ms |
3156 KB |
Output is correct |
48 |
Correct |
5 ms |
3156 KB |
Output is correct |
49 |
Correct |
5 ms |
3112 KB |
Output is correct |
50 |
Correct |
82 ms |
3136 KB |
Output is correct |
51 |
Correct |
80 ms |
3168 KB |
Output is correct |
52 |
Correct |
83 ms |
3156 KB |
Output is correct |
53 |
Correct |
92 ms |
3176 KB |
Output is correct |
54 |
Correct |
83 ms |
3172 KB |
Output is correct |
55 |
Correct |
80 ms |
3172 KB |
Output is correct |
56 |
Correct |
12 ms |
2900 KB |
Output is correct |
57 |
Correct |
2 ms |
3028 KB |
Output is correct |
58 |
Correct |
3 ms |
3028 KB |
Output is correct |
59 |
Correct |
2 ms |
2772 KB |
Output is correct |
60 |
Correct |
3 ms |
2772 KB |
Output is correct |
61 |
Correct |
12 ms |
2924 KB |
Output is correct |
62 |
Execution timed out |
5061 ms |
18176 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |