#include <bits/stdc++.h>
using namespace std;
#define mid ((l + r) >> 1)
#define tl (node + 1)
#define tr (node + 2 * (mid - l + 1))
bool intersect (pair <int, int> a, pair <int, int> b) {
return !(a.second < b.first || b.second < a.first);
}
const int MAXN = 2e5 + 25;
struct SegmentTree {
int tree[4 * MAXN], lazy[4 * MAXN];
void init () {
for (int i = 1; i < 4 * MAXN; i++) lazy[i] = -1;
}
void prop (int l, int r, int node) {
if (lazy[node] == -1) return;
if (l != r) {
lazy[tl] = lazy[tr] = lazy[node];
}
tree[node] = lazy[node];
lazy[node] = -1;
}
void update (int l, int r, int a, int b, int c, int node) {
prop(l, r, node);
if (l > b || r < a) return;
if (l >= a && r <= b) {
lazy[node] = c;
prop(l, r, node);
return;
}
update(l, mid, a, b, c, tl);
update(mid + 1, r, a, b, c, tr);
tree[node] = min(tree[tl], tree[tr]);
}
int get (int l, int r, int a, int b, int node) {
prop(l, r, node);
if (l > b || r < a) return 1e9;
if (l >= a && r <= b) return tree[node];
return min(get(l, mid, a, b, tl), get(mid + 1, r, a, b, tr));
}
} cur;
pair <int, int> arr[MAXN];
int nxt[MAXN];
vector <int> l;
int sparse[18][MAXN];
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n; cur.init();
for (int i = 1; i <= n; i++) cin >> arr[i].first >> arr[i].second;
for (int i = 1; i <= n; i++) {
l.push_back(arr[i].first); l.push_back(arr[i].second);
}
l.push_back(1); l.push_back((int)1e9);
sort(l.begin(), l.end()); l.resize(unique(l.begin(), l.end()) - l.begin());
for (int i = 1; i <= n; i++) {
arr[i].first = 1 + (lower_bound(l.begin(), l.end(), arr[i].first) - l.begin());
arr[i].second = 1 + (lower_bound(l.begin(), l.end(), arr[i].second) - l.begin());
}
cur.init(); cur.update(1, 400025, 1, 400025, n + 1, 1);
for (int i = n; i >= 1; i--) {
nxt[i] = cur.get(1, 400025, arr[i].first, arr[i].second, 1);
if (i < n) nxt[i] = min(nxt[i], nxt[i + 1]);
cur.update(1, 400025, arr[i].first, arr[i].second, i, 1);
}
nxt[n + 1] = n + 1;
for (int i = 1; i <= n + 1; i++) {
sparse[0][i] = nxt[i];
}
for (int i = 1; i < 18; i++) {
for (int j = 1; j <= n + 1; j++) {
sparse[i][j] = sparse[i - 1][sparse[i - 1][j]];
}
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
int cnt = 0;
for (int i = 17; i >= 0; i--) {
if (sparse[i][l] <= r) {
cnt += (1 << i);
l = sparse[i][l];
}
}
cout << cnt + 1 << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
267 ms |
28284 KB |
Output is correct |
2 |
Correct |
269 ms |
27588 KB |
Output is correct |
3 |
Correct |
263 ms |
28356 KB |
Output is correct |
4 |
Correct |
278 ms |
28304 KB |
Output is correct |
5 |
Correct |
288 ms |
28504 KB |
Output is correct |
6 |
Correct |
79 ms |
28104 KB |
Output is correct |
7 |
Correct |
87 ms |
28128 KB |
Output is correct |
8 |
Correct |
84 ms |
28100 KB |
Output is correct |
9 |
Correct |
90 ms |
28492 KB |
Output is correct |
10 |
Correct |
89 ms |
28100 KB |
Output is correct |
11 |
Correct |
177 ms |
27820 KB |
Output is correct |
12 |
Correct |
167 ms |
27540 KB |
Output is correct |
13 |
Correct |
175 ms |
27528 KB |
Output is correct |
14 |
Correct |
177 ms |
27836 KB |
Output is correct |
15 |
Correct |
178 ms |
27776 KB |
Output is correct |
16 |
Correct |
3 ms |
23128 KB |
Output is correct |
17 |
Correct |
84 ms |
28444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23128 KB |
Output is correct |
2 |
Correct |
9 ms |
23132 KB |
Output is correct |
3 |
Correct |
9 ms |
23132 KB |
Output is correct |
4 |
Correct |
10 ms |
23132 KB |
Output is correct |
5 |
Correct |
10 ms |
23128 KB |
Output is correct |
6 |
Correct |
7 ms |
23132 KB |
Output is correct |
7 |
Correct |
6 ms |
23204 KB |
Output is correct |
8 |
Correct |
7 ms |
23132 KB |
Output is correct |
9 |
Correct |
8 ms |
23300 KB |
Output is correct |
10 |
Correct |
7 ms |
23128 KB |
Output is correct |
11 |
Correct |
8 ms |
23188 KB |
Output is correct |
12 |
Correct |
7 ms |
23132 KB |
Output is correct |
13 |
Correct |
7 ms |
23176 KB |
Output is correct |
14 |
Correct |
8 ms |
23132 KB |
Output is correct |
15 |
Correct |
8 ms |
23224 KB |
Output is correct |
16 |
Correct |
3 ms |
22872 KB |
Output is correct |
17 |
Correct |
6 ms |
23132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23128 KB |
Output is correct |
2 |
Correct |
9 ms |
23132 KB |
Output is correct |
3 |
Correct |
9 ms |
23132 KB |
Output is correct |
4 |
Correct |
10 ms |
23132 KB |
Output is correct |
5 |
Correct |
10 ms |
23128 KB |
Output is correct |
6 |
Correct |
7 ms |
23132 KB |
Output is correct |
7 |
Correct |
6 ms |
23204 KB |
Output is correct |
8 |
Correct |
7 ms |
23132 KB |
Output is correct |
9 |
Correct |
8 ms |
23300 KB |
Output is correct |
10 |
Correct |
7 ms |
23128 KB |
Output is correct |
11 |
Correct |
8 ms |
23188 KB |
Output is correct |
12 |
Correct |
7 ms |
23132 KB |
Output is correct |
13 |
Correct |
7 ms |
23176 KB |
Output is correct |
14 |
Correct |
8 ms |
23132 KB |
Output is correct |
15 |
Correct |
8 ms |
23224 KB |
Output is correct |
16 |
Correct |
3 ms |
22872 KB |
Output is correct |
17 |
Correct |
6 ms |
23132 KB |
Output is correct |
18 |
Correct |
64 ms |
25924 KB |
Output is correct |
19 |
Correct |
61 ms |
25680 KB |
Output is correct |
20 |
Correct |
62 ms |
25748 KB |
Output is correct |
21 |
Correct |
57 ms |
25616 KB |
Output is correct |
22 |
Correct |
59 ms |
25824 KB |
Output is correct |
23 |
Correct |
59 ms |
25684 KB |
Output is correct |
24 |
Correct |
58 ms |
26000 KB |
Output is correct |
25 |
Correct |
57 ms |
25680 KB |
Output is correct |
26 |
Correct |
55 ms |
25608 KB |
Output is correct |
27 |
Correct |
54 ms |
25428 KB |
Output is correct |
28 |
Correct |
45 ms |
25152 KB |
Output is correct |
29 |
Correct |
43 ms |
25168 KB |
Output is correct |
30 |
Correct |
43 ms |
25172 KB |
Output is correct |
31 |
Correct |
44 ms |
25300 KB |
Output is correct |
32 |
Correct |
42 ms |
25168 KB |
Output is correct |
33 |
Correct |
3 ms |
23244 KB |
Output is correct |
34 |
Correct |
45 ms |
25428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
283 ms |
24656 KB |
Output is correct |
2 |
Correct |
271 ms |
24608 KB |
Output is correct |
3 |
Correct |
259 ms |
24776 KB |
Output is correct |
4 |
Correct |
247 ms |
28176 KB |
Output is correct |
5 |
Correct |
267 ms |
28356 KB |
Output is correct |
6 |
Correct |
79 ms |
28104 KB |
Output is correct |
7 |
Correct |
79 ms |
28036 KB |
Output is correct |
8 |
Correct |
82 ms |
27980 KB |
Output is correct |
9 |
Correct |
84 ms |
28168 KB |
Output is correct |
10 |
Correct |
97 ms |
28416 KB |
Output is correct |
11 |
Correct |
166 ms |
27512 KB |
Output is correct |
12 |
Correct |
190 ms |
28104 KB |
Output is correct |
13 |
Correct |
164 ms |
27492 KB |
Output is correct |
14 |
Correct |
169 ms |
27792 KB |
Output is correct |
15 |
Correct |
186 ms |
27916 KB |
Output is correct |
16 |
Correct |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
80 ms |
28156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
267 ms |
28284 KB |
Output is correct |
2 |
Correct |
269 ms |
27588 KB |
Output is correct |
3 |
Correct |
263 ms |
28356 KB |
Output is correct |
4 |
Correct |
278 ms |
28304 KB |
Output is correct |
5 |
Correct |
288 ms |
28504 KB |
Output is correct |
6 |
Correct |
79 ms |
28104 KB |
Output is correct |
7 |
Correct |
87 ms |
28128 KB |
Output is correct |
8 |
Correct |
84 ms |
28100 KB |
Output is correct |
9 |
Correct |
90 ms |
28492 KB |
Output is correct |
10 |
Correct |
89 ms |
28100 KB |
Output is correct |
11 |
Correct |
177 ms |
27820 KB |
Output is correct |
12 |
Correct |
167 ms |
27540 KB |
Output is correct |
13 |
Correct |
175 ms |
27528 KB |
Output is correct |
14 |
Correct |
177 ms |
27836 KB |
Output is correct |
15 |
Correct |
178 ms |
27776 KB |
Output is correct |
16 |
Correct |
3 ms |
23128 KB |
Output is correct |
17 |
Correct |
84 ms |
28444 KB |
Output is correct |
18 |
Correct |
10 ms |
23128 KB |
Output is correct |
19 |
Correct |
9 ms |
23132 KB |
Output is correct |
20 |
Correct |
9 ms |
23132 KB |
Output is correct |
21 |
Correct |
10 ms |
23132 KB |
Output is correct |
22 |
Correct |
10 ms |
23128 KB |
Output is correct |
23 |
Correct |
7 ms |
23132 KB |
Output is correct |
24 |
Correct |
6 ms |
23204 KB |
Output is correct |
25 |
Correct |
7 ms |
23132 KB |
Output is correct |
26 |
Correct |
8 ms |
23300 KB |
Output is correct |
27 |
Correct |
7 ms |
23128 KB |
Output is correct |
28 |
Correct |
8 ms |
23188 KB |
Output is correct |
29 |
Correct |
7 ms |
23132 KB |
Output is correct |
30 |
Correct |
7 ms |
23176 KB |
Output is correct |
31 |
Correct |
8 ms |
23132 KB |
Output is correct |
32 |
Correct |
8 ms |
23224 KB |
Output is correct |
33 |
Correct |
3 ms |
22872 KB |
Output is correct |
34 |
Correct |
6 ms |
23132 KB |
Output is correct |
35 |
Correct |
64 ms |
25924 KB |
Output is correct |
36 |
Correct |
61 ms |
25680 KB |
Output is correct |
37 |
Correct |
62 ms |
25748 KB |
Output is correct |
38 |
Correct |
57 ms |
25616 KB |
Output is correct |
39 |
Correct |
59 ms |
25824 KB |
Output is correct |
40 |
Correct |
59 ms |
25684 KB |
Output is correct |
41 |
Correct |
58 ms |
26000 KB |
Output is correct |
42 |
Correct |
57 ms |
25680 KB |
Output is correct |
43 |
Correct |
55 ms |
25608 KB |
Output is correct |
44 |
Correct |
54 ms |
25428 KB |
Output is correct |
45 |
Correct |
45 ms |
25152 KB |
Output is correct |
46 |
Correct |
43 ms |
25168 KB |
Output is correct |
47 |
Correct |
43 ms |
25172 KB |
Output is correct |
48 |
Correct |
44 ms |
25300 KB |
Output is correct |
49 |
Correct |
42 ms |
25168 KB |
Output is correct |
50 |
Correct |
3 ms |
23244 KB |
Output is correct |
51 |
Correct |
45 ms |
25428 KB |
Output is correct |
52 |
Correct |
283 ms |
24656 KB |
Output is correct |
53 |
Correct |
271 ms |
24608 KB |
Output is correct |
54 |
Correct |
259 ms |
24776 KB |
Output is correct |
55 |
Correct |
247 ms |
28176 KB |
Output is correct |
56 |
Correct |
267 ms |
28356 KB |
Output is correct |
57 |
Correct |
79 ms |
28104 KB |
Output is correct |
58 |
Correct |
79 ms |
28036 KB |
Output is correct |
59 |
Correct |
82 ms |
27980 KB |
Output is correct |
60 |
Correct |
84 ms |
28168 KB |
Output is correct |
61 |
Correct |
97 ms |
28416 KB |
Output is correct |
62 |
Correct |
166 ms |
27512 KB |
Output is correct |
63 |
Correct |
190 ms |
28104 KB |
Output is correct |
64 |
Correct |
164 ms |
27492 KB |
Output is correct |
65 |
Correct |
169 ms |
27792 KB |
Output is correct |
66 |
Correct |
186 ms |
27916 KB |
Output is correct |
67 |
Correct |
3 ms |
22876 KB |
Output is correct |
68 |
Correct |
80 ms |
28156 KB |
Output is correct |
69 |
Correct |
346 ms |
31944 KB |
Output is correct |
70 |
Correct |
394 ms |
31860 KB |
Output is correct |
71 |
Correct |
330 ms |
31568 KB |
Output is correct |
72 |
Correct |
342 ms |
31688 KB |
Output is correct |
73 |
Correct |
360 ms |
31756 KB |
Output is correct |
74 |
Correct |
183 ms |
32276 KB |
Output is correct |
75 |
Correct |
167 ms |
31700 KB |
Output is correct |
76 |
Correct |
175 ms |
32204 KB |
Output is correct |
77 |
Correct |
165 ms |
31616 KB |
Output is correct |
78 |
Correct |
195 ms |
31688 KB |
Output is correct |
79 |
Correct |
236 ms |
30708 KB |
Output is correct |
80 |
Correct |
216 ms |
30532 KB |
Output is correct |
81 |
Correct |
225 ms |
30256 KB |
Output is correct |
82 |
Correct |
238 ms |
30716 KB |
Output is correct |
83 |
Correct |
211 ms |
30468 KB |
Output is correct |
84 |
Correct |
3 ms |
22872 KB |
Output is correct |
85 |
Correct |
130 ms |
31756 KB |
Output is correct |