#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define ll long long
#define oo 1e9 + 9
#define pii pair<int, int>
using namespace std;
const int MAX = 2e5 + 5;
int n, q;
pii arr[MAX];
int st[20][MAX];
bool checkRange(pii a, pii b){
return a.first <= b.second && a.second >= b.first;
}
void calcMaxR(){
set<pii> s;
int l = 0;
for (int i = 0; i < n; i++)
{
while(!s.empty()){
auto itr1 = s.upper_bound({arr[i].first, oo});
auto itr2 = s.upper_bound({arr[i].second, oo});
if(itr1 != s.begin()) --itr1;
if(itr2 != s.begin()) --itr2;
if((checkRange(arr[i], (*itr1))) || checkRange(arr[i], (*itr2))){
st[0][l] = i;
s.erase(arr[l]);
++l;
}
else{
break;
}
}
s.insert(arr[i]);
}
for (int j = l; j <= n; j++)
{
st[0][j] = n;
}
}
void build(){
for (int j = 1; j < 20; j++)
{
for (int i = 0; i <= n; i++)
{
st[j][i] = st[j - 1][st[j - 1][i]];
}
}
}
int ask(int l, int r){
int ans = 1;
for(int j = 19; j >= 0; --j){
if(st[j][l] <= r){
l = st[j][l];
ans += (1 << j);
}
}
return ans;
}
int main(){
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> arr[i].first >> arr[i].second;
}
calcMaxR();
build();
cin >> q;
while(q--){
int l, r; cin >> l >> r;
--l; --r;
cout << ask(l, r) << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
154 ms |
16804 KB |
Output is correct |
2 |
Correct |
155 ms |
16540 KB |
Output is correct |
3 |
Correct |
154 ms |
16560 KB |
Output is correct |
4 |
Correct |
155 ms |
16796 KB |
Output is correct |
5 |
Correct |
166 ms |
17484 KB |
Output is correct |
6 |
Correct |
151 ms |
16032 KB |
Output is correct |
7 |
Correct |
150 ms |
16256 KB |
Output is correct |
8 |
Correct |
151 ms |
16376 KB |
Output is correct |
9 |
Correct |
163 ms |
16784 KB |
Output is correct |
10 |
Correct |
178 ms |
16252 KB |
Output is correct |
11 |
Correct |
295 ms |
26360 KB |
Output is correct |
12 |
Correct |
258 ms |
27724 KB |
Output is correct |
13 |
Correct |
237 ms |
27736 KB |
Output is correct |
14 |
Correct |
266 ms |
25548 KB |
Output is correct |
15 |
Correct |
248 ms |
23836 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
156 ms |
20756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
864 KB |
Output is correct |
2 |
Correct |
13 ms |
828 KB |
Output is correct |
3 |
Correct |
12 ms |
712 KB |
Output is correct |
4 |
Correct |
13 ms |
852 KB |
Output is correct |
5 |
Correct |
12 ms |
728 KB |
Output is correct |
6 |
Correct |
19 ms |
852 KB |
Output is correct |
7 |
Correct |
16 ms |
832 KB |
Output is correct |
8 |
Correct |
14 ms |
848 KB |
Output is correct |
9 |
Correct |
13 ms |
724 KB |
Output is correct |
10 |
Correct |
16 ms |
832 KB |
Output is correct |
11 |
Correct |
14 ms |
1080 KB |
Output is correct |
12 |
Correct |
12 ms |
980 KB |
Output is correct |
13 |
Correct |
14 ms |
1048 KB |
Output is correct |
14 |
Correct |
14 ms |
944 KB |
Output is correct |
15 |
Correct |
14 ms |
852 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
13 ms |
964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
864 KB |
Output is correct |
2 |
Correct |
13 ms |
828 KB |
Output is correct |
3 |
Correct |
12 ms |
712 KB |
Output is correct |
4 |
Correct |
13 ms |
852 KB |
Output is correct |
5 |
Correct |
12 ms |
728 KB |
Output is correct |
6 |
Correct |
19 ms |
852 KB |
Output is correct |
7 |
Correct |
16 ms |
832 KB |
Output is correct |
8 |
Correct |
14 ms |
848 KB |
Output is correct |
9 |
Correct |
13 ms |
724 KB |
Output is correct |
10 |
Correct |
16 ms |
832 KB |
Output is correct |
11 |
Correct |
14 ms |
1080 KB |
Output is correct |
12 |
Correct |
12 ms |
980 KB |
Output is correct |
13 |
Correct |
14 ms |
1048 KB |
Output is correct |
14 |
Correct |
14 ms |
944 KB |
Output is correct |
15 |
Correct |
14 ms |
852 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
13 ms |
964 KB |
Output is correct |
18 |
Correct |
367 ms |
1636 KB |
Output is correct |
19 |
Correct |
343 ms |
1668 KB |
Output is correct |
20 |
Correct |
372 ms |
1692 KB |
Output is correct |
21 |
Correct |
334 ms |
1720 KB |
Output is correct |
22 |
Correct |
345 ms |
1680 KB |
Output is correct |
23 |
Correct |
343 ms |
1552 KB |
Output is correct |
24 |
Correct |
359 ms |
1612 KB |
Output is correct |
25 |
Correct |
377 ms |
1612 KB |
Output is correct |
26 |
Correct |
349 ms |
1676 KB |
Output is correct |
27 |
Correct |
336 ms |
1748 KB |
Output is correct |
28 |
Correct |
324 ms |
1308 KB |
Output is correct |
29 |
Correct |
349 ms |
1356 KB |
Output is correct |
30 |
Correct |
334 ms |
1356 KB |
Output is correct |
31 |
Correct |
328 ms |
1180 KB |
Output is correct |
32 |
Correct |
333 ms |
1276 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
334 ms |
1888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
159 ms |
17488 KB |
Output is correct |
2 |
Correct |
177 ms |
17296 KB |
Output is correct |
3 |
Correct |
152 ms |
16036 KB |
Output is correct |
4 |
Correct |
143 ms |
15884 KB |
Output is correct |
5 |
Correct |
146 ms |
16336 KB |
Output is correct |
6 |
Correct |
150 ms |
16096 KB |
Output is correct |
7 |
Correct |
147 ms |
15864 KB |
Output is correct |
8 |
Correct |
152 ms |
16108 KB |
Output is correct |
9 |
Correct |
159 ms |
15908 KB |
Output is correct |
10 |
Correct |
175 ms |
17192 KB |
Output is correct |
11 |
Correct |
243 ms |
24628 KB |
Output is correct |
12 |
Correct |
264 ms |
30128 KB |
Output is correct |
13 |
Correct |
267 ms |
27504 KB |
Output is correct |
14 |
Correct |
281 ms |
23852 KB |
Output is correct |
15 |
Correct |
286 ms |
25940 KB |
Output is correct |
16 |
Correct |
1 ms |
308 KB |
Output is correct |
17 |
Correct |
152 ms |
19916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
154 ms |
16804 KB |
Output is correct |
2 |
Correct |
155 ms |
16540 KB |
Output is correct |
3 |
Correct |
154 ms |
16560 KB |
Output is correct |
4 |
Correct |
155 ms |
16796 KB |
Output is correct |
5 |
Correct |
166 ms |
17484 KB |
Output is correct |
6 |
Correct |
151 ms |
16032 KB |
Output is correct |
7 |
Correct |
150 ms |
16256 KB |
Output is correct |
8 |
Correct |
151 ms |
16376 KB |
Output is correct |
9 |
Correct |
163 ms |
16784 KB |
Output is correct |
10 |
Correct |
178 ms |
16252 KB |
Output is correct |
11 |
Correct |
295 ms |
26360 KB |
Output is correct |
12 |
Correct |
258 ms |
27724 KB |
Output is correct |
13 |
Correct |
237 ms |
27736 KB |
Output is correct |
14 |
Correct |
266 ms |
25548 KB |
Output is correct |
15 |
Correct |
248 ms |
23836 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
156 ms |
20756 KB |
Output is correct |
18 |
Correct |
14 ms |
864 KB |
Output is correct |
19 |
Correct |
13 ms |
828 KB |
Output is correct |
20 |
Correct |
12 ms |
712 KB |
Output is correct |
21 |
Correct |
13 ms |
852 KB |
Output is correct |
22 |
Correct |
12 ms |
728 KB |
Output is correct |
23 |
Correct |
19 ms |
852 KB |
Output is correct |
24 |
Correct |
16 ms |
832 KB |
Output is correct |
25 |
Correct |
14 ms |
848 KB |
Output is correct |
26 |
Correct |
13 ms |
724 KB |
Output is correct |
27 |
Correct |
16 ms |
832 KB |
Output is correct |
28 |
Correct |
14 ms |
1080 KB |
Output is correct |
29 |
Correct |
12 ms |
980 KB |
Output is correct |
30 |
Correct |
14 ms |
1048 KB |
Output is correct |
31 |
Correct |
14 ms |
944 KB |
Output is correct |
32 |
Correct |
14 ms |
852 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
13 ms |
964 KB |
Output is correct |
35 |
Correct |
367 ms |
1636 KB |
Output is correct |
36 |
Correct |
343 ms |
1668 KB |
Output is correct |
37 |
Correct |
372 ms |
1692 KB |
Output is correct |
38 |
Correct |
334 ms |
1720 KB |
Output is correct |
39 |
Correct |
345 ms |
1680 KB |
Output is correct |
40 |
Correct |
343 ms |
1552 KB |
Output is correct |
41 |
Correct |
359 ms |
1612 KB |
Output is correct |
42 |
Correct |
377 ms |
1612 KB |
Output is correct |
43 |
Correct |
349 ms |
1676 KB |
Output is correct |
44 |
Correct |
336 ms |
1748 KB |
Output is correct |
45 |
Correct |
324 ms |
1308 KB |
Output is correct |
46 |
Correct |
349 ms |
1356 KB |
Output is correct |
47 |
Correct |
334 ms |
1356 KB |
Output is correct |
48 |
Correct |
328 ms |
1180 KB |
Output is correct |
49 |
Correct |
333 ms |
1276 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
334 ms |
1888 KB |
Output is correct |
52 |
Correct |
159 ms |
17488 KB |
Output is correct |
53 |
Correct |
177 ms |
17296 KB |
Output is correct |
54 |
Correct |
152 ms |
16036 KB |
Output is correct |
55 |
Correct |
143 ms |
15884 KB |
Output is correct |
56 |
Correct |
146 ms |
16336 KB |
Output is correct |
57 |
Correct |
150 ms |
16096 KB |
Output is correct |
58 |
Correct |
147 ms |
15864 KB |
Output is correct |
59 |
Correct |
152 ms |
16108 KB |
Output is correct |
60 |
Correct |
159 ms |
15908 KB |
Output is correct |
61 |
Correct |
175 ms |
17192 KB |
Output is correct |
62 |
Correct |
243 ms |
24628 KB |
Output is correct |
63 |
Correct |
264 ms |
30128 KB |
Output is correct |
64 |
Correct |
267 ms |
27504 KB |
Output is correct |
65 |
Correct |
281 ms |
23852 KB |
Output is correct |
66 |
Correct |
286 ms |
25940 KB |
Output is correct |
67 |
Correct |
1 ms |
308 KB |
Output is correct |
68 |
Correct |
152 ms |
19916 KB |
Output is correct |
69 |
Correct |
690 ms |
23476 KB |
Output is correct |
70 |
Correct |
691 ms |
24612 KB |
Output is correct |
71 |
Correct |
661 ms |
22904 KB |
Output is correct |
72 |
Correct |
676 ms |
23972 KB |
Output is correct |
73 |
Correct |
675 ms |
23588 KB |
Output is correct |
74 |
Correct |
687 ms |
24972 KB |
Output is correct |
75 |
Correct |
694 ms |
23244 KB |
Output is correct |
76 |
Correct |
689 ms |
24988 KB |
Output is correct |
77 |
Correct |
652 ms |
22992 KB |
Output is correct |
78 |
Correct |
675 ms |
23508 KB |
Output is correct |
79 |
Correct |
667 ms |
32892 KB |
Output is correct |
80 |
Correct |
608 ms |
30412 KB |
Output is correct |
81 |
Correct |
633 ms |
30408 KB |
Output is correct |
82 |
Correct |
660 ms |
27088 KB |
Output is correct |
83 |
Correct |
616 ms |
25904 KB |
Output is correct |
84 |
Correct |
1 ms |
340 KB |
Output is correct |
85 |
Correct |
518 ms |
23180 KB |
Output is correct |