#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")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#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 = 2e5 + 5;
const int INF = 1e9 + 7;
int n, m, q;
vector<int> adj[maxn];
int dep[maxn], in[maxn], dfn = 0;
int succ[maxn][18];
int id[maxn];
void dfs(int pos, int prev){
succ[pos][0] = prev;
for(int i = 1; i < 18; i++) succ[pos][i] = succ[succ[pos][i - 1]][i - 1];
in[pos] = ++dfn;
id[dfn] = pos;
for(auto x : adj[pos]){
if(x == prev) continue;
dep[x] = dep[pos] + 1;
dfs(x, pos);
}
}
void lift(int &b, int steps){
for(int i = 17; i >= 0; i--){
if(steps & (1 << i)) b = succ[b][i];
}
}
int lca(int a, int b){
if(dep[a] > dep[b]) swap(a, b);
lift(b, dep[b] - dep[a]);
if(a == b) return a;
for(int i = 17; i >= 0; i--){
if(succ[a][i] != succ[b][i]) a = succ[a][i], b = succ[b][i];
}
return succ[a][0];
}
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;
};
bool operator > (const info& other) const{
return r > other.r;
}
};
vector<info> b[500];
const int block = 320;
int ans[maxn];
int c[maxn];
int cur = 0;
multiset<int> st;
int path(int a, int b){
a = id[a], b = id[b];
return dep[a] + dep[b] - 2 * dep[lca(a, b)];
}
void add(int x){
x = in[c[x]];
st.insert(x);
if(st.size() == 1) return;
auto it = st.lower_bound(x);
auto itp = (it == st.begin() ? prev(st.end()) : prev(it));
auto itn = (it == prev(st.end()) ? st.begin() : next(it));
cur -= path(*itp, *itn);
cur += path(*it, *itp);
cur += path(*it, *itn);
}
void del(int x){
x = in[c[x]];
auto it = st.lower_bound(x);
auto itp = (it == st.begin() ? prev(st.end()) : prev(it));
auto itn = (it == prev(st.end()) ? st.begin() : next(it));
cur += path(*itp, *itn);
cur -= path(*it, *itp);
cur -= path(*it, *itn);
st.erase(it);
}
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 = 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++){
if(i % 2) sort(b[i].begin(), b[i].end(), greater<info>());
else sort(b[i].begin(), b[i].end());
}
int cl = 0, cr = -1;
for(int i = 0; i <= m / block; i++){
for(auto [l, r, id] : b[i]){
while(cr < r) add(++cr);
while(cl > l) add(--cl);
while(cr > r) del(cr--);
while(cl < l) del(cl++);
ans[id] = cur;
}
}
for(int i = 0; i < q; i++) cout<<ans[i] / 2 + 1<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12120 KB |
Output is correct |
2 |
Correct |
3 ms |
12120 KB |
Output is correct |
3 |
Correct |
3 ms |
12120 KB |
Output is correct |
4 |
Correct |
9 ms |
12124 KB |
Output is correct |
5 |
Correct |
7 ms |
12124 KB |
Output is correct |
6 |
Correct |
8 ms |
12124 KB |
Output is correct |
7 |
Correct |
8 ms |
12340 KB |
Output is correct |
8 |
Correct |
6 ms |
12336 KB |
Output is correct |
9 |
Correct |
13 ms |
12352 KB |
Output is correct |
10 |
Correct |
14 ms |
12348 KB |
Output is correct |
11 |
Correct |
13 ms |
12124 KB |
Output is correct |
12 |
Correct |
3 ms |
12124 KB |
Output is correct |
13 |
Correct |
3 ms |
12124 KB |
Output is correct |
14 |
Correct |
4 ms |
12124 KB |
Output is correct |
15 |
Correct |
11 ms |
12356 KB |
Output is correct |
16 |
Correct |
11 ms |
12124 KB |
Output is correct |
17 |
Correct |
11 ms |
12360 KB |
Output is correct |
18 |
Correct |
13 ms |
12124 KB |
Output is correct |
19 |
Correct |
12 ms |
12124 KB |
Output is correct |
20 |
Correct |
11 ms |
12124 KB |
Output is correct |
21 |
Correct |
13 ms |
12124 KB |
Output is correct |
22 |
Correct |
13 ms |
12120 KB |
Output is correct |
23 |
Correct |
12 ms |
12124 KB |
Output is correct |
24 |
Correct |
15 ms |
12124 KB |
Output is correct |
25 |
Correct |
12 ms |
12348 KB |
Output is correct |
26 |
Correct |
12 ms |
12124 KB |
Output is correct |
27 |
Correct |
8 ms |
12268 KB |
Output is correct |
28 |
Correct |
2 ms |
12124 KB |
Output is correct |
29 |
Correct |
20 ms |
12344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12120 KB |
Output is correct |
2 |
Correct |
3 ms |
12120 KB |
Output is correct |
3 |
Correct |
3 ms |
12120 KB |
Output is correct |
4 |
Correct |
9 ms |
12124 KB |
Output is correct |
5 |
Correct |
7 ms |
12124 KB |
Output is correct |
6 |
Correct |
8 ms |
12124 KB |
Output is correct |
7 |
Correct |
8 ms |
12340 KB |
Output is correct |
8 |
Correct |
6 ms |
12336 KB |
Output is correct |
9 |
Correct |
13 ms |
12352 KB |
Output is correct |
10 |
Correct |
14 ms |
12348 KB |
Output is correct |
11 |
Correct |
13 ms |
12124 KB |
Output is correct |
12 |
Correct |
3 ms |
12124 KB |
Output is correct |
13 |
Correct |
3 ms |
12124 KB |
Output is correct |
14 |
Correct |
4 ms |
12124 KB |
Output is correct |
15 |
Correct |
11 ms |
12356 KB |
Output is correct |
16 |
Correct |
11 ms |
12124 KB |
Output is correct |
17 |
Correct |
11 ms |
12360 KB |
Output is correct |
18 |
Correct |
13 ms |
12124 KB |
Output is correct |
19 |
Correct |
12 ms |
12124 KB |
Output is correct |
20 |
Correct |
11 ms |
12124 KB |
Output is correct |
21 |
Correct |
13 ms |
12124 KB |
Output is correct |
22 |
Correct |
13 ms |
12120 KB |
Output is correct |
23 |
Correct |
12 ms |
12124 KB |
Output is correct |
24 |
Correct |
15 ms |
12124 KB |
Output is correct |
25 |
Correct |
12 ms |
12348 KB |
Output is correct |
26 |
Correct |
12 ms |
12124 KB |
Output is correct |
27 |
Correct |
8 ms |
12268 KB |
Output is correct |
28 |
Correct |
2 ms |
12124 KB |
Output is correct |
29 |
Correct |
20 ms |
12344 KB |
Output is correct |
30 |
Correct |
36 ms |
12380 KB |
Output is correct |
31 |
Correct |
49 ms |
12552 KB |
Output is correct |
32 |
Correct |
59 ms |
12600 KB |
Output is correct |
33 |
Correct |
55 ms |
12380 KB |
Output is correct |
34 |
Correct |
53 ms |
12376 KB |
Output is correct |
35 |
Correct |
10 ms |
12380 KB |
Output is correct |
36 |
Correct |
8 ms |
12376 KB |
Output is correct |
37 |
Correct |
9 ms |
12380 KB |
Output is correct |
38 |
Correct |
44 ms |
12636 KB |
Output is correct |
39 |
Correct |
42 ms |
12668 KB |
Output is correct |
40 |
Correct |
42 ms |
12636 KB |
Output is correct |
41 |
Correct |
7 ms |
12632 KB |
Output is correct |
42 |
Correct |
7 ms |
12632 KB |
Output is correct |
43 |
Correct |
8 ms |
12636 KB |
Output is correct |
44 |
Correct |
46 ms |
12632 KB |
Output is correct |
45 |
Correct |
45 ms |
12380 KB |
Output is correct |
46 |
Correct |
49 ms |
12916 KB |
Output is correct |
47 |
Correct |
7 ms |
12636 KB |
Output is correct |
48 |
Correct |
7 ms |
12632 KB |
Output is correct |
49 |
Correct |
7 ms |
12636 KB |
Output is correct |
50 |
Correct |
48 ms |
12380 KB |
Output is correct |
51 |
Correct |
46 ms |
12376 KB |
Output is correct |
52 |
Correct |
50 ms |
12636 KB |
Output is correct |
53 |
Correct |
47 ms |
12380 KB |
Output is correct |
54 |
Correct |
49 ms |
12380 KB |
Output is correct |
55 |
Correct |
53 ms |
12376 KB |
Output is correct |
56 |
Correct |
21 ms |
12288 KB |
Output is correct |
57 |
Correct |
3 ms |
12380 KB |
Output is correct |
58 |
Correct |
6 ms |
12568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12124 KB |
Output is correct |
2 |
Correct |
7 ms |
12124 KB |
Output is correct |
3 |
Correct |
21 ms |
12376 KB |
Output is correct |
4 |
Execution timed out |
5063 ms |
35156 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12124 KB |
Output is correct |
2 |
Correct |
93 ms |
22540 KB |
Output is correct |
3 |
Correct |
142 ms |
24880 KB |
Output is correct |
4 |
Correct |
125 ms |
25504 KB |
Output is correct |
5 |
Correct |
133 ms |
38100 KB |
Output is correct |
6 |
Correct |
150 ms |
36564 KB |
Output is correct |
7 |
Correct |
144 ms |
34388 KB |
Output is correct |
8 |
Correct |
138 ms |
33912 KB |
Output is correct |
9 |
Correct |
139 ms |
33752 KB |
Output is correct |
10 |
Correct |
142 ms |
33696 KB |
Output is correct |
11 |
Correct |
186 ms |
33616 KB |
Output is correct |
12 |
Correct |
187 ms |
33616 KB |
Output is correct |
13 |
Correct |
219 ms |
34116 KB |
Output is correct |
14 |
Correct |
183 ms |
34936 KB |
Output is correct |
15 |
Correct |
189 ms |
38480 KB |
Output is correct |
16 |
Correct |
225 ms |
36116 KB |
Output is correct |
17 |
Correct |
227 ms |
36156 KB |
Output is correct |
18 |
Correct |
181 ms |
36180 KB |
Output is correct |
19 |
Correct |
106 ms |
37716 KB |
Output is correct |
20 |
Correct |
160 ms |
37736 KB |
Output is correct |
21 |
Correct |
137 ms |
35040 KB |
Output is correct |
22 |
Correct |
173 ms |
34116 KB |
Output is correct |
23 |
Correct |
151 ms |
33648 KB |
Output is correct |
24 |
Correct |
145 ms |
33356 KB |
Output is correct |
25 |
Correct |
164 ms |
33004 KB |
Output is correct |
26 |
Correct |
176 ms |
33248 KB |
Output is correct |
27 |
Correct |
270 ms |
33360 KB |
Output is correct |
28 |
Correct |
296 ms |
33104 KB |
Output is correct |
29 |
Correct |
326 ms |
33276 KB |
Output is correct |
30 |
Correct |
284 ms |
33424 KB |
Output is correct |
31 |
Correct |
308 ms |
33628 KB |
Output is correct |
32 |
Correct |
275 ms |
34128 KB |
Output is correct |
33 |
Correct |
268 ms |
35620 KB |
Output is correct |
34 |
Correct |
259 ms |
38432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12124 KB |
Output is correct |
2 |
Correct |
7 ms |
12124 KB |
Output is correct |
3 |
Correct |
22 ms |
12472 KB |
Output is correct |
4 |
Execution timed out |
5039 ms |
33108 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12120 KB |
Output is correct |
2 |
Correct |
3 ms |
12120 KB |
Output is correct |
3 |
Correct |
3 ms |
12120 KB |
Output is correct |
4 |
Correct |
9 ms |
12124 KB |
Output is correct |
5 |
Correct |
7 ms |
12124 KB |
Output is correct |
6 |
Correct |
8 ms |
12124 KB |
Output is correct |
7 |
Correct |
8 ms |
12340 KB |
Output is correct |
8 |
Correct |
6 ms |
12336 KB |
Output is correct |
9 |
Correct |
13 ms |
12352 KB |
Output is correct |
10 |
Correct |
14 ms |
12348 KB |
Output is correct |
11 |
Correct |
13 ms |
12124 KB |
Output is correct |
12 |
Correct |
3 ms |
12124 KB |
Output is correct |
13 |
Correct |
3 ms |
12124 KB |
Output is correct |
14 |
Correct |
4 ms |
12124 KB |
Output is correct |
15 |
Correct |
11 ms |
12356 KB |
Output is correct |
16 |
Correct |
11 ms |
12124 KB |
Output is correct |
17 |
Correct |
11 ms |
12360 KB |
Output is correct |
18 |
Correct |
13 ms |
12124 KB |
Output is correct |
19 |
Correct |
12 ms |
12124 KB |
Output is correct |
20 |
Correct |
11 ms |
12124 KB |
Output is correct |
21 |
Correct |
13 ms |
12124 KB |
Output is correct |
22 |
Correct |
13 ms |
12120 KB |
Output is correct |
23 |
Correct |
12 ms |
12124 KB |
Output is correct |
24 |
Correct |
15 ms |
12124 KB |
Output is correct |
25 |
Correct |
12 ms |
12348 KB |
Output is correct |
26 |
Correct |
12 ms |
12124 KB |
Output is correct |
27 |
Correct |
8 ms |
12268 KB |
Output is correct |
28 |
Correct |
2 ms |
12124 KB |
Output is correct |
29 |
Correct |
20 ms |
12344 KB |
Output is correct |
30 |
Correct |
36 ms |
12380 KB |
Output is correct |
31 |
Correct |
49 ms |
12552 KB |
Output is correct |
32 |
Correct |
59 ms |
12600 KB |
Output is correct |
33 |
Correct |
55 ms |
12380 KB |
Output is correct |
34 |
Correct |
53 ms |
12376 KB |
Output is correct |
35 |
Correct |
10 ms |
12380 KB |
Output is correct |
36 |
Correct |
8 ms |
12376 KB |
Output is correct |
37 |
Correct |
9 ms |
12380 KB |
Output is correct |
38 |
Correct |
44 ms |
12636 KB |
Output is correct |
39 |
Correct |
42 ms |
12668 KB |
Output is correct |
40 |
Correct |
42 ms |
12636 KB |
Output is correct |
41 |
Correct |
7 ms |
12632 KB |
Output is correct |
42 |
Correct |
7 ms |
12632 KB |
Output is correct |
43 |
Correct |
8 ms |
12636 KB |
Output is correct |
44 |
Correct |
46 ms |
12632 KB |
Output is correct |
45 |
Correct |
45 ms |
12380 KB |
Output is correct |
46 |
Correct |
49 ms |
12916 KB |
Output is correct |
47 |
Correct |
7 ms |
12636 KB |
Output is correct |
48 |
Correct |
7 ms |
12632 KB |
Output is correct |
49 |
Correct |
7 ms |
12636 KB |
Output is correct |
50 |
Correct |
48 ms |
12380 KB |
Output is correct |
51 |
Correct |
46 ms |
12376 KB |
Output is correct |
52 |
Correct |
50 ms |
12636 KB |
Output is correct |
53 |
Correct |
47 ms |
12380 KB |
Output is correct |
54 |
Correct |
49 ms |
12380 KB |
Output is correct |
55 |
Correct |
53 ms |
12376 KB |
Output is correct |
56 |
Correct |
21 ms |
12288 KB |
Output is correct |
57 |
Correct |
3 ms |
12380 KB |
Output is correct |
58 |
Correct |
6 ms |
12568 KB |
Output is correct |
59 |
Correct |
2 ms |
12124 KB |
Output is correct |
60 |
Correct |
7 ms |
12124 KB |
Output is correct |
61 |
Correct |
21 ms |
12376 KB |
Output is correct |
62 |
Execution timed out |
5063 ms |
35156 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |