#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
struct node {
int val, lazy;
};
void minSelf(int &x, const int &y) {
if (y < x) {
x = y;
}
}
struct ST {
vector<node> t;
ST(int n) {
int dim = 1;
while (dim < n) {
dim <<= 1;
}
t.resize(dim << 1, {INF, INF});
}
void push(int x) {
if (t[x].lazy == INF) {
return;
}
for (int i = 0; i < 2; ++i) {
t[x << 1 | i] = {t[x].lazy, t[x].lazy};
}
t[x].lazy = INF;
}
void update(int x, int lx, int rx, int st, int dr, int val) {
if (st <= lx && rx <= dr) {
t[x] = {val, val};
return;
}
push(x);
int mid = (lx + rx) >> 1;
if (st <= mid) {
update(x << 1, lx, mid, st, dr, val);
}
if (mid < dr) {
update(x << 1 | 1, mid + 1, rx, st, dr, val);
}
t[x].val = min(t[x << 1].val, t[x << 1 | 1].val);
}
int query(int x, int lx, int rx, int st, int dr) {
if (st <= lx && rx <= dr) {
return t[x].val;
}
push(x);
int mid = (lx + rx) >> 1, ans = INF;
if (st <= mid) {
minSelf(ans, query(x << 1, lx, mid, st, dr));
}
if (mid < dr) {
minSelf(ans, query(x << 1 | 1, mid + 1, rx, st, dr));
}
return ans;
}
};
void TestCase() {
int n;
cin >> n;
vector<int> st(n + 1), dr(n + 1), points;
for (int i = 1; i <= n; ++i) {
cin >> st[i] >> dr[i];
points.emplace_back(st[i]);
points.emplace_back(dr[i]);
}
sort(points.begin(), points.end());
points.erase(unique(points.begin(), points.end()), points.end());
auto getIndex = [&](const int &x) -> int {
return upper_bound(points.begin(), points.end(), x) - points.begin();
};
for (int i = 1; i <= n; ++i) {
st[i] = getIndex(st[i]);
dr[i] = getIndex(dr[i]);
}
int m = points.size();
points.clear();
vector<int> lg2(n + 1);
for (int i = 2; i <= n; ++i) {
lg2[i] = lg2[i >> 1] + 1;
}
vector<vector<int>> nxt(n + 1, vector<int>(lg2[n] + 1, INF));
ST t(m);
for (int i = n; i > 0; --i) {
nxt[i][0] = t.query(1, 1, m, st[i], dr[i]);
t.update(1, 1, m, st[i], dr[i], i);
if (i < n) {
minSelf(nxt[i][0], nxt[i + 1][0]);
}
}
for (int j = 1; j <= lg2[n]; ++j) {
for (int i = 1; i <= n; ++i) {
if (nxt[i][j - 1] != INF) {
nxt[i][j] = nxt[nxt[i][j - 1]][j - 1];
}
}
}
int q;
cin >> q;
for (int i = 1; i <= q; ++i) {
int l, r;
cin >> l >> r;
int ans = 0;
for (int jump = lg2[r - l]; jump >= 0; --jump) {
if (nxt[l][jump] <= r) {
ans += (1 << jump);
l = nxt[l][jump];
}
}
cout << ans + 1 << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 1; tc <= tests; ++tc) {
TestCase();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
335 ms |
35396 KB |
Output is correct |
2 |
Correct |
359 ms |
34976 KB |
Output is correct |
3 |
Correct |
327 ms |
35084 KB |
Output is correct |
4 |
Correct |
344 ms |
35408 KB |
Output is correct |
5 |
Correct |
400 ms |
36476 KB |
Output is correct |
6 |
Correct |
93 ms |
25812 KB |
Output is correct |
7 |
Correct |
108 ms |
26232 KB |
Output is correct |
8 |
Correct |
99 ms |
26456 KB |
Output is correct |
9 |
Correct |
101 ms |
26984 KB |
Output is correct |
10 |
Correct |
110 ms |
26156 KB |
Output is correct |
11 |
Correct |
229 ms |
35448 KB |
Output is correct |
12 |
Correct |
223 ms |
33724 KB |
Output is correct |
13 |
Correct |
243 ms |
33692 KB |
Output is correct |
14 |
Correct |
243 ms |
35532 KB |
Output is correct |
15 |
Correct |
233 ms |
34664 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
97 ms |
27560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
1208 KB |
Output is correct |
2 |
Correct |
8 ms |
1216 KB |
Output is correct |
3 |
Correct |
8 ms |
1228 KB |
Output is correct |
4 |
Correct |
8 ms |
1228 KB |
Output is correct |
5 |
Correct |
7 ms |
1172 KB |
Output is correct |
6 |
Correct |
4 ms |
972 KB |
Output is correct |
7 |
Correct |
4 ms |
844 KB |
Output is correct |
8 |
Correct |
4 ms |
960 KB |
Output is correct |
9 |
Correct |
4 ms |
932 KB |
Output is correct |
10 |
Correct |
6 ms |
844 KB |
Output is correct |
11 |
Correct |
8 ms |
1164 KB |
Output is correct |
12 |
Correct |
6 ms |
1192 KB |
Output is correct |
13 |
Correct |
7 ms |
1092 KB |
Output is correct |
14 |
Correct |
6 ms |
1112 KB |
Output is correct |
15 |
Correct |
5 ms |
1144 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
4 ms |
956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
1208 KB |
Output is correct |
2 |
Correct |
8 ms |
1216 KB |
Output is correct |
3 |
Correct |
8 ms |
1228 KB |
Output is correct |
4 |
Correct |
8 ms |
1228 KB |
Output is correct |
5 |
Correct |
7 ms |
1172 KB |
Output is correct |
6 |
Correct |
4 ms |
972 KB |
Output is correct |
7 |
Correct |
4 ms |
844 KB |
Output is correct |
8 |
Correct |
4 ms |
960 KB |
Output is correct |
9 |
Correct |
4 ms |
932 KB |
Output is correct |
10 |
Correct |
6 ms |
844 KB |
Output is correct |
11 |
Correct |
8 ms |
1164 KB |
Output is correct |
12 |
Correct |
6 ms |
1192 KB |
Output is correct |
13 |
Correct |
7 ms |
1092 KB |
Output is correct |
14 |
Correct |
6 ms |
1112 KB |
Output is correct |
15 |
Correct |
5 ms |
1144 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
4 ms |
956 KB |
Output is correct |
18 |
Correct |
81 ms |
3900 KB |
Output is correct |
19 |
Correct |
70 ms |
3604 KB |
Output is correct |
20 |
Correct |
78 ms |
3892 KB |
Output is correct |
21 |
Correct |
98 ms |
3668 KB |
Output is correct |
22 |
Correct |
76 ms |
3692 KB |
Output is correct |
23 |
Correct |
75 ms |
3376 KB |
Output is correct |
24 |
Correct |
74 ms |
3552 KB |
Output is correct |
25 |
Correct |
70 ms |
3572 KB |
Output is correct |
26 |
Correct |
69 ms |
3488 KB |
Output is correct |
27 |
Correct |
70 ms |
3228 KB |
Output is correct |
28 |
Correct |
46 ms |
3140 KB |
Output is correct |
29 |
Correct |
58 ms |
3280 KB |
Output is correct |
30 |
Correct |
48 ms |
3264 KB |
Output is correct |
31 |
Correct |
52 ms |
3256 KB |
Output is correct |
32 |
Correct |
65 ms |
3212 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
52 ms |
3272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
355 ms |
36632 KB |
Output is correct |
2 |
Correct |
364 ms |
36060 KB |
Output is correct |
3 |
Correct |
321 ms |
34072 KB |
Output is correct |
4 |
Correct |
319 ms |
33904 KB |
Output is correct |
5 |
Correct |
366 ms |
34644 KB |
Output is correct |
6 |
Correct |
85 ms |
26024 KB |
Output is correct |
7 |
Correct |
88 ms |
25896 KB |
Output is correct |
8 |
Correct |
123 ms |
25836 KB |
Output is correct |
9 |
Correct |
96 ms |
25760 KB |
Output is correct |
10 |
Correct |
121 ms |
27696 KB |
Output is correct |
11 |
Correct |
211 ms |
33584 KB |
Output is correct |
12 |
Correct |
242 ms |
36076 KB |
Output is correct |
13 |
Correct |
220 ms |
33536 KB |
Output is correct |
14 |
Correct |
227 ms |
34236 KB |
Output is correct |
15 |
Correct |
251 ms |
35804 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
87 ms |
26276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
335 ms |
35396 KB |
Output is correct |
2 |
Correct |
359 ms |
34976 KB |
Output is correct |
3 |
Correct |
327 ms |
35084 KB |
Output is correct |
4 |
Correct |
344 ms |
35408 KB |
Output is correct |
5 |
Correct |
400 ms |
36476 KB |
Output is correct |
6 |
Correct |
93 ms |
25812 KB |
Output is correct |
7 |
Correct |
108 ms |
26232 KB |
Output is correct |
8 |
Correct |
99 ms |
26456 KB |
Output is correct |
9 |
Correct |
101 ms |
26984 KB |
Output is correct |
10 |
Correct |
110 ms |
26156 KB |
Output is correct |
11 |
Correct |
229 ms |
35448 KB |
Output is correct |
12 |
Correct |
223 ms |
33724 KB |
Output is correct |
13 |
Correct |
243 ms |
33692 KB |
Output is correct |
14 |
Correct |
243 ms |
35532 KB |
Output is correct |
15 |
Correct |
233 ms |
34664 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
97 ms |
27560 KB |
Output is correct |
18 |
Correct |
10 ms |
1208 KB |
Output is correct |
19 |
Correct |
8 ms |
1216 KB |
Output is correct |
20 |
Correct |
8 ms |
1228 KB |
Output is correct |
21 |
Correct |
8 ms |
1228 KB |
Output is correct |
22 |
Correct |
7 ms |
1172 KB |
Output is correct |
23 |
Correct |
4 ms |
972 KB |
Output is correct |
24 |
Correct |
4 ms |
844 KB |
Output is correct |
25 |
Correct |
4 ms |
960 KB |
Output is correct |
26 |
Correct |
4 ms |
932 KB |
Output is correct |
27 |
Correct |
6 ms |
844 KB |
Output is correct |
28 |
Correct |
8 ms |
1164 KB |
Output is correct |
29 |
Correct |
6 ms |
1192 KB |
Output is correct |
30 |
Correct |
7 ms |
1092 KB |
Output is correct |
31 |
Correct |
6 ms |
1112 KB |
Output is correct |
32 |
Correct |
5 ms |
1144 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
4 ms |
956 KB |
Output is correct |
35 |
Correct |
81 ms |
3900 KB |
Output is correct |
36 |
Correct |
70 ms |
3604 KB |
Output is correct |
37 |
Correct |
78 ms |
3892 KB |
Output is correct |
38 |
Correct |
98 ms |
3668 KB |
Output is correct |
39 |
Correct |
76 ms |
3692 KB |
Output is correct |
40 |
Correct |
75 ms |
3376 KB |
Output is correct |
41 |
Correct |
74 ms |
3552 KB |
Output is correct |
42 |
Correct |
70 ms |
3572 KB |
Output is correct |
43 |
Correct |
69 ms |
3488 KB |
Output is correct |
44 |
Correct |
70 ms |
3228 KB |
Output is correct |
45 |
Correct |
46 ms |
3140 KB |
Output is correct |
46 |
Correct |
58 ms |
3280 KB |
Output is correct |
47 |
Correct |
48 ms |
3264 KB |
Output is correct |
48 |
Correct |
52 ms |
3256 KB |
Output is correct |
49 |
Correct |
65 ms |
3212 KB |
Output is correct |
50 |
Correct |
1 ms |
204 KB |
Output is correct |
51 |
Correct |
52 ms |
3272 KB |
Output is correct |
52 |
Correct |
355 ms |
36632 KB |
Output is correct |
53 |
Correct |
364 ms |
36060 KB |
Output is correct |
54 |
Correct |
321 ms |
34072 KB |
Output is correct |
55 |
Correct |
319 ms |
33904 KB |
Output is correct |
56 |
Correct |
366 ms |
34644 KB |
Output is correct |
57 |
Correct |
85 ms |
26024 KB |
Output is correct |
58 |
Correct |
88 ms |
25896 KB |
Output is correct |
59 |
Correct |
123 ms |
25836 KB |
Output is correct |
60 |
Correct |
96 ms |
25760 KB |
Output is correct |
61 |
Correct |
121 ms |
27696 KB |
Output is correct |
62 |
Correct |
211 ms |
33584 KB |
Output is correct |
63 |
Correct |
242 ms |
36076 KB |
Output is correct |
64 |
Correct |
220 ms |
33536 KB |
Output is correct |
65 |
Correct |
227 ms |
34236 KB |
Output is correct |
66 |
Correct |
251 ms |
35804 KB |
Output is correct |
67 |
Correct |
1 ms |
204 KB |
Output is correct |
68 |
Correct |
87 ms |
26276 KB |
Output is correct |
69 |
Correct |
632 ms |
38136 KB |
Output is correct |
70 |
Correct |
655 ms |
39612 KB |
Output is correct |
71 |
Correct |
583 ms |
37308 KB |
Output is correct |
72 |
Correct |
626 ms |
38716 KB |
Output is correct |
73 |
Correct |
649 ms |
38288 KB |
Output is correct |
74 |
Correct |
432 ms |
31672 KB |
Output is correct |
75 |
Correct |
430 ms |
29560 KB |
Output is correct |
76 |
Correct |
408 ms |
31780 KB |
Output is correct |
77 |
Correct |
351 ms |
29368 KB |
Output is correct |
78 |
Correct |
402 ms |
29848 KB |
Output is correct |
79 |
Correct |
341 ms |
38720 KB |
Output is correct |
80 |
Correct |
330 ms |
36400 KB |
Output is correct |
81 |
Correct |
297 ms |
36288 KB |
Output is correct |
82 |
Correct |
326 ms |
38064 KB |
Output is correct |
83 |
Correct |
288 ms |
36372 KB |
Output is correct |
84 |
Correct |
1 ms |
204 KB |
Output is correct |
85 |
Correct |
169 ms |
29428 KB |
Output is correct |