#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define fi first
#define se second
#define ld long double
#define pb push_back
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int N, Q;
vector<array<int, 2> > v;
struct segment {
const int dim = 1 << 19;
struct node {
int val, prop;
node(int a = -1, int b = -1) {
val = a;
prop = b;
}
};
vector<node> s = vector<node>(2 * dim);
void prop(int pos) {
if (pos >= dim || s[pos].prop == -1) return;
s[2 * pos].val = s[pos].prop;
s[2 * pos].prop = s[pos].prop;
s[2 * pos + 1].val = s[pos].prop;
s[2 * pos + 1].prop = s[pos].prop;
}
void upd(int pos, int l, int r, int a, int b, int k) {
prop(pos);
if (r < a || l > b) return;
if (a <= l && r <= b) {
s[pos].val = k;
s[pos].prop = k;
return;
}
int m = (l + r) / 2;
upd(2 * pos, l, m, a, b, k);
upd(2 * pos + 1, m + 1, r, a, b, k);
s[pos].val = max(s[2 * pos].val, s[2 * pos + 1].val);
s[pos].prop = -1;
}
int query(int pos, int l, int r, int a, int b) {
prop(pos);
if (r < a || l > b) return -1;
if (a <= l && r <= b) return s[pos].val;
int m = (l + r) / 2;
return max(query(2 * pos, l, m, a, b), query(2 * pos + 1, m + 1, r, a, b));
}
void upd(int a, int b, int k) {
upd(1, 0, dim - 1, a, b, k);
}
int query(int a, int b) {
return query(1, 0, dim - 1, a, b);
}
} seg;
int r[20][200010];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N;
vector<int> x;
for (int i = 0; i < N; i++) {
int a, b;
cin >> a >> b;
v.pb({a, b});
x.pb(a);
x.pb(b);
}
sort(x.begin(), x.end());
x.resize(unique(x.begin(), x.end()) - x.begin());
for (int i = 0; i < N; i++) {
v[i][0] = lower_bound(x.begin(), x.end(), v[i][0]) - x.begin();
v[i][1] = lower_bound(x.begin(), x.end(), v[i][1]) - x.begin();
}
for (int i = 0; i < N; i++) {
r[0][i] = seg.query(v[i][0], v[i][1]);
seg.upd(v[i][0], v[i][1], i);
if (i) r[0][i] = max(r[0][i], r[0][i - 1]);
}
for (int i = 1; i < 19; i++) {
for (int j = 0; j < N; j++) {
if (r[i - 1][j] == -1)
r[i][j] = -1;
else
r[i][j] = r[i - 1][r[i - 1][j]];
}
}
cin >> Q;
for (int i = 0; i < Q; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
int tot = 1;
int pos = b;
int q = 18;
while (q >= 0) {
if (r[q][pos] >= a) {
tot += (1 << q);
pos = r[q][pos];
}
q--;
}
cout << tot << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
392 ms |
51372 KB |
Output is correct |
2 |
Correct |
392 ms |
50900 KB |
Output is correct |
3 |
Correct |
394 ms |
50912 KB |
Output is correct |
4 |
Correct |
395 ms |
51572 KB |
Output is correct |
5 |
Correct |
411 ms |
52808 KB |
Output is correct |
6 |
Correct |
110 ms |
49560 KB |
Output is correct |
7 |
Correct |
116 ms |
50156 KB |
Output is correct |
8 |
Correct |
117 ms |
50492 KB |
Output is correct |
9 |
Correct |
120 ms |
51356 KB |
Output is correct |
10 |
Correct |
120 ms |
50216 KB |
Output is correct |
11 |
Correct |
247 ms |
52188 KB |
Output is correct |
12 |
Correct |
224 ms |
50084 KB |
Output is correct |
13 |
Correct |
217 ms |
49928 KB |
Output is correct |
14 |
Correct |
245 ms |
52176 KB |
Output is correct |
15 |
Correct |
229 ms |
51260 KB |
Output is correct |
16 |
Correct |
7 ms |
16852 KB |
Output is correct |
17 |
Correct |
111 ms |
51824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
17876 KB |
Output is correct |
2 |
Correct |
16 ms |
17876 KB |
Output is correct |
3 |
Correct |
16 ms |
17840 KB |
Output is correct |
4 |
Correct |
16 ms |
17876 KB |
Output is correct |
5 |
Correct |
16 ms |
17748 KB |
Output is correct |
6 |
Correct |
12 ms |
17852 KB |
Output is correct |
7 |
Correct |
12 ms |
17836 KB |
Output is correct |
8 |
Correct |
13 ms |
17824 KB |
Output is correct |
9 |
Correct |
11 ms |
17868 KB |
Output is correct |
10 |
Correct |
12 ms |
17888 KB |
Output is correct |
11 |
Correct |
13 ms |
17876 KB |
Output is correct |
12 |
Correct |
12 ms |
17776 KB |
Output is correct |
13 |
Correct |
12 ms |
17852 KB |
Output is correct |
14 |
Correct |
13 ms |
17876 KB |
Output is correct |
15 |
Correct |
16 ms |
17824 KB |
Output is correct |
16 |
Correct |
7 ms |
16852 KB |
Output is correct |
17 |
Correct |
11 ms |
17876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
17876 KB |
Output is correct |
2 |
Correct |
16 ms |
17876 KB |
Output is correct |
3 |
Correct |
16 ms |
17840 KB |
Output is correct |
4 |
Correct |
16 ms |
17876 KB |
Output is correct |
5 |
Correct |
16 ms |
17748 KB |
Output is correct |
6 |
Correct |
12 ms |
17852 KB |
Output is correct |
7 |
Correct |
12 ms |
17836 KB |
Output is correct |
8 |
Correct |
13 ms |
17824 KB |
Output is correct |
9 |
Correct |
11 ms |
17868 KB |
Output is correct |
10 |
Correct |
12 ms |
17888 KB |
Output is correct |
11 |
Correct |
13 ms |
17876 KB |
Output is correct |
12 |
Correct |
12 ms |
17776 KB |
Output is correct |
13 |
Correct |
12 ms |
17852 KB |
Output is correct |
14 |
Correct |
13 ms |
17876 KB |
Output is correct |
15 |
Correct |
16 ms |
17824 KB |
Output is correct |
16 |
Correct |
7 ms |
16852 KB |
Output is correct |
17 |
Correct |
11 ms |
17876 KB |
Output is correct |
18 |
Correct |
92 ms |
18740 KB |
Output is correct |
19 |
Correct |
74 ms |
18764 KB |
Output is correct |
20 |
Correct |
91 ms |
18804 KB |
Output is correct |
21 |
Correct |
75 ms |
18620 KB |
Output is correct |
22 |
Correct |
81 ms |
18644 KB |
Output is correct |
23 |
Correct |
69 ms |
18680 KB |
Output is correct |
24 |
Correct |
77 ms |
18852 KB |
Output is correct |
25 |
Correct |
75 ms |
18640 KB |
Output is correct |
26 |
Correct |
71 ms |
18672 KB |
Output is correct |
27 |
Correct |
68 ms |
18500 KB |
Output is correct |
28 |
Correct |
51 ms |
18236 KB |
Output is correct |
29 |
Correct |
53 ms |
18216 KB |
Output is correct |
30 |
Correct |
54 ms |
18192 KB |
Output is correct |
31 |
Correct |
54 ms |
18204 KB |
Output is correct |
32 |
Correct |
53 ms |
18224 KB |
Output is correct |
33 |
Correct |
7 ms |
16852 KB |
Output is correct |
34 |
Correct |
55 ms |
19020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
417 ms |
52888 KB |
Output is correct |
2 |
Correct |
411 ms |
52260 KB |
Output is correct |
3 |
Correct |
378 ms |
49812 KB |
Output is correct |
4 |
Correct |
374 ms |
49628 KB |
Output is correct |
5 |
Correct |
386 ms |
50448 KB |
Output is correct |
6 |
Correct |
110 ms |
49828 KB |
Output is correct |
7 |
Correct |
112 ms |
49548 KB |
Output is correct |
8 |
Correct |
120 ms |
49928 KB |
Output is correct |
9 |
Correct |
114 ms |
49512 KB |
Output is correct |
10 |
Correct |
140 ms |
52220 KB |
Output is correct |
11 |
Correct |
234 ms |
49700 KB |
Output is correct |
12 |
Correct |
244 ms |
52900 KB |
Output is correct |
13 |
Correct |
229 ms |
49720 KB |
Output is correct |
14 |
Correct |
239 ms |
50792 KB |
Output is correct |
15 |
Correct |
244 ms |
52780 KB |
Output is correct |
16 |
Correct |
8 ms |
16852 KB |
Output is correct |
17 |
Correct |
106 ms |
50376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
392 ms |
51372 KB |
Output is correct |
2 |
Correct |
392 ms |
50900 KB |
Output is correct |
3 |
Correct |
394 ms |
50912 KB |
Output is correct |
4 |
Correct |
395 ms |
51572 KB |
Output is correct |
5 |
Correct |
411 ms |
52808 KB |
Output is correct |
6 |
Correct |
110 ms |
49560 KB |
Output is correct |
7 |
Correct |
116 ms |
50156 KB |
Output is correct |
8 |
Correct |
117 ms |
50492 KB |
Output is correct |
9 |
Correct |
120 ms |
51356 KB |
Output is correct |
10 |
Correct |
120 ms |
50216 KB |
Output is correct |
11 |
Correct |
247 ms |
52188 KB |
Output is correct |
12 |
Correct |
224 ms |
50084 KB |
Output is correct |
13 |
Correct |
217 ms |
49928 KB |
Output is correct |
14 |
Correct |
245 ms |
52176 KB |
Output is correct |
15 |
Correct |
229 ms |
51260 KB |
Output is correct |
16 |
Correct |
7 ms |
16852 KB |
Output is correct |
17 |
Correct |
111 ms |
51824 KB |
Output is correct |
18 |
Correct |
17 ms |
17876 KB |
Output is correct |
19 |
Correct |
16 ms |
17876 KB |
Output is correct |
20 |
Correct |
16 ms |
17840 KB |
Output is correct |
21 |
Correct |
16 ms |
17876 KB |
Output is correct |
22 |
Correct |
16 ms |
17748 KB |
Output is correct |
23 |
Correct |
12 ms |
17852 KB |
Output is correct |
24 |
Correct |
12 ms |
17836 KB |
Output is correct |
25 |
Correct |
13 ms |
17824 KB |
Output is correct |
26 |
Correct |
11 ms |
17868 KB |
Output is correct |
27 |
Correct |
12 ms |
17888 KB |
Output is correct |
28 |
Correct |
13 ms |
17876 KB |
Output is correct |
29 |
Correct |
12 ms |
17776 KB |
Output is correct |
30 |
Correct |
12 ms |
17852 KB |
Output is correct |
31 |
Correct |
13 ms |
17876 KB |
Output is correct |
32 |
Correct |
16 ms |
17824 KB |
Output is correct |
33 |
Correct |
7 ms |
16852 KB |
Output is correct |
34 |
Correct |
11 ms |
17876 KB |
Output is correct |
35 |
Correct |
92 ms |
18740 KB |
Output is correct |
36 |
Correct |
74 ms |
18764 KB |
Output is correct |
37 |
Correct |
91 ms |
18804 KB |
Output is correct |
38 |
Correct |
75 ms |
18620 KB |
Output is correct |
39 |
Correct |
81 ms |
18644 KB |
Output is correct |
40 |
Correct |
69 ms |
18680 KB |
Output is correct |
41 |
Correct |
77 ms |
18852 KB |
Output is correct |
42 |
Correct |
75 ms |
18640 KB |
Output is correct |
43 |
Correct |
71 ms |
18672 KB |
Output is correct |
44 |
Correct |
68 ms |
18500 KB |
Output is correct |
45 |
Correct |
51 ms |
18236 KB |
Output is correct |
46 |
Correct |
53 ms |
18216 KB |
Output is correct |
47 |
Correct |
54 ms |
18192 KB |
Output is correct |
48 |
Correct |
54 ms |
18204 KB |
Output is correct |
49 |
Correct |
53 ms |
18224 KB |
Output is correct |
50 |
Correct |
7 ms |
16852 KB |
Output is correct |
51 |
Correct |
55 ms |
19020 KB |
Output is correct |
52 |
Correct |
417 ms |
52888 KB |
Output is correct |
53 |
Correct |
411 ms |
52260 KB |
Output is correct |
54 |
Correct |
378 ms |
49812 KB |
Output is correct |
55 |
Correct |
374 ms |
49628 KB |
Output is correct |
56 |
Correct |
386 ms |
50448 KB |
Output is correct |
57 |
Correct |
110 ms |
49828 KB |
Output is correct |
58 |
Correct |
112 ms |
49548 KB |
Output is correct |
59 |
Correct |
120 ms |
49928 KB |
Output is correct |
60 |
Correct |
114 ms |
49512 KB |
Output is correct |
61 |
Correct |
140 ms |
52220 KB |
Output is correct |
62 |
Correct |
234 ms |
49700 KB |
Output is correct |
63 |
Correct |
244 ms |
52900 KB |
Output is correct |
64 |
Correct |
229 ms |
49720 KB |
Output is correct |
65 |
Correct |
239 ms |
50792 KB |
Output is correct |
66 |
Correct |
244 ms |
52780 KB |
Output is correct |
67 |
Correct |
8 ms |
16852 KB |
Output is correct |
68 |
Correct |
106 ms |
50376 KB |
Output is correct |
69 |
Correct |
690 ms |
57576 KB |
Output is correct |
70 |
Correct |
703 ms |
59728 KB |
Output is correct |
71 |
Correct |
682 ms |
56496 KB |
Output is correct |
72 |
Correct |
688 ms |
58468 KB |
Output is correct |
73 |
Correct |
697 ms |
57896 KB |
Output is correct |
74 |
Correct |
454 ms |
60024 KB |
Output is correct |
75 |
Correct |
401 ms |
57140 KB |
Output is correct |
76 |
Correct |
402 ms |
60296 KB |
Output is correct |
77 |
Correct |
389 ms |
56860 KB |
Output is correct |
78 |
Correct |
402 ms |
57560 KB |
Output is correct |
79 |
Correct |
341 ms |
58876 KB |
Output is correct |
80 |
Correct |
315 ms |
55788 KB |
Output is correct |
81 |
Correct |
317 ms |
55572 KB |
Output is correct |
82 |
Correct |
336 ms |
57780 KB |
Output is correct |
83 |
Correct |
319 ms |
55920 KB |
Output is correct |
84 |
Correct |
7 ms |
16852 KB |
Output is correct |
85 |
Correct |
167 ms |
56932 KB |
Output is correct |