//LCA
#include <bits/stdc++.h>
using namespace std;
int P[200001][18];
int main(){
int n;
cin>>n;
for(int i = 0;i<=n;i++)P[i][0]= n;
multiset<pair<pair<int,int>,int>> s;
set<int> ind;
for(int i = 0;i<n;i++)ind.insert(i);
for(int i = 0;i<n;i++){
int a,b;cin>>a>>b;
int ma = -1;
pair<pair<int,int>,int> p = {{a,0},0};
auto it = s.lower_bound(p);
if(it!=s.begin()){
it--;
if((*it).first.second>=a){
ma = max(ma,(*it).second);
s.erase(it);
}
}
it = s.lower_bound(p);
while(it!=s.end()&&(*it).first.first<=b){
ma = max(ma,(*it).second);
s.erase(it);
it = s.lower_bound(p);
}
p.second = i;
p.first.second = b;
s.insert(p);
while(!ind.empty()&&(*ind.begin())<=ma){
P[(*ind.begin())][0] = i;
ind.erase(ind.begin());
}
}
for(int i = n;i>=0;i--){
for(int j= 1;j<=17;j++){
P[i][j] = P[P[i][j-1]][j-1];
}
}
int q;cin>>q;
while(q--){
int x,y;cin>>x>>y;
x--;y--;
long long sum = 0;
for(int j = 17;j>=0;j--){
if(P[x][j]<=y){
x = P[x][j];
sum+=(1<<j);
}
}
cout<<sum+1<<endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
210 ms |
22708 KB |
Output is correct |
2 |
Correct |
212 ms |
25992 KB |
Output is correct |
3 |
Correct |
214 ms |
26060 KB |
Output is correct |
4 |
Correct |
215 ms |
26412 KB |
Output is correct |
5 |
Correct |
227 ms |
27592 KB |
Output is correct |
6 |
Correct |
201 ms |
25016 KB |
Output is correct |
7 |
Correct |
210 ms |
25388 KB |
Output is correct |
8 |
Correct |
204 ms |
25668 KB |
Output is correct |
9 |
Correct |
235 ms |
26320 KB |
Output is correct |
10 |
Correct |
216 ms |
25396 KB |
Output is correct |
11 |
Correct |
316 ms |
38692 KB |
Output is correct |
12 |
Correct |
289 ms |
36324 KB |
Output is correct |
13 |
Correct |
302 ms |
36272 KB |
Output is correct |
14 |
Correct |
309 ms |
33568 KB |
Output is correct |
15 |
Correct |
309 ms |
32244 KB |
Output is correct |
16 |
Correct |
1 ms |
308 KB |
Output is correct |
17 |
Correct |
218 ms |
26676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
852 KB |
Output is correct |
2 |
Correct |
15 ms |
960 KB |
Output is correct |
3 |
Correct |
15 ms |
980 KB |
Output is correct |
4 |
Correct |
13 ms |
980 KB |
Output is correct |
5 |
Correct |
13 ms |
980 KB |
Output is correct |
6 |
Correct |
13 ms |
1024 KB |
Output is correct |
7 |
Correct |
13 ms |
980 KB |
Output is correct |
8 |
Correct |
13 ms |
980 KB |
Output is correct |
9 |
Correct |
17 ms |
992 KB |
Output is correct |
10 |
Correct |
15 ms |
964 KB |
Output is correct |
11 |
Correct |
15 ms |
1220 KB |
Output is correct |
12 |
Correct |
14 ms |
1144 KB |
Output is correct |
13 |
Correct |
14 ms |
1148 KB |
Output is correct |
14 |
Correct |
15 ms |
1224 KB |
Output is correct |
15 |
Correct |
14 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
308 KB |
Output is correct |
17 |
Correct |
14 ms |
1016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
852 KB |
Output is correct |
2 |
Correct |
15 ms |
960 KB |
Output is correct |
3 |
Correct |
15 ms |
980 KB |
Output is correct |
4 |
Correct |
13 ms |
980 KB |
Output is correct |
5 |
Correct |
13 ms |
980 KB |
Output is correct |
6 |
Correct |
13 ms |
1024 KB |
Output is correct |
7 |
Correct |
13 ms |
980 KB |
Output is correct |
8 |
Correct |
13 ms |
980 KB |
Output is correct |
9 |
Correct |
17 ms |
992 KB |
Output is correct |
10 |
Correct |
15 ms |
964 KB |
Output is correct |
11 |
Correct |
15 ms |
1220 KB |
Output is correct |
12 |
Correct |
14 ms |
1144 KB |
Output is correct |
13 |
Correct |
14 ms |
1148 KB |
Output is correct |
14 |
Correct |
15 ms |
1224 KB |
Output is correct |
15 |
Correct |
14 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
308 KB |
Output is correct |
17 |
Correct |
14 ms |
1016 KB |
Output is correct |
18 |
Correct |
363 ms |
2264 KB |
Output is correct |
19 |
Correct |
318 ms |
2156 KB |
Output is correct |
20 |
Correct |
372 ms |
2176 KB |
Output is correct |
21 |
Correct |
352 ms |
2152 KB |
Output is correct |
22 |
Correct |
339 ms |
2132 KB |
Output is correct |
23 |
Correct |
320 ms |
2328 KB |
Output is correct |
24 |
Correct |
340 ms |
2104 KB |
Output is correct |
25 |
Correct |
348 ms |
2180 KB |
Output is correct |
26 |
Correct |
342 ms |
2152 KB |
Output is correct |
27 |
Correct |
322 ms |
2004 KB |
Output is correct |
28 |
Correct |
318 ms |
2008 KB |
Output is correct |
29 |
Correct |
329 ms |
1964 KB |
Output is correct |
30 |
Correct |
334 ms |
2180 KB |
Output is correct |
31 |
Correct |
335 ms |
1880 KB |
Output is correct |
32 |
Correct |
325 ms |
2020 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
330 ms |
2720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
235 ms |
23708 KB |
Output is correct |
2 |
Correct |
221 ms |
26948 KB |
Output is correct |
3 |
Correct |
207 ms |
25124 KB |
Output is correct |
4 |
Correct |
203 ms |
24900 KB |
Output is correct |
5 |
Correct |
217 ms |
25716 KB |
Output is correct |
6 |
Correct |
206 ms |
25288 KB |
Output is correct |
7 |
Correct |
215 ms |
25060 KB |
Output is correct |
8 |
Correct |
200 ms |
25112 KB |
Output is correct |
9 |
Correct |
214 ms |
24944 KB |
Output is correct |
10 |
Correct |
234 ms |
26864 KB |
Output is correct |
11 |
Correct |
299 ms |
35916 KB |
Output is correct |
12 |
Correct |
320 ms |
39628 KB |
Output is correct |
13 |
Correct |
293 ms |
35952 KB |
Output is correct |
14 |
Correct |
302 ms |
31184 KB |
Output is correct |
15 |
Correct |
334 ms |
34920 KB |
Output is correct |
16 |
Correct |
1 ms |
304 KB |
Output is correct |
17 |
Correct |
214 ms |
25692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
210 ms |
22708 KB |
Output is correct |
2 |
Correct |
212 ms |
25992 KB |
Output is correct |
3 |
Correct |
214 ms |
26060 KB |
Output is correct |
4 |
Correct |
215 ms |
26412 KB |
Output is correct |
5 |
Correct |
227 ms |
27592 KB |
Output is correct |
6 |
Correct |
201 ms |
25016 KB |
Output is correct |
7 |
Correct |
210 ms |
25388 KB |
Output is correct |
8 |
Correct |
204 ms |
25668 KB |
Output is correct |
9 |
Correct |
235 ms |
26320 KB |
Output is correct |
10 |
Correct |
216 ms |
25396 KB |
Output is correct |
11 |
Correct |
316 ms |
38692 KB |
Output is correct |
12 |
Correct |
289 ms |
36324 KB |
Output is correct |
13 |
Correct |
302 ms |
36272 KB |
Output is correct |
14 |
Correct |
309 ms |
33568 KB |
Output is correct |
15 |
Correct |
309 ms |
32244 KB |
Output is correct |
16 |
Correct |
1 ms |
308 KB |
Output is correct |
17 |
Correct |
218 ms |
26676 KB |
Output is correct |
18 |
Correct |
16 ms |
852 KB |
Output is correct |
19 |
Correct |
15 ms |
960 KB |
Output is correct |
20 |
Correct |
15 ms |
980 KB |
Output is correct |
21 |
Correct |
13 ms |
980 KB |
Output is correct |
22 |
Correct |
13 ms |
980 KB |
Output is correct |
23 |
Correct |
13 ms |
1024 KB |
Output is correct |
24 |
Correct |
13 ms |
980 KB |
Output is correct |
25 |
Correct |
13 ms |
980 KB |
Output is correct |
26 |
Correct |
17 ms |
992 KB |
Output is correct |
27 |
Correct |
15 ms |
964 KB |
Output is correct |
28 |
Correct |
15 ms |
1220 KB |
Output is correct |
29 |
Correct |
14 ms |
1144 KB |
Output is correct |
30 |
Correct |
14 ms |
1148 KB |
Output is correct |
31 |
Correct |
15 ms |
1224 KB |
Output is correct |
32 |
Correct |
14 ms |
1108 KB |
Output is correct |
33 |
Correct |
1 ms |
308 KB |
Output is correct |
34 |
Correct |
14 ms |
1016 KB |
Output is correct |
35 |
Correct |
363 ms |
2264 KB |
Output is correct |
36 |
Correct |
318 ms |
2156 KB |
Output is correct |
37 |
Correct |
372 ms |
2176 KB |
Output is correct |
38 |
Correct |
352 ms |
2152 KB |
Output is correct |
39 |
Correct |
339 ms |
2132 KB |
Output is correct |
40 |
Correct |
320 ms |
2328 KB |
Output is correct |
41 |
Correct |
340 ms |
2104 KB |
Output is correct |
42 |
Correct |
348 ms |
2180 KB |
Output is correct |
43 |
Correct |
342 ms |
2152 KB |
Output is correct |
44 |
Correct |
322 ms |
2004 KB |
Output is correct |
45 |
Correct |
318 ms |
2008 KB |
Output is correct |
46 |
Correct |
329 ms |
1964 KB |
Output is correct |
47 |
Correct |
334 ms |
2180 KB |
Output is correct |
48 |
Correct |
335 ms |
1880 KB |
Output is correct |
49 |
Correct |
325 ms |
2020 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |
51 |
Correct |
330 ms |
2720 KB |
Output is correct |
52 |
Correct |
235 ms |
23708 KB |
Output is correct |
53 |
Correct |
221 ms |
26948 KB |
Output is correct |
54 |
Correct |
207 ms |
25124 KB |
Output is correct |
55 |
Correct |
203 ms |
24900 KB |
Output is correct |
56 |
Correct |
217 ms |
25716 KB |
Output is correct |
57 |
Correct |
206 ms |
25288 KB |
Output is correct |
58 |
Correct |
215 ms |
25060 KB |
Output is correct |
59 |
Correct |
200 ms |
25112 KB |
Output is correct |
60 |
Correct |
214 ms |
24944 KB |
Output is correct |
61 |
Correct |
234 ms |
26864 KB |
Output is correct |
62 |
Correct |
299 ms |
35916 KB |
Output is correct |
63 |
Correct |
320 ms |
39628 KB |
Output is correct |
64 |
Correct |
293 ms |
35952 KB |
Output is correct |
65 |
Correct |
302 ms |
31184 KB |
Output is correct |
66 |
Correct |
334 ms |
34920 KB |
Output is correct |
67 |
Correct |
1 ms |
304 KB |
Output is correct |
68 |
Correct |
214 ms |
25692 KB |
Output is correct |
69 |
Correct |
690 ms |
29224 KB |
Output is correct |
70 |
Correct |
708 ms |
30676 KB |
Output is correct |
71 |
Correct |
668 ms |
28260 KB |
Output is correct |
72 |
Correct |
666 ms |
29788 KB |
Output is correct |
73 |
Correct |
692 ms |
29356 KB |
Output is correct |
74 |
Correct |
697 ms |
30880 KB |
Output is correct |
75 |
Correct |
719 ms |
28748 KB |
Output is correct |
76 |
Correct |
708 ms |
31060 KB |
Output is correct |
77 |
Correct |
690 ms |
28600 KB |
Output is correct |
78 |
Correct |
710 ms |
29184 KB |
Output is correct |
79 |
Correct |
722 ms |
42188 KB |
Output is correct |
80 |
Correct |
698 ms |
39036 KB |
Output is correct |
81 |
Correct |
654 ms |
38740 KB |
Output is correct |
82 |
Correct |
680 ms |
34892 KB |
Output is correct |
83 |
Correct |
639 ms |
33848 KB |
Output is correct |
84 |
Correct |
1 ms |
212 KB |
Output is correct |
85 |
Correct |
567 ms |
28756 KB |
Output is correct |