#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0);
#pragma GCC optimize("Ofast","unroll-loops")
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define int long long
#define lowbit(x) x&-x
const int maxn = 3e5 + 5;
const int INF = 1e9 + 7;
int n, m, q;
vector<int> adj[maxn];
int dep[maxn], in[maxn], dfn = 0;
int arr[maxn][18];
int id[maxn];
void dfs(int pos, int prev){
in[pos] = ++dfn;
id[dfn] = pos;
arr[dfn][0] = dep[pos];
for(auto x : adj[pos]){
if(x == prev) continue;
dep[x] = dep[pos] + 1;
dfs(x, pos);
arr[++dfn][0] = dep[pos];
}
arr[++dfn][0] = dep[pos];
}
int lca(int a, int b){
if(a > b) swap(a, b);
int len = __lg(b - a + 1);
return min(arr[a][len], arr[b - (1 << len) + 1][len]);
}
struct info{
int l, r, id;
info(){}
info(int _l, int _r, int _id) : l(_l), r(_r), id(_id){}
bool operator < (const info& other) const{
return r < other.r;
};
};
vector<info> b[500];
const int block = 320;
int ans[maxn];
int c[maxn], loc[maxn];
int cur = 0;
int path(int a, int b){
return dep[a] + dep[b] - 2 * lca(in[a], in[b]);
}
struct node{
int l, r, val;
} st[maxn];
struct rollback{
int l, r, m;
};
stack<rollback> stk;
void roll_back(){
auto [l, r, m] = stk.top(); stk.pop();
cur -= path(st[l].val, st[r].val);
cur += path(st[l].val, st[m].val) + path(st[m].val, st[r].val);
st[l].r = m, st[r].l = m;
}
void del(int x){
x = loc[x];
int prv = st[x].l, nxt = st[x].r;
stk.push({prv, nxt, x});
cur -= path(st[prv].val, st[x].val) + path(st[x].val, st[nxt].val);
cur += path(st[prv].val, st[nxt].val);
st[prv].r = nxt, st[nxt].l = prv;
}
signed main(void){
fastio;
cin>>n>>m>>q;
for(int i = 0; i < n - 1; i++){
int a, b;
cin>>a>>b;
a--, b--;
adj[a].pb(b);
adj[b].pb(a);
}
dfs(0, 0);
for(int i = 1; (1 << i) <= dfn; i++){
for(int j = 0; j + (1 << i) - 1 <= dfn; j++){
arr[j][i] = min(arr[j][i - 1], arr[j + (1 << (i - 1))][i - 1]);
}
}
for(int i = 0; i < m; i++) cin>>c[i], c[i] --;
for(int i = 0; i < q; i++){
int l, r;
cin>>l>>r;
l--, r--;
b[l / block].pb(info(l, r, i));
}
for(int i = 0; i <= m / block; i++){
sort(b[i].begin(), b[i].end());
}
vector<pll> cpy;
for(int i = 0; i < m; i++) cpy.eb(c[i], i);
sort(cpy.begin(), cpy.end(), [&](pll a, pll b){ return in[a.f] < in[b.f];});
for(int i = 0; i < m; i++){
auto [val, id] = cpy[i];
loc[id] = i;
st[i].val = val;
if(!i) st[i].l = m - 1;
else st[i].l = i - 1;
if(i == m - 1) st[i].r = 0;
else st[i].r = i + 1;
}
for(int i = 0; i < m; i++) cur += path(st[i].val, st[st[i].r].val);
int cl = 0, cr = m - 1;
for(int i = 0; i <= m / block; i++){
if(!b[i].size()) continue;
for(auto [l, r, id] : b[i]){
while(r > cr){
roll_back();
cr++;
}
while(r < cr){
del(cr--);
}
while(cl < l) del(cl++);
ans[id] = cur;
while(cl > 0){
roll_back();
cl--;
}
}
}
for(int i = 0; i < q; i++) cout<<ans[i] / 2 + 1<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
19292 KB |
Output is correct |
2 |
Correct |
3 ms |
19292 KB |
Output is correct |
3 |
Correct |
3 ms |
19292 KB |
Output is correct |
4 |
Correct |
4 ms |
19436 KB |
Output is correct |
5 |
Correct |
4 ms |
19288 KB |
Output is correct |
6 |
Correct |
4 ms |
19292 KB |
Output is correct |
7 |
Correct |
4 ms |
19292 KB |
Output is correct |
8 |
Correct |
4 ms |
19292 KB |
Output is correct |
9 |
Correct |
5 ms |
19288 KB |
Output is correct |
10 |
Correct |
4 ms |
19292 KB |
Output is correct |
11 |
Correct |
6 ms |
19292 KB |
Output is correct |
12 |
Correct |
4 ms |
19292 KB |
Output is correct |
13 |
Correct |
3 ms |
19292 KB |
Output is correct |
14 |
Correct |
4 ms |
19292 KB |
Output is correct |
15 |
Correct |
4 ms |
19548 KB |
Output is correct |
16 |
Correct |
5 ms |
19548 KB |
Output is correct |
17 |
Correct |
5 ms |
19548 KB |
Output is correct |
18 |
Correct |
5 ms |
19292 KB |
Output is correct |
19 |
Correct |
5 ms |
19292 KB |
Output is correct |
20 |
Correct |
5 ms |
19448 KB |
Output is correct |
21 |
Correct |
5 ms |
19292 KB |
Output is correct |
22 |
Correct |
4 ms |
19292 KB |
Output is correct |
23 |
Correct |
5 ms |
19544 KB |
Output is correct |
24 |
Correct |
5 ms |
19292 KB |
Output is correct |
25 |
Correct |
5 ms |
19292 KB |
Output is correct |
26 |
Correct |
5 ms |
19288 KB |
Output is correct |
27 |
Correct |
5 ms |
19292 KB |
Output is correct |
28 |
Correct |
4 ms |
19292 KB |
Output is correct |
29 |
Correct |
5 ms |
19292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
19292 KB |
Output is correct |
2 |
Correct |
3 ms |
19292 KB |
Output is correct |
3 |
Correct |
3 ms |
19292 KB |
Output is correct |
4 |
Correct |
4 ms |
19436 KB |
Output is correct |
5 |
Correct |
4 ms |
19288 KB |
Output is correct |
6 |
Correct |
4 ms |
19292 KB |
Output is correct |
7 |
Correct |
4 ms |
19292 KB |
Output is correct |
8 |
Correct |
4 ms |
19292 KB |
Output is correct |
9 |
Correct |
5 ms |
19288 KB |
Output is correct |
10 |
Correct |
4 ms |
19292 KB |
Output is correct |
11 |
Correct |
6 ms |
19292 KB |
Output is correct |
12 |
Correct |
4 ms |
19292 KB |
Output is correct |
13 |
Correct |
3 ms |
19292 KB |
Output is correct |
14 |
Correct |
4 ms |
19292 KB |
Output is correct |
15 |
Correct |
4 ms |
19548 KB |
Output is correct |
16 |
Correct |
5 ms |
19548 KB |
Output is correct |
17 |
Correct |
5 ms |
19548 KB |
Output is correct |
18 |
Correct |
5 ms |
19292 KB |
Output is correct |
19 |
Correct |
5 ms |
19292 KB |
Output is correct |
20 |
Correct |
5 ms |
19448 KB |
Output is correct |
21 |
Correct |
5 ms |
19292 KB |
Output is correct |
22 |
Correct |
4 ms |
19292 KB |
Output is correct |
23 |
Correct |
5 ms |
19544 KB |
Output is correct |
24 |
Correct |
5 ms |
19292 KB |
Output is correct |
25 |
Correct |
5 ms |
19292 KB |
Output is correct |
26 |
Correct |
5 ms |
19288 KB |
Output is correct |
27 |
Correct |
5 ms |
19292 KB |
Output is correct |
28 |
Correct |
4 ms |
19292 KB |
Output is correct |
29 |
Correct |
5 ms |
19292 KB |
Output is correct |
30 |
Correct |
32 ms |
21596 KB |
Output is correct |
31 |
Correct |
46 ms |
21592 KB |
Output is correct |
32 |
Correct |
69 ms |
21852 KB |
Output is correct |
33 |
Correct |
65 ms |
21852 KB |
Output is correct |
34 |
Correct |
67 ms |
21852 KB |
Output is correct |
35 |
Correct |
6 ms |
21848 KB |
Output is correct |
36 |
Correct |
6 ms |
21852 KB |
Output is correct |
37 |
Correct |
7 ms |
21852 KB |
Output is correct |
38 |
Correct |
62 ms |
21840 KB |
Output is correct |
39 |
Correct |
67 ms |
21852 KB |
Output is correct |
40 |
Correct |
63 ms |
21848 KB |
Output is correct |
41 |
Correct |
6 ms |
21852 KB |
Output is correct |
42 |
Correct |
7 ms |
21852 KB |
Output is correct |
43 |
Correct |
6 ms |
21692 KB |
Output is correct |
44 |
Correct |
65 ms |
21892 KB |
Output is correct |
45 |
Correct |
65 ms |
21880 KB |
Output is correct |
46 |
Correct |
64 ms |
21852 KB |
Output is correct |
47 |
Correct |
6 ms |
21848 KB |
Output is correct |
48 |
Correct |
7 ms |
21620 KB |
Output is correct |
49 |
Correct |
6 ms |
21852 KB |
Output is correct |
50 |
Correct |
64 ms |
21852 KB |
Output is correct |
51 |
Correct |
66 ms |
21852 KB |
Output is correct |
52 |
Correct |
63 ms |
21860 KB |
Output is correct |
53 |
Correct |
75 ms |
21856 KB |
Output is correct |
54 |
Correct |
63 ms |
21852 KB |
Output is correct |
55 |
Correct |
63 ms |
21852 KB |
Output is correct |
56 |
Correct |
49 ms |
19544 KB |
Output is correct |
57 |
Correct |
4 ms |
21596 KB |
Output is correct |
58 |
Correct |
94 ms |
21852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19436 KB |
Output is correct |
2 |
Correct |
4 ms |
19292 KB |
Output is correct |
3 |
Correct |
50 ms |
19552 KB |
Output is correct |
4 |
Execution timed out |
5063 ms |
61408 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19292 KB |
Output is correct |
2 |
Correct |
4934 ms |
52412 KB |
Output is correct |
3 |
Execution timed out |
5005 ms |
56264 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19292 KB |
Output is correct |
2 |
Correct |
6 ms |
19292 KB |
Output is correct |
3 |
Correct |
50 ms |
19548 KB |
Output is correct |
4 |
Execution timed out |
5038 ms |
58172 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
19292 KB |
Output is correct |
2 |
Correct |
3 ms |
19292 KB |
Output is correct |
3 |
Correct |
3 ms |
19292 KB |
Output is correct |
4 |
Correct |
4 ms |
19436 KB |
Output is correct |
5 |
Correct |
4 ms |
19288 KB |
Output is correct |
6 |
Correct |
4 ms |
19292 KB |
Output is correct |
7 |
Correct |
4 ms |
19292 KB |
Output is correct |
8 |
Correct |
4 ms |
19292 KB |
Output is correct |
9 |
Correct |
5 ms |
19288 KB |
Output is correct |
10 |
Correct |
4 ms |
19292 KB |
Output is correct |
11 |
Correct |
6 ms |
19292 KB |
Output is correct |
12 |
Correct |
4 ms |
19292 KB |
Output is correct |
13 |
Correct |
3 ms |
19292 KB |
Output is correct |
14 |
Correct |
4 ms |
19292 KB |
Output is correct |
15 |
Correct |
4 ms |
19548 KB |
Output is correct |
16 |
Correct |
5 ms |
19548 KB |
Output is correct |
17 |
Correct |
5 ms |
19548 KB |
Output is correct |
18 |
Correct |
5 ms |
19292 KB |
Output is correct |
19 |
Correct |
5 ms |
19292 KB |
Output is correct |
20 |
Correct |
5 ms |
19448 KB |
Output is correct |
21 |
Correct |
5 ms |
19292 KB |
Output is correct |
22 |
Correct |
4 ms |
19292 KB |
Output is correct |
23 |
Correct |
5 ms |
19544 KB |
Output is correct |
24 |
Correct |
5 ms |
19292 KB |
Output is correct |
25 |
Correct |
5 ms |
19292 KB |
Output is correct |
26 |
Correct |
5 ms |
19288 KB |
Output is correct |
27 |
Correct |
5 ms |
19292 KB |
Output is correct |
28 |
Correct |
4 ms |
19292 KB |
Output is correct |
29 |
Correct |
5 ms |
19292 KB |
Output is correct |
30 |
Correct |
32 ms |
21596 KB |
Output is correct |
31 |
Correct |
46 ms |
21592 KB |
Output is correct |
32 |
Correct |
69 ms |
21852 KB |
Output is correct |
33 |
Correct |
65 ms |
21852 KB |
Output is correct |
34 |
Correct |
67 ms |
21852 KB |
Output is correct |
35 |
Correct |
6 ms |
21848 KB |
Output is correct |
36 |
Correct |
6 ms |
21852 KB |
Output is correct |
37 |
Correct |
7 ms |
21852 KB |
Output is correct |
38 |
Correct |
62 ms |
21840 KB |
Output is correct |
39 |
Correct |
67 ms |
21852 KB |
Output is correct |
40 |
Correct |
63 ms |
21848 KB |
Output is correct |
41 |
Correct |
6 ms |
21852 KB |
Output is correct |
42 |
Correct |
7 ms |
21852 KB |
Output is correct |
43 |
Correct |
6 ms |
21692 KB |
Output is correct |
44 |
Correct |
65 ms |
21892 KB |
Output is correct |
45 |
Correct |
65 ms |
21880 KB |
Output is correct |
46 |
Correct |
64 ms |
21852 KB |
Output is correct |
47 |
Correct |
6 ms |
21848 KB |
Output is correct |
48 |
Correct |
7 ms |
21620 KB |
Output is correct |
49 |
Correct |
6 ms |
21852 KB |
Output is correct |
50 |
Correct |
64 ms |
21852 KB |
Output is correct |
51 |
Correct |
66 ms |
21852 KB |
Output is correct |
52 |
Correct |
63 ms |
21860 KB |
Output is correct |
53 |
Correct |
75 ms |
21856 KB |
Output is correct |
54 |
Correct |
63 ms |
21852 KB |
Output is correct |
55 |
Correct |
63 ms |
21852 KB |
Output is correct |
56 |
Correct |
49 ms |
19544 KB |
Output is correct |
57 |
Correct |
4 ms |
21596 KB |
Output is correct |
58 |
Correct |
94 ms |
21852 KB |
Output is correct |
59 |
Correct |
3 ms |
19436 KB |
Output is correct |
60 |
Correct |
4 ms |
19292 KB |
Output is correct |
61 |
Correct |
50 ms |
19552 KB |
Output is correct |
62 |
Execution timed out |
5063 ms |
61408 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |