#include<bits/stdc++.h>
using namespace std;
const int N = 1e5;
vector <int> e[N];
vector <pair<int,int>> q2[N];
map <int,pair<int,int>> f;
int sub[N];
int top[N];
int st[N],cnt = 0;
int c[N];
int d[N];
int pr[N];
int ans[N];
int st2[N];
int n,m,q;
void dfs(int node, int p){
sub[node] = 1;
pr[node] = p;
if(p == -1){
d[node] = 0;
}else{
d[node] = d[p] + 1;
}
for(auto i:e[node]){
if(i == p)continue;
dfs(i, node);
sub[node]+=sub[i];
}
}
void dfs2(int node, int p){
int mxid = -1;
for(auto i:e[node]){
if(i == p)continue;
if(mxid == -1 || sub[mxid] < sub[i]){
mxid = i;
}
}
st[node] = cnt++;
st2[cnt - 1] = node;
if(mxid != -1){
top[mxid] = top[node];
dfs2(mxid, node);
}
for(auto i:e[node]){
if(i == p || i == mxid)continue;
top[i] = i;
dfs2(i, node);
}
}
int v[N];
void add(int u,int w,int id){
u = st[u];
w = st[w];
if(u > w)swap(u,w);
///segment override
while(1){
auto x = f.lower_bound(u);
if(x == f.end() || x->first > w){
break;
}
int l = min(x->first,u),r = max(x->second.first,w),id2 = x->second.second;
f.erase(x);
if(l < u){
f[l] = {u - 1,id2};
}
if(w < r){
f[w + 1] = {r,id2};
}
}
auto x = f.lower_bound(u);
if(x != f.begin() && prev(x)->second.first >= u){
x = prev(x);
int l = min(x->first,u),r = max(x->second.first,w),id2 = x->second.second;
f.erase(x);
if(l < u){
f[l] = {u - 1,id2};
}
if(w < r){
f[w + 1] = {r,id2};
}
}
f[u] = {w,id};
}
int get(int pos){
int r = 0;
for(auto i:f){
if(i.second.second >= pos){
r+=i.second.first - i.first + 1;
}
//cout<<i.first<<' '<<i.second.first<<' '<<i.second.second<<'\n';
}
//cout<<'\n';
return r;
}
int main(){
//ios_base::sync_with_stdio(0);
//cin.tie(0);
cin>>n>>m>>q;
for(int i = 0;i < n - 1;i++){
int a,b;
cin>>a>>b;
a--;b--;
e[a].push_back(b);
e[b].push_back(a);
v[i] = -1;
}
v[n - 1] = -1;
dfs(0, -1);
top[0] = 0;
dfs2(0, -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;
if(l == r){
ans[i] = 1;
continue;
}
q2[r - 1].push_back({l - 1,i});
}
for(int i = 1;i < m;i++){
///add
int u = c[i],w = c[i - 1];
while(top[u] != top[w]){
if(d[top[u]] > d[top[w]])swap(u,w);
//cout<<w<<' '<<top[w]<<' '<<i - 1<<'\n';
add(w,top[w],i - 1);
w = pr[top[w]];
}
//cout<<u<<' '<<w<<' '<<i - 1<<'\n';
add(u,w,i - 1);
get(0);
///queries
for(auto j:q2[i]){
ans[j.second] = get(j.first);
}
}
for(int i = 0;i < q;i++){
cout<<ans[i]<<'\n';
}
return 0;
}
/**
8 3 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
2 4 7
1 3
**/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8284 KB |
Output is correct |
2 |
Correct |
3 ms |
8284 KB |
Output is correct |
3 |
Correct |
3 ms |
8364 KB |
Output is correct |
4 |
Correct |
4 ms |
8692 KB |
Output is correct |
5 |
Correct |
4 ms |
8284 KB |
Output is correct |
6 |
Correct |
3 ms |
8284 KB |
Output is correct |
7 |
Correct |
3 ms |
8284 KB |
Output is correct |
8 |
Correct |
4 ms |
8280 KB |
Output is correct |
9 |
Correct |
4 ms |
8284 KB |
Output is correct |
10 |
Correct |
3 ms |
8284 KB |
Output is correct |
11 |
Correct |
4 ms |
8280 KB |
Output is correct |
12 |
Correct |
4 ms |
8284 KB |
Output is correct |
13 |
Correct |
4 ms |
8284 KB |
Output is correct |
14 |
Correct |
3 ms |
8284 KB |
Output is correct |
15 |
Correct |
3 ms |
8284 KB |
Output is correct |
16 |
Correct |
5 ms |
8284 KB |
Output is correct |
17 |
Correct |
3 ms |
8284 KB |
Output is correct |
18 |
Correct |
3 ms |
8280 KB |
Output is correct |
19 |
Correct |
3 ms |
8284 KB |
Output is correct |
20 |
Correct |
3 ms |
8284 KB |
Output is correct |
21 |
Correct |
3 ms |
8396 KB |
Output is correct |
22 |
Correct |
3 ms |
8284 KB |
Output is correct |
23 |
Correct |
3 ms |
8284 KB |
Output is correct |
24 |
Correct |
4 ms |
8284 KB |
Output is correct |
25 |
Correct |
4 ms |
8488 KB |
Output is correct |
26 |
Correct |
3 ms |
8280 KB |
Output is correct |
27 |
Correct |
3 ms |
8284 KB |
Output is correct |
28 |
Correct |
3 ms |
8284 KB |
Output is correct |
29 |
Correct |
3 ms |
8284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8284 KB |
Output is correct |
2 |
Correct |
3 ms |
8284 KB |
Output is correct |
3 |
Correct |
3 ms |
8364 KB |
Output is correct |
4 |
Correct |
4 ms |
8692 KB |
Output is correct |
5 |
Correct |
4 ms |
8284 KB |
Output is correct |
6 |
Correct |
3 ms |
8284 KB |
Output is correct |
7 |
Correct |
3 ms |
8284 KB |
Output is correct |
8 |
Correct |
4 ms |
8280 KB |
Output is correct |
9 |
Correct |
4 ms |
8284 KB |
Output is correct |
10 |
Correct |
3 ms |
8284 KB |
Output is correct |
11 |
Correct |
4 ms |
8280 KB |
Output is correct |
12 |
Correct |
4 ms |
8284 KB |
Output is correct |
13 |
Correct |
4 ms |
8284 KB |
Output is correct |
14 |
Correct |
3 ms |
8284 KB |
Output is correct |
15 |
Correct |
3 ms |
8284 KB |
Output is correct |
16 |
Correct |
5 ms |
8284 KB |
Output is correct |
17 |
Correct |
3 ms |
8284 KB |
Output is correct |
18 |
Correct |
3 ms |
8280 KB |
Output is correct |
19 |
Correct |
3 ms |
8284 KB |
Output is correct |
20 |
Correct |
3 ms |
8284 KB |
Output is correct |
21 |
Correct |
3 ms |
8396 KB |
Output is correct |
22 |
Correct |
3 ms |
8284 KB |
Output is correct |
23 |
Correct |
3 ms |
8284 KB |
Output is correct |
24 |
Correct |
4 ms |
8284 KB |
Output is correct |
25 |
Correct |
4 ms |
8488 KB |
Output is correct |
26 |
Correct |
3 ms |
8280 KB |
Output is correct |
27 |
Correct |
3 ms |
8284 KB |
Output is correct |
28 |
Correct |
3 ms |
8284 KB |
Output is correct |
29 |
Correct |
3 ms |
8284 KB |
Output is correct |
30 |
Correct |
14 ms |
8540 KB |
Output is correct |
31 |
Correct |
16 ms |
8536 KB |
Output is correct |
32 |
Correct |
22 ms |
8536 KB |
Output is correct |
33 |
Correct |
25 ms |
8792 KB |
Output is correct |
34 |
Correct |
22 ms |
8540 KB |
Output is correct |
35 |
Correct |
21 ms |
8588 KB |
Output is correct |
36 |
Correct |
18 ms |
8540 KB |
Output is correct |
37 |
Correct |
19 ms |
8536 KB |
Output is correct |
38 |
Correct |
6 ms |
8536 KB |
Output is correct |
39 |
Correct |
7 ms |
8792 KB |
Output is correct |
40 |
Correct |
6 ms |
8540 KB |
Output is correct |
41 |
Correct |
5 ms |
8540 KB |
Output is correct |
42 |
Correct |
6 ms |
8540 KB |
Output is correct |
43 |
Correct |
6 ms |
8504 KB |
Output is correct |
44 |
Correct |
7 ms |
8540 KB |
Output is correct |
45 |
Correct |
7 ms |
8800 KB |
Output is correct |
46 |
Correct |
7 ms |
8540 KB |
Output is correct |
47 |
Correct |
8 ms |
8536 KB |
Output is correct |
48 |
Correct |
8 ms |
8540 KB |
Output is correct |
49 |
Correct |
7 ms |
8540 KB |
Output is correct |
50 |
Correct |
25 ms |
8792 KB |
Output is correct |
51 |
Correct |
22 ms |
8540 KB |
Output is correct |
52 |
Correct |
23 ms |
8712 KB |
Output is correct |
53 |
Correct |
23 ms |
8680 KB |
Output is correct |
54 |
Correct |
28 ms |
8536 KB |
Output is correct |
55 |
Correct |
29 ms |
8536 KB |
Output is correct |
56 |
Correct |
4 ms |
8540 KB |
Output is correct |
57 |
Correct |
5 ms |
8536 KB |
Output is correct |
58 |
Correct |
10 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8280 KB |
Output is correct |
2 |
Correct |
4 ms |
8280 KB |
Output is correct |
3 |
Correct |
4 ms |
8540 KB |
Output is correct |
4 |
Correct |
133 ms |
18672 KB |
Output is correct |
5 |
Correct |
136 ms |
20900 KB |
Output is correct |
6 |
Correct |
143 ms |
22356 KB |
Output is correct |
7 |
Correct |
188 ms |
24952 KB |
Output is correct |
8 |
Correct |
204 ms |
25184 KB |
Output is correct |
9 |
Correct |
178 ms |
24812 KB |
Output is correct |
10 |
Correct |
183 ms |
24848 KB |
Output is correct |
11 |
Correct |
193 ms |
25160 KB |
Output is correct |
12 |
Correct |
4992 ms |
24432 KB |
Output is correct |
13 |
Correct |
4973 ms |
24436 KB |
Output is correct |
14 |
Execution timed out |
5038 ms |
23924 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8280 KB |
Output is correct |
2 |
Correct |
819 ms |
12240 KB |
Output is correct |
3 |
Correct |
1242 ms |
14368 KB |
Output is correct |
4 |
Correct |
1014 ms |
14164 KB |
Output is correct |
5 |
Correct |
420 ms |
16764 KB |
Output is correct |
6 |
Correct |
451 ms |
16840 KB |
Output is correct |
7 |
Correct |
454 ms |
16772 KB |
Output is correct |
8 |
Correct |
434 ms |
16944 KB |
Output is correct |
9 |
Correct |
506 ms |
17244 KB |
Output is correct |
10 |
Correct |
644 ms |
17248 KB |
Output is correct |
11 |
Correct |
1126 ms |
16836 KB |
Output is correct |
12 |
Correct |
2340 ms |
17000 KB |
Output is correct |
13 |
Execution timed out |
5041 ms |
16916 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8284 KB |
Output is correct |
2 |
Correct |
3 ms |
8284 KB |
Output is correct |
3 |
Correct |
5 ms |
8284 KB |
Output is correct |
4 |
Execution timed out |
5025 ms |
13912 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8284 KB |
Output is correct |
2 |
Correct |
3 ms |
8284 KB |
Output is correct |
3 |
Correct |
3 ms |
8364 KB |
Output is correct |
4 |
Correct |
4 ms |
8692 KB |
Output is correct |
5 |
Correct |
4 ms |
8284 KB |
Output is correct |
6 |
Correct |
3 ms |
8284 KB |
Output is correct |
7 |
Correct |
3 ms |
8284 KB |
Output is correct |
8 |
Correct |
4 ms |
8280 KB |
Output is correct |
9 |
Correct |
4 ms |
8284 KB |
Output is correct |
10 |
Correct |
3 ms |
8284 KB |
Output is correct |
11 |
Correct |
4 ms |
8280 KB |
Output is correct |
12 |
Correct |
4 ms |
8284 KB |
Output is correct |
13 |
Correct |
4 ms |
8284 KB |
Output is correct |
14 |
Correct |
3 ms |
8284 KB |
Output is correct |
15 |
Correct |
3 ms |
8284 KB |
Output is correct |
16 |
Correct |
5 ms |
8284 KB |
Output is correct |
17 |
Correct |
3 ms |
8284 KB |
Output is correct |
18 |
Correct |
3 ms |
8280 KB |
Output is correct |
19 |
Correct |
3 ms |
8284 KB |
Output is correct |
20 |
Correct |
3 ms |
8284 KB |
Output is correct |
21 |
Correct |
3 ms |
8396 KB |
Output is correct |
22 |
Correct |
3 ms |
8284 KB |
Output is correct |
23 |
Correct |
3 ms |
8284 KB |
Output is correct |
24 |
Correct |
4 ms |
8284 KB |
Output is correct |
25 |
Correct |
4 ms |
8488 KB |
Output is correct |
26 |
Correct |
3 ms |
8280 KB |
Output is correct |
27 |
Correct |
3 ms |
8284 KB |
Output is correct |
28 |
Correct |
3 ms |
8284 KB |
Output is correct |
29 |
Correct |
3 ms |
8284 KB |
Output is correct |
30 |
Correct |
14 ms |
8540 KB |
Output is correct |
31 |
Correct |
16 ms |
8536 KB |
Output is correct |
32 |
Correct |
22 ms |
8536 KB |
Output is correct |
33 |
Correct |
25 ms |
8792 KB |
Output is correct |
34 |
Correct |
22 ms |
8540 KB |
Output is correct |
35 |
Correct |
21 ms |
8588 KB |
Output is correct |
36 |
Correct |
18 ms |
8540 KB |
Output is correct |
37 |
Correct |
19 ms |
8536 KB |
Output is correct |
38 |
Correct |
6 ms |
8536 KB |
Output is correct |
39 |
Correct |
7 ms |
8792 KB |
Output is correct |
40 |
Correct |
6 ms |
8540 KB |
Output is correct |
41 |
Correct |
5 ms |
8540 KB |
Output is correct |
42 |
Correct |
6 ms |
8540 KB |
Output is correct |
43 |
Correct |
6 ms |
8504 KB |
Output is correct |
44 |
Correct |
7 ms |
8540 KB |
Output is correct |
45 |
Correct |
7 ms |
8800 KB |
Output is correct |
46 |
Correct |
7 ms |
8540 KB |
Output is correct |
47 |
Correct |
8 ms |
8536 KB |
Output is correct |
48 |
Correct |
8 ms |
8540 KB |
Output is correct |
49 |
Correct |
7 ms |
8540 KB |
Output is correct |
50 |
Correct |
25 ms |
8792 KB |
Output is correct |
51 |
Correct |
22 ms |
8540 KB |
Output is correct |
52 |
Correct |
23 ms |
8712 KB |
Output is correct |
53 |
Correct |
23 ms |
8680 KB |
Output is correct |
54 |
Correct |
28 ms |
8536 KB |
Output is correct |
55 |
Correct |
29 ms |
8536 KB |
Output is correct |
56 |
Correct |
4 ms |
8540 KB |
Output is correct |
57 |
Correct |
5 ms |
8536 KB |
Output is correct |
58 |
Correct |
10 ms |
8540 KB |
Output is correct |
59 |
Correct |
2 ms |
8280 KB |
Output is correct |
60 |
Correct |
4 ms |
8280 KB |
Output is correct |
61 |
Correct |
4 ms |
8540 KB |
Output is correct |
62 |
Correct |
133 ms |
18672 KB |
Output is correct |
63 |
Correct |
136 ms |
20900 KB |
Output is correct |
64 |
Correct |
143 ms |
22356 KB |
Output is correct |
65 |
Correct |
188 ms |
24952 KB |
Output is correct |
66 |
Correct |
204 ms |
25184 KB |
Output is correct |
67 |
Correct |
178 ms |
24812 KB |
Output is correct |
68 |
Correct |
183 ms |
24848 KB |
Output is correct |
69 |
Correct |
193 ms |
25160 KB |
Output is correct |
70 |
Correct |
4992 ms |
24432 KB |
Output is correct |
71 |
Correct |
4973 ms |
24436 KB |
Output is correct |
72 |
Execution timed out |
5038 ms |
23924 KB |
Time limit exceeded |
73 |
Halted |
0 ms |
0 KB |
- |