//author: Ahmet Alp Orakci
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
bool intersect(int l1, int l2, int r1, int r2) {
if(l2 < r1 || r2 < l1)
return false;
return true;
}
#define ONLINE_JUDGE
void solve() {
int n;
cin >> n;
vector <array <int, 2>> vec(n +1);
for(int i = 1; i <= n; i++) {
cin >> vec[i][0] >> vec[i][1];
}
vector <vector <int>> bl(n +2);
for(int i = 1; i <= n +1; i++)
bl[i].resize(21);
multiset <pair <int, int>> ms;
int l = 1, r = 1;
ms.emplace(vec[l][0], vec[l][1]);
while(l <= n) {
if(r < l) {
ms.emplace(vec[l][0], vec[l][1]);
r++;
continue;
}
auto ok = [&](int x) -> bool {
bool res = true;
auto it = ms.lower_bound({vec[x][0], 0});
if(it != ms.end()) {
auto [a, b] = *it;
res &= !intersect(a, b, vec[x][0], vec[x][1]);
}
if(it != ms.begin()) {
auto [a, b] = *prev(it);
res &= !intersect(a, b, vec[x][0], vec[x][1]);
}
return res;
};
while(r +1 <= n && ok(r +1)) {
r++;
ms.emplace(vec[r][0], vec[r][1]);
}
//cerr << l << " " << r << "\n";
bl[l][0] = r +1;
ms.erase(ms.find({vec[l][0], vec[l][1]}));
l++;
}
bl[n +1][0] = n +1;
for(int i = 1; i < 21; i++) {
for(int j = 1; j <= n +1; j++) {
bl[j][i] = bl[bl[j][i -1]][i -1];
}
}
auto get = [&](int l, int r) -> int {
int res = 0;
for(int i = 20; i >= 0; i--) {
if(bl[l][i] <= r) {
l = bl[l][i];
res += (1 << i);
}
}
return res +1;
};
int q;
cin >> q;
while(q--) {
int l, r;
cin >> l >> r;
cout << get(l, r) << "\n";
}
return;
}
signed main() {
#ifndef ONLINE_JUDGE
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t = 1; //cin >> t;
for(int i = 1; i <= t; i++) {
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
24372 KB |
Output is correct |
2 |
Correct |
80 ms |
23952 KB |
Output is correct |
3 |
Correct |
81 ms |
23948 KB |
Output is correct |
4 |
Correct |
89 ms |
24144 KB |
Output is correct |
5 |
Correct |
83 ms |
25168 KB |
Output is correct |
6 |
Correct |
70 ms |
23124 KB |
Output is correct |
7 |
Correct |
76 ms |
23388 KB |
Output is correct |
8 |
Correct |
78 ms |
23792 KB |
Output is correct |
9 |
Correct |
84 ms |
24156 KB |
Output is correct |
10 |
Correct |
86 ms |
23376 KB |
Output is correct |
11 |
Correct |
180 ms |
34072 KB |
Output is correct |
12 |
Correct |
181 ms |
34896 KB |
Output is correct |
13 |
Correct |
179 ms |
35416 KB |
Output is correct |
14 |
Correct |
167 ms |
33364 KB |
Output is correct |
15 |
Correct |
162 ms |
31256 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
82 ms |
28636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
860 KB |
Output is correct |
2 |
Correct |
3 ms |
860 KB |
Output is correct |
3 |
Correct |
3 ms |
860 KB |
Output is correct |
4 |
Correct |
3 ms |
860 KB |
Output is correct |
5 |
Correct |
3 ms |
860 KB |
Output is correct |
6 |
Correct |
3 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
3 ms |
1088 KB |
Output is correct |
9 |
Correct |
3 ms |
860 KB |
Output is correct |
10 |
Correct |
3 ms |
860 KB |
Output is correct |
11 |
Correct |
4 ms |
1116 KB |
Output is correct |
12 |
Correct |
4 ms |
1116 KB |
Output is correct |
13 |
Correct |
4 ms |
1116 KB |
Output is correct |
14 |
Correct |
4 ms |
1164 KB |
Output is correct |
15 |
Correct |
4 ms |
1112 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
860 KB |
Output is correct |
2 |
Correct |
3 ms |
860 KB |
Output is correct |
3 |
Correct |
3 ms |
860 KB |
Output is correct |
4 |
Correct |
3 ms |
860 KB |
Output is correct |
5 |
Correct |
3 ms |
860 KB |
Output is correct |
6 |
Correct |
3 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
3 ms |
1088 KB |
Output is correct |
9 |
Correct |
3 ms |
860 KB |
Output is correct |
10 |
Correct |
3 ms |
860 KB |
Output is correct |
11 |
Correct |
4 ms |
1116 KB |
Output is correct |
12 |
Correct |
4 ms |
1116 KB |
Output is correct |
13 |
Correct |
4 ms |
1116 KB |
Output is correct |
14 |
Correct |
4 ms |
1164 KB |
Output is correct |
15 |
Correct |
4 ms |
1112 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
980 KB |
Output is correct |
18 |
Correct |
59 ms |
1776 KB |
Output is correct |
19 |
Correct |
53 ms |
1808 KB |
Output is correct |
20 |
Correct |
61 ms |
1812 KB |
Output is correct |
21 |
Correct |
59 ms |
1748 KB |
Output is correct |
22 |
Correct |
58 ms |
1824 KB |
Output is correct |
23 |
Correct |
59 ms |
1620 KB |
Output is correct |
24 |
Correct |
62 ms |
1796 KB |
Output is correct |
25 |
Correct |
57 ms |
1712 KB |
Output is correct |
26 |
Correct |
57 ms |
1728 KB |
Output is correct |
27 |
Correct |
51 ms |
1592 KB |
Output is correct |
28 |
Correct |
37 ms |
1616 KB |
Output is correct |
29 |
Correct |
42 ms |
1480 KB |
Output is correct |
30 |
Correct |
38 ms |
1620 KB |
Output is correct |
31 |
Correct |
38 ms |
1360 KB |
Output is correct |
32 |
Correct |
38 ms |
1392 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
51 ms |
1844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
25344 KB |
Output is correct |
2 |
Correct |
102 ms |
24784 KB |
Output is correct |
3 |
Correct |
80 ms |
23116 KB |
Output is correct |
4 |
Correct |
74 ms |
22812 KB |
Output is correct |
5 |
Correct |
82 ms |
23636 KB |
Output is correct |
6 |
Correct |
72 ms |
23248 KB |
Output is correct |
7 |
Correct |
79 ms |
22860 KB |
Output is correct |
8 |
Correct |
80 ms |
23304 KB |
Output is correct |
9 |
Correct |
81 ms |
23128 KB |
Output is correct |
10 |
Correct |
92 ms |
25000 KB |
Output is correct |
11 |
Correct |
171 ms |
31736 KB |
Output is correct |
12 |
Correct |
201 ms |
38220 KB |
Output is correct |
13 |
Correct |
171 ms |
34640 KB |
Output is correct |
14 |
Correct |
186 ms |
31056 KB |
Output is correct |
15 |
Correct |
179 ms |
33876 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
74 ms |
27224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
24372 KB |
Output is correct |
2 |
Correct |
80 ms |
23952 KB |
Output is correct |
3 |
Correct |
81 ms |
23948 KB |
Output is correct |
4 |
Correct |
89 ms |
24144 KB |
Output is correct |
5 |
Correct |
83 ms |
25168 KB |
Output is correct |
6 |
Correct |
70 ms |
23124 KB |
Output is correct |
7 |
Correct |
76 ms |
23388 KB |
Output is correct |
8 |
Correct |
78 ms |
23792 KB |
Output is correct |
9 |
Correct |
84 ms |
24156 KB |
Output is correct |
10 |
Correct |
86 ms |
23376 KB |
Output is correct |
11 |
Correct |
180 ms |
34072 KB |
Output is correct |
12 |
Correct |
181 ms |
34896 KB |
Output is correct |
13 |
Correct |
179 ms |
35416 KB |
Output is correct |
14 |
Correct |
167 ms |
33364 KB |
Output is correct |
15 |
Correct |
162 ms |
31256 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
82 ms |
28636 KB |
Output is correct |
18 |
Correct |
3 ms |
860 KB |
Output is correct |
19 |
Correct |
3 ms |
860 KB |
Output is correct |
20 |
Correct |
3 ms |
860 KB |
Output is correct |
21 |
Correct |
3 ms |
860 KB |
Output is correct |
22 |
Correct |
3 ms |
860 KB |
Output is correct |
23 |
Correct |
3 ms |
860 KB |
Output is correct |
24 |
Correct |
3 ms |
860 KB |
Output is correct |
25 |
Correct |
3 ms |
1088 KB |
Output is correct |
26 |
Correct |
3 ms |
860 KB |
Output is correct |
27 |
Correct |
3 ms |
860 KB |
Output is correct |
28 |
Correct |
4 ms |
1116 KB |
Output is correct |
29 |
Correct |
4 ms |
1116 KB |
Output is correct |
30 |
Correct |
4 ms |
1116 KB |
Output is correct |
31 |
Correct |
4 ms |
1164 KB |
Output is correct |
32 |
Correct |
4 ms |
1112 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
3 ms |
980 KB |
Output is correct |
35 |
Correct |
59 ms |
1776 KB |
Output is correct |
36 |
Correct |
53 ms |
1808 KB |
Output is correct |
37 |
Correct |
61 ms |
1812 KB |
Output is correct |
38 |
Correct |
59 ms |
1748 KB |
Output is correct |
39 |
Correct |
58 ms |
1824 KB |
Output is correct |
40 |
Correct |
59 ms |
1620 KB |
Output is correct |
41 |
Correct |
62 ms |
1796 KB |
Output is correct |
42 |
Correct |
57 ms |
1712 KB |
Output is correct |
43 |
Correct |
57 ms |
1728 KB |
Output is correct |
44 |
Correct |
51 ms |
1592 KB |
Output is correct |
45 |
Correct |
37 ms |
1616 KB |
Output is correct |
46 |
Correct |
42 ms |
1480 KB |
Output is correct |
47 |
Correct |
38 ms |
1620 KB |
Output is correct |
48 |
Correct |
38 ms |
1360 KB |
Output is correct |
49 |
Correct |
38 ms |
1392 KB |
Output is correct |
50 |
Correct |
1 ms |
348 KB |
Output is correct |
51 |
Correct |
51 ms |
1844 KB |
Output is correct |
52 |
Correct |
89 ms |
25344 KB |
Output is correct |
53 |
Correct |
102 ms |
24784 KB |
Output is correct |
54 |
Correct |
80 ms |
23116 KB |
Output is correct |
55 |
Correct |
74 ms |
22812 KB |
Output is correct |
56 |
Correct |
82 ms |
23636 KB |
Output is correct |
57 |
Correct |
72 ms |
23248 KB |
Output is correct |
58 |
Correct |
79 ms |
22860 KB |
Output is correct |
59 |
Correct |
80 ms |
23304 KB |
Output is correct |
60 |
Correct |
81 ms |
23128 KB |
Output is correct |
61 |
Correct |
92 ms |
25000 KB |
Output is correct |
62 |
Correct |
171 ms |
31736 KB |
Output is correct |
63 |
Correct |
201 ms |
38220 KB |
Output is correct |
64 |
Correct |
171 ms |
34640 KB |
Output is correct |
65 |
Correct |
186 ms |
31056 KB |
Output is correct |
66 |
Correct |
179 ms |
33876 KB |
Output is correct |
67 |
Correct |
0 ms |
348 KB |
Output is correct |
68 |
Correct |
74 ms |
27224 KB |
Output is correct |
69 |
Correct |
287 ms |
30780 KB |
Output is correct |
70 |
Correct |
338 ms |
32224 KB |
Output is correct |
71 |
Correct |
271 ms |
29908 KB |
Output is correct |
72 |
Correct |
287 ms |
31576 KB |
Output is correct |
73 |
Correct |
298 ms |
31028 KB |
Output is correct |
74 |
Correct |
322 ms |
32632 KB |
Output is correct |
75 |
Correct |
288 ms |
30420 KB |
Output is correct |
76 |
Correct |
329 ms |
32596 KB |
Output is correct |
77 |
Correct |
256 ms |
30244 KB |
Output is correct |
78 |
Correct |
254 ms |
30724 KB |
Output is correct |
79 |
Correct |
280 ms |
40528 KB |
Output is correct |
80 |
Correct |
231 ms |
37768 KB |
Output is correct |
81 |
Correct |
232 ms |
37716 KB |
Output is correct |
82 |
Correct |
230 ms |
34640 KB |
Output is correct |
83 |
Correct |
239 ms |
33436 KB |
Output is correct |
84 |
Correct |
0 ms |
344 KB |
Output is correct |
85 |
Correct |
137 ms |
30296 KB |
Output is correct |