#include <bits/stdc++.h>
using namespace std;
#define int long long
struct fenwick{
int n;vector<int> tree;
fenwick(int n):n(n),tree(n+1){}
int get(int k){
int ans = 0;
while(k){
ans+=tree[k];
k-=k&-k;
}
return ans;
}
void add(int k,int x){
while(k<=n){
tree[k]+=x;
k+=k&-k;
}
}
};
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,m,q;
cin >> n >> m >> q;
vector<vector<int>> adj(n+1);
vector<vector<int>> nadj(n+1);
for(int i=1;i<n;i++){
int a,b;cin>>a>>b;
adj[a].emplace_back(b);
adj[b].emplace_back(a);
}
vector<int> tim(n+1);
vector<int> siz(n+1);
int c = 0;
vector<int> next(n+1);
vector<int> p(n+1);
function<int(int,int)> dfs1 = [&](int x,int par){
p[x]=par;
siz[x]=1;
for(int&i:adj[x])if(i!=par){
nadj[x].emplace_back(i);
siz[x]+=dfs1(i,x);
}
for(int&i:nadj[x])if(siz[i]>siz[nadj[x][0]])swap(i,nadj[x][0]);
return siz[x];
};
dfs1(1,1);
swap(nadj,adj);
function<void(int)> dfs2 = [&](int x){
tim[x] = ++c;
for(int&i:adj[x]){
if(i==adj[x][0])next[i]=next[x];
else next[i]=i;
dfs2(i);
}
};
dfs2(1);
map<int,int> mp;
fenwick cnt(m+1);
auto traverse_path = [&](int a,int b,int x){
if(a==b)return;
vector<pair<int,int>> paths;
while(true){
if(tim[a]>tim[b])swap(a,b);
if(next[a]==next[b]){
paths.emplace_back(tim[a]+1,tim[b]);
break;
}
paths.emplace_back(tim[next[b]],tim[b]);
b = p[next[b]];
}
for(auto [l,r]:paths){
r++;
if(l==r)continue;
for(int y : {l,r}){
if(mp.count(y)==0){
auto itr = mp.lower_bound(y);
itr--;
mp[y]=itr->second;
}
}
while(true){
auto itr = mp.lower_bound(l);
if(itr->first==r)break;
auto nxt = std::next(itr);
cnt.add(itr->second,(itr->first)-(nxt->first));
mp.erase(itr);
}
mp[l]=x;
cnt.add(x,r-l);
}
};
vector<int> checkpoints(m+1);
for(int i=1;i<=m;i++)cin>>checkpoints[i];
vector<int> L(q),R(q);
vector<vector<int>> queries(m+1);
vector<int> ans(q);
for(int i=0;i<q;i++){
cin >> L[i] >> R[i];
if(L[i]!=m)queries[L[i]].emplace_back(i);
}
cnt.add(m,n);
mp[0]=-1;
mp[1]=m;
mp[n+1]=-1;
for(int l=m-1;l;l--){
traverse_path(checkpoints[l],checkpoints[l+1],l);
for(int&idx:queries[l]){
ans[idx] = cnt.get(R[idx]-1);
}
}
for(int&i:ans)cout<<i+1<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
472 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
456 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
472 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
456 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
4 ms |
856 KB |
Output is correct |
31 |
Correct |
4 ms |
664 KB |
Output is correct |
32 |
Correct |
5 ms |
860 KB |
Output is correct |
33 |
Correct |
5 ms |
1012 KB |
Output is correct |
34 |
Correct |
5 ms |
860 KB |
Output is correct |
35 |
Correct |
5 ms |
992 KB |
Output is correct |
36 |
Correct |
5 ms |
860 KB |
Output is correct |
37 |
Correct |
6 ms |
1000 KB |
Output is correct |
38 |
Correct |
2 ms |
856 KB |
Output is correct |
39 |
Correct |
2 ms |
860 KB |
Output is correct |
40 |
Correct |
3 ms |
1368 KB |
Output is correct |
41 |
Correct |
2 ms |
1116 KB |
Output is correct |
42 |
Correct |
2 ms |
1116 KB |
Output is correct |
43 |
Correct |
2 ms |
860 KB |
Output is correct |
44 |
Correct |
3 ms |
1112 KB |
Output is correct |
45 |
Correct |
3 ms |
1116 KB |
Output is correct |
46 |
Correct |
3 ms |
860 KB |
Output is correct |
47 |
Correct |
3 ms |
860 KB |
Output is correct |
48 |
Correct |
3 ms |
1112 KB |
Output is correct |
49 |
Correct |
3 ms |
860 KB |
Output is correct |
50 |
Correct |
3 ms |
860 KB |
Output is correct |
51 |
Correct |
3 ms |
860 KB |
Output is correct |
52 |
Correct |
3 ms |
860 KB |
Output is correct |
53 |
Correct |
4 ms |
928 KB |
Output is correct |
54 |
Correct |
4 ms |
860 KB |
Output is correct |
55 |
Correct |
3 ms |
856 KB |
Output is correct |
56 |
Correct |
1 ms |
600 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
5 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
76 ms |
22788 KB |
Output is correct |
5 |
Correct |
64 ms |
27144 KB |
Output is correct |
6 |
Correct |
75 ms |
30804 KB |
Output is correct |
7 |
Correct |
97 ms |
35924 KB |
Output is correct |
8 |
Correct |
95 ms |
35920 KB |
Output is correct |
9 |
Correct |
98 ms |
35924 KB |
Output is correct |
10 |
Correct |
96 ms |
36064 KB |
Output is correct |
11 |
Correct |
101 ms |
35920 KB |
Output is correct |
12 |
Correct |
96 ms |
35404 KB |
Output is correct |
13 |
Correct |
99 ms |
35408 KB |
Output is correct |
14 |
Correct |
93 ms |
35664 KB |
Output is correct |
15 |
Correct |
42 ms |
29008 KB |
Output is correct |
16 |
Correct |
99 ms |
35668 KB |
Output is correct |
17 |
Correct |
32 ms |
9808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
224 ms |
12884 KB |
Output is correct |
3 |
Correct |
368 ms |
15656 KB |
Output is correct |
4 |
Correct |
287 ms |
16212 KB |
Output is correct |
5 |
Correct |
449 ms |
23732 KB |
Output is correct |
6 |
Correct |
445 ms |
23636 KB |
Output is correct |
7 |
Correct |
477 ms |
23624 KB |
Output is correct |
8 |
Correct |
448 ms |
23636 KB |
Output is correct |
9 |
Correct |
455 ms |
23636 KB |
Output is correct |
10 |
Correct |
468 ms |
23892 KB |
Output is correct |
11 |
Correct |
452 ms |
23632 KB |
Output is correct |
12 |
Correct |
470 ms |
23888 KB |
Output is correct |
13 |
Correct |
457 ms |
24256 KB |
Output is correct |
14 |
Correct |
465 ms |
25628 KB |
Output is correct |
15 |
Correct |
499 ms |
30504 KB |
Output is correct |
16 |
Correct |
420 ms |
24404 KB |
Output is correct |
17 |
Correct |
468 ms |
24464 KB |
Output is correct |
18 |
Correct |
438 ms |
24404 KB |
Output is correct |
19 |
Correct |
257 ms |
20564 KB |
Output is correct |
20 |
Correct |
298 ms |
20496 KB |
Output is correct |
21 |
Correct |
274 ms |
20556 KB |
Output is correct |
22 |
Correct |
258 ms |
20540 KB |
Output is correct |
23 |
Correct |
242 ms |
20816 KB |
Output is correct |
24 |
Correct |
243 ms |
20584 KB |
Output is correct |
25 |
Correct |
264 ms |
20560 KB |
Output is correct |
26 |
Correct |
277 ms |
20560 KB |
Output is correct |
27 |
Correct |
256 ms |
20560 KB |
Output is correct |
28 |
Correct |
272 ms |
20580 KB |
Output is correct |
29 |
Correct |
275 ms |
20816 KB |
Output is correct |
30 |
Correct |
272 ms |
21076 KB |
Output is correct |
31 |
Correct |
257 ms |
21184 KB |
Output is correct |
32 |
Correct |
276 ms |
21876 KB |
Output is correct |
33 |
Correct |
266 ms |
24144 KB |
Output is correct |
34 |
Correct |
256 ms |
27536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
456 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
655 ms |
20084 KB |
Output is correct |
5 |
Correct |
586 ms |
21328 KB |
Output is correct |
6 |
Correct |
643 ms |
26960 KB |
Output is correct |
7 |
Correct |
738 ms |
30288 KB |
Output is correct |
8 |
Correct |
731 ms |
30036 KB |
Output is correct |
9 |
Correct |
745 ms |
30032 KB |
Output is correct |
10 |
Correct |
760 ms |
30016 KB |
Output is correct |
11 |
Correct |
716 ms |
30208 KB |
Output is correct |
12 |
Correct |
701 ms |
30124 KB |
Output is correct |
13 |
Correct |
733 ms |
30288 KB |
Output is correct |
14 |
Correct |
31 ms |
10208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
472 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
456 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
4 ms |
856 KB |
Output is correct |
31 |
Correct |
4 ms |
664 KB |
Output is correct |
32 |
Correct |
5 ms |
860 KB |
Output is correct |
33 |
Correct |
5 ms |
1012 KB |
Output is correct |
34 |
Correct |
5 ms |
860 KB |
Output is correct |
35 |
Correct |
5 ms |
992 KB |
Output is correct |
36 |
Correct |
5 ms |
860 KB |
Output is correct |
37 |
Correct |
6 ms |
1000 KB |
Output is correct |
38 |
Correct |
2 ms |
856 KB |
Output is correct |
39 |
Correct |
2 ms |
860 KB |
Output is correct |
40 |
Correct |
3 ms |
1368 KB |
Output is correct |
41 |
Correct |
2 ms |
1116 KB |
Output is correct |
42 |
Correct |
2 ms |
1116 KB |
Output is correct |
43 |
Correct |
2 ms |
860 KB |
Output is correct |
44 |
Correct |
3 ms |
1112 KB |
Output is correct |
45 |
Correct |
3 ms |
1116 KB |
Output is correct |
46 |
Correct |
3 ms |
860 KB |
Output is correct |
47 |
Correct |
3 ms |
860 KB |
Output is correct |
48 |
Correct |
3 ms |
1112 KB |
Output is correct |
49 |
Correct |
3 ms |
860 KB |
Output is correct |
50 |
Correct |
3 ms |
860 KB |
Output is correct |
51 |
Correct |
3 ms |
860 KB |
Output is correct |
52 |
Correct |
3 ms |
860 KB |
Output is correct |
53 |
Correct |
4 ms |
928 KB |
Output is correct |
54 |
Correct |
4 ms |
860 KB |
Output is correct |
55 |
Correct |
3 ms |
856 KB |
Output is correct |
56 |
Correct |
1 ms |
600 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
5 ms |
860 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
1 ms |
604 KB |
Output is correct |
62 |
Correct |
76 ms |
22788 KB |
Output is correct |
63 |
Correct |
64 ms |
27144 KB |
Output is correct |
64 |
Correct |
75 ms |
30804 KB |
Output is correct |
65 |
Correct |
97 ms |
35924 KB |
Output is correct |
66 |
Correct |
95 ms |
35920 KB |
Output is correct |
67 |
Correct |
98 ms |
35924 KB |
Output is correct |
68 |
Correct |
96 ms |
36064 KB |
Output is correct |
69 |
Correct |
101 ms |
35920 KB |
Output is correct |
70 |
Correct |
96 ms |
35404 KB |
Output is correct |
71 |
Correct |
99 ms |
35408 KB |
Output is correct |
72 |
Correct |
93 ms |
35664 KB |
Output is correct |
73 |
Correct |
42 ms |
29008 KB |
Output is correct |
74 |
Correct |
99 ms |
35668 KB |
Output is correct |
75 |
Correct |
32 ms |
9808 KB |
Output is correct |
76 |
Correct |
1 ms |
344 KB |
Output is correct |
77 |
Correct |
224 ms |
12884 KB |
Output is correct |
78 |
Correct |
368 ms |
15656 KB |
Output is correct |
79 |
Correct |
287 ms |
16212 KB |
Output is correct |
80 |
Correct |
449 ms |
23732 KB |
Output is correct |
81 |
Correct |
445 ms |
23636 KB |
Output is correct |
82 |
Correct |
477 ms |
23624 KB |
Output is correct |
83 |
Correct |
448 ms |
23636 KB |
Output is correct |
84 |
Correct |
455 ms |
23636 KB |
Output is correct |
85 |
Correct |
468 ms |
23892 KB |
Output is correct |
86 |
Correct |
452 ms |
23632 KB |
Output is correct |
87 |
Correct |
470 ms |
23888 KB |
Output is correct |
88 |
Correct |
457 ms |
24256 KB |
Output is correct |
89 |
Correct |
465 ms |
25628 KB |
Output is correct |
90 |
Correct |
499 ms |
30504 KB |
Output is correct |
91 |
Correct |
420 ms |
24404 KB |
Output is correct |
92 |
Correct |
468 ms |
24464 KB |
Output is correct |
93 |
Correct |
438 ms |
24404 KB |
Output is correct |
94 |
Correct |
257 ms |
20564 KB |
Output is correct |
95 |
Correct |
298 ms |
20496 KB |
Output is correct |
96 |
Correct |
274 ms |
20556 KB |
Output is correct |
97 |
Correct |
258 ms |
20540 KB |
Output is correct |
98 |
Correct |
242 ms |
20816 KB |
Output is correct |
99 |
Correct |
243 ms |
20584 KB |
Output is correct |
100 |
Correct |
264 ms |
20560 KB |
Output is correct |
101 |
Correct |
277 ms |
20560 KB |
Output is correct |
102 |
Correct |
256 ms |
20560 KB |
Output is correct |
103 |
Correct |
272 ms |
20580 KB |
Output is correct |
104 |
Correct |
275 ms |
20816 KB |
Output is correct |
105 |
Correct |
272 ms |
21076 KB |
Output is correct |
106 |
Correct |
257 ms |
21184 KB |
Output is correct |
107 |
Correct |
276 ms |
21876 KB |
Output is correct |
108 |
Correct |
266 ms |
24144 KB |
Output is correct |
109 |
Correct |
256 ms |
27536 KB |
Output is correct |
110 |
Correct |
1 ms |
348 KB |
Output is correct |
111 |
Correct |
1 ms |
456 KB |
Output is correct |
112 |
Correct |
1 ms |
604 KB |
Output is correct |
113 |
Correct |
655 ms |
20084 KB |
Output is correct |
114 |
Correct |
586 ms |
21328 KB |
Output is correct |
115 |
Correct |
643 ms |
26960 KB |
Output is correct |
116 |
Correct |
738 ms |
30288 KB |
Output is correct |
117 |
Correct |
731 ms |
30036 KB |
Output is correct |
118 |
Correct |
745 ms |
30032 KB |
Output is correct |
119 |
Correct |
760 ms |
30016 KB |
Output is correct |
120 |
Correct |
716 ms |
30208 KB |
Output is correct |
121 |
Correct |
701 ms |
30124 KB |
Output is correct |
122 |
Correct |
733 ms |
30288 KB |
Output is correct |
123 |
Correct |
31 ms |
10208 KB |
Output is correct |
124 |
Correct |
408 ms |
28600 KB |
Output is correct |
125 |
Correct |
301 ms |
24656 KB |
Output is correct |
126 |
Correct |
508 ms |
29660 KB |
Output is correct |
127 |
Correct |
487 ms |
29692 KB |
Output is correct |
128 |
Correct |
534 ms |
29664 KB |
Output is correct |
129 |
Correct |
500 ms |
29696 KB |
Output is correct |
130 |
Correct |
446 ms |
29708 KB |
Output is correct |
131 |
Correct |
115 ms |
35408 KB |
Output is correct |
132 |
Correct |
110 ms |
37004 KB |
Output is correct |
133 |
Correct |
114 ms |
31572 KB |
Output is correct |
134 |
Correct |
286 ms |
26760 KB |
Output is correct |
135 |
Correct |
307 ms |
26544 KB |
Output is correct |
136 |
Correct |
296 ms |
26676 KB |
Output is correct |
137 |
Correct |
176 ms |
30020 KB |
Output is correct |
138 |
Correct |
170 ms |
30028 KB |
Output is correct |
139 |
Correct |
184 ms |
30024 KB |
Output is correct |
140 |
Correct |
175 ms |
30016 KB |
Output is correct |
141 |
Correct |
177 ms |
30048 KB |
Output is correct |
142 |
Correct |
199 ms |
30052 KB |
Output is correct |
143 |
Correct |
67 ms |
18000 KB |
Output is correct |
144 |
Correct |
452 ms |
29400 KB |
Output is correct |