#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> l(n), r(n);
vector<int> cs;
for (int i = 0; i < n; i++) {
cin >> l[i] >> r[i];
}
multiset<pair<int, int>> segs;
auto Check = [&](int L, int R) {
auto it = segs.lower_bound({L, -1});
if (it != segs.end()) {
if (it->first <= R) {
return false;
}
}
if (it != segs.begin()) {
it = prev(it);
if (it->second >= L) {
return false;
}
}
return true;
};
const int L = 20;
vector<vector<int>> jump(n + 1, vector<int>(L, n));
int ptr = n - 1;
for (int i = n - 1; i >= 0; i--) {
while (!Check(l[i], r[i])) {
segs.erase(segs.find({l[ptr], r[ptr]}));
ptr--;
}
segs.insert({l[i], r[i]});
jump[i][0] = ptr;
}
for (int j = 1; j < L; j++) {
for (int i = 0; i < n; i++) {
if (jump[i][j - 1] < n) {
jump[i][j] = jump[jump[i][j - 1] + 1][j - 1];
}
}
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
--l; --r;
int ans = 0;
for (int j = L - 1; j >= 0; j--) {
if (jump[l][j] <= r) {
l = jump[l][j] + 1;
ans += 1 << j;
}
if (l >= n) {
break;
}
}
cout << ans + (l <= r ? 1 : 0) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
24236 KB |
Output is correct |
2 |
Correct |
102 ms |
23828 KB |
Output is correct |
3 |
Correct |
100 ms |
23816 KB |
Output is correct |
4 |
Correct |
103 ms |
24260 KB |
Output is correct |
5 |
Correct |
100 ms |
25180 KB |
Output is correct |
6 |
Correct |
86 ms |
22988 KB |
Output is correct |
7 |
Correct |
91 ms |
23388 KB |
Output is correct |
8 |
Correct |
123 ms |
23656 KB |
Output is correct |
9 |
Correct |
106 ms |
24084 KB |
Output is correct |
10 |
Correct |
103 ms |
23404 KB |
Output is correct |
11 |
Correct |
169 ms |
33876 KB |
Output is correct |
12 |
Correct |
160 ms |
31840 KB |
Output is correct |
13 |
Correct |
176 ms |
31736 KB |
Output is correct |
14 |
Correct |
180 ms |
30080 KB |
Output is correct |
15 |
Correct |
181 ms |
28108 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
103 ms |
24460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
844 KB |
Output is correct |
2 |
Correct |
5 ms |
844 KB |
Output is correct |
3 |
Correct |
4 ms |
844 KB |
Output is correct |
4 |
Correct |
4 ms |
932 KB |
Output is correct |
5 |
Correct |
4 ms |
844 KB |
Output is correct |
6 |
Correct |
4 ms |
844 KB |
Output is correct |
7 |
Correct |
5 ms |
976 KB |
Output is correct |
8 |
Correct |
4 ms |
844 KB |
Output is correct |
9 |
Correct |
4 ms |
856 KB |
Output is correct |
10 |
Correct |
4 ms |
856 KB |
Output is correct |
11 |
Correct |
4 ms |
1124 KB |
Output is correct |
12 |
Correct |
4 ms |
1100 KB |
Output is correct |
13 |
Correct |
4 ms |
1100 KB |
Output is correct |
14 |
Correct |
5 ms |
1028 KB |
Output is correct |
15 |
Correct |
4 ms |
1000 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
6 ms |
844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
844 KB |
Output is correct |
2 |
Correct |
5 ms |
844 KB |
Output is correct |
3 |
Correct |
4 ms |
844 KB |
Output is correct |
4 |
Correct |
4 ms |
932 KB |
Output is correct |
5 |
Correct |
4 ms |
844 KB |
Output is correct |
6 |
Correct |
4 ms |
844 KB |
Output is correct |
7 |
Correct |
5 ms |
976 KB |
Output is correct |
8 |
Correct |
4 ms |
844 KB |
Output is correct |
9 |
Correct |
4 ms |
856 KB |
Output is correct |
10 |
Correct |
4 ms |
856 KB |
Output is correct |
11 |
Correct |
4 ms |
1124 KB |
Output is correct |
12 |
Correct |
4 ms |
1100 KB |
Output is correct |
13 |
Correct |
4 ms |
1100 KB |
Output is correct |
14 |
Correct |
5 ms |
1028 KB |
Output is correct |
15 |
Correct |
4 ms |
1000 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
6 ms |
844 KB |
Output is correct |
18 |
Correct |
88 ms |
1684 KB |
Output is correct |
19 |
Correct |
66 ms |
1660 KB |
Output is correct |
20 |
Correct |
77 ms |
1704 KB |
Output is correct |
21 |
Correct |
71 ms |
1580 KB |
Output is correct |
22 |
Correct |
74 ms |
1724 KB |
Output is correct |
23 |
Correct |
68 ms |
1604 KB |
Output is correct |
24 |
Correct |
70 ms |
1684 KB |
Output is correct |
25 |
Correct |
72 ms |
1684 KB |
Output is correct |
26 |
Correct |
69 ms |
1668 KB |
Output is correct |
27 |
Correct |
67 ms |
1496 KB |
Output is correct |
28 |
Correct |
63 ms |
1468 KB |
Output is correct |
29 |
Correct |
54 ms |
1476 KB |
Output is correct |
30 |
Correct |
59 ms |
1568 KB |
Output is correct |
31 |
Correct |
50 ms |
1272 KB |
Output is correct |
32 |
Correct |
67 ms |
1320 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
51 ms |
1796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
25312 KB |
Output is correct |
2 |
Correct |
104 ms |
24816 KB |
Output is correct |
3 |
Correct |
103 ms |
23136 KB |
Output is correct |
4 |
Correct |
101 ms |
22968 KB |
Output is correct |
5 |
Correct |
96 ms |
23492 KB |
Output is correct |
6 |
Correct |
87 ms |
23180 KB |
Output is correct |
7 |
Correct |
95 ms |
22996 KB |
Output is correct |
8 |
Correct |
121 ms |
23108 KB |
Output is correct |
9 |
Correct |
96 ms |
22852 KB |
Output is correct |
10 |
Correct |
123 ms |
24772 KB |
Output is correct |
11 |
Correct |
162 ms |
31604 KB |
Output is correct |
12 |
Correct |
175 ms |
34700 KB |
Output is correct |
13 |
Correct |
192 ms |
31660 KB |
Output is correct |
14 |
Correct |
167 ms |
27972 KB |
Output is correct |
15 |
Correct |
208 ms |
30420 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
92 ms |
23436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
24236 KB |
Output is correct |
2 |
Correct |
102 ms |
23828 KB |
Output is correct |
3 |
Correct |
100 ms |
23816 KB |
Output is correct |
4 |
Correct |
103 ms |
24260 KB |
Output is correct |
5 |
Correct |
100 ms |
25180 KB |
Output is correct |
6 |
Correct |
86 ms |
22988 KB |
Output is correct |
7 |
Correct |
91 ms |
23388 KB |
Output is correct |
8 |
Correct |
123 ms |
23656 KB |
Output is correct |
9 |
Correct |
106 ms |
24084 KB |
Output is correct |
10 |
Correct |
103 ms |
23404 KB |
Output is correct |
11 |
Correct |
169 ms |
33876 KB |
Output is correct |
12 |
Correct |
160 ms |
31840 KB |
Output is correct |
13 |
Correct |
176 ms |
31736 KB |
Output is correct |
14 |
Correct |
180 ms |
30080 KB |
Output is correct |
15 |
Correct |
181 ms |
28108 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
103 ms |
24460 KB |
Output is correct |
18 |
Correct |
4 ms |
844 KB |
Output is correct |
19 |
Correct |
5 ms |
844 KB |
Output is correct |
20 |
Correct |
4 ms |
844 KB |
Output is correct |
21 |
Correct |
4 ms |
932 KB |
Output is correct |
22 |
Correct |
4 ms |
844 KB |
Output is correct |
23 |
Correct |
4 ms |
844 KB |
Output is correct |
24 |
Correct |
5 ms |
976 KB |
Output is correct |
25 |
Correct |
4 ms |
844 KB |
Output is correct |
26 |
Correct |
4 ms |
856 KB |
Output is correct |
27 |
Correct |
4 ms |
856 KB |
Output is correct |
28 |
Correct |
4 ms |
1124 KB |
Output is correct |
29 |
Correct |
4 ms |
1100 KB |
Output is correct |
30 |
Correct |
4 ms |
1100 KB |
Output is correct |
31 |
Correct |
5 ms |
1028 KB |
Output is correct |
32 |
Correct |
4 ms |
1000 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
6 ms |
844 KB |
Output is correct |
35 |
Correct |
88 ms |
1684 KB |
Output is correct |
36 |
Correct |
66 ms |
1660 KB |
Output is correct |
37 |
Correct |
77 ms |
1704 KB |
Output is correct |
38 |
Correct |
71 ms |
1580 KB |
Output is correct |
39 |
Correct |
74 ms |
1724 KB |
Output is correct |
40 |
Correct |
68 ms |
1604 KB |
Output is correct |
41 |
Correct |
70 ms |
1684 KB |
Output is correct |
42 |
Correct |
72 ms |
1684 KB |
Output is correct |
43 |
Correct |
69 ms |
1668 KB |
Output is correct |
44 |
Correct |
67 ms |
1496 KB |
Output is correct |
45 |
Correct |
63 ms |
1468 KB |
Output is correct |
46 |
Correct |
54 ms |
1476 KB |
Output is correct |
47 |
Correct |
59 ms |
1568 KB |
Output is correct |
48 |
Correct |
50 ms |
1272 KB |
Output is correct |
49 |
Correct |
67 ms |
1320 KB |
Output is correct |
50 |
Correct |
0 ms |
204 KB |
Output is correct |
51 |
Correct |
51 ms |
1796 KB |
Output is correct |
52 |
Correct |
103 ms |
25312 KB |
Output is correct |
53 |
Correct |
104 ms |
24816 KB |
Output is correct |
54 |
Correct |
103 ms |
23136 KB |
Output is correct |
55 |
Correct |
101 ms |
22968 KB |
Output is correct |
56 |
Correct |
96 ms |
23492 KB |
Output is correct |
57 |
Correct |
87 ms |
23180 KB |
Output is correct |
58 |
Correct |
95 ms |
22996 KB |
Output is correct |
59 |
Correct |
121 ms |
23108 KB |
Output is correct |
60 |
Correct |
96 ms |
22852 KB |
Output is correct |
61 |
Correct |
123 ms |
24772 KB |
Output is correct |
62 |
Correct |
162 ms |
31604 KB |
Output is correct |
63 |
Correct |
175 ms |
34700 KB |
Output is correct |
64 |
Correct |
192 ms |
31660 KB |
Output is correct |
65 |
Correct |
167 ms |
27972 KB |
Output is correct |
66 |
Correct |
208 ms |
30420 KB |
Output is correct |
67 |
Correct |
1 ms |
204 KB |
Output is correct |
68 |
Correct |
92 ms |
23436 KB |
Output is correct |
69 |
Correct |
383 ms |
24632 KB |
Output is correct |
70 |
Correct |
444 ms |
25976 KB |
Output is correct |
71 |
Correct |
365 ms |
29940 KB |
Output is correct |
72 |
Correct |
413 ms |
31404 KB |
Output is correct |
73 |
Correct |
397 ms |
30804 KB |
Output is correct |
74 |
Correct |
402 ms |
32484 KB |
Output is correct |
75 |
Correct |
388 ms |
30232 KB |
Output is correct |
76 |
Correct |
391 ms |
32596 KB |
Output is correct |
77 |
Correct |
365 ms |
30148 KB |
Output is correct |
78 |
Correct |
374 ms |
30620 KB |
Output is correct |
79 |
Correct |
260 ms |
40544 KB |
Output is correct |
80 |
Correct |
271 ms |
37568 KB |
Output is correct |
81 |
Correct |
301 ms |
37404 KB |
Output is correct |
82 |
Correct |
265 ms |
34556 KB |
Output is correct |
83 |
Correct |
292 ms |
33172 KB |
Output is correct |
84 |
Correct |
1 ms |
204 KB |
Output is correct |
85 |
Correct |
174 ms |
30140 KB |
Output is correct |