#include <bits/stdc++.h>
#define int int64_t
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;
using ar = array<int, 2>;
using ar3 = array<int, 3>;
const int64_t INF = 1ll << 60;
const int N = 2e5 + 5;
const int LG = 20;
namespace RMQ {
ar mn[LG][N];
void build(ar a[], int n) {
FOR(i, 0, n) mn[0][i] = a[i];
FOR(l, 0, LG - 1) for (int i = 0; i + (2 << l) <= n; ++i)
mn[l + 1][i] = min(mn[l][i], mn[l][i + (1 << l)]);
}
int get(int l, int r) { //[l, r)
int t = __lg(r - l);
return min(mn[t][l], mn[t][r - (1 << t)])[1];
}
}
int pr[N][LG], h[N], D[2 * N], loc[N], n, q;
ar3 it[N];
ar en[2 * N];
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> q;
FOR(i, 0, n) {
cin >> it[i][0] >> it[i][1];
it[i][2] = i;
D[i << 1] = it[i][0];
D[i << 1 | 1] = it[i][1];
}
fill(en, en + 2 * n, ar{INF, INF});
sort(D, D + 2 * n);
sort(it, it + n, [](const auto &A, const auto &B) {
if (A[0] != B[0]) return A[0] < B[0];
return A[2] < B[2];
});
FOR(i, 0, n) {
it[i][0] = lower_bound(D, D + 2 * n, it[i][0]) - D;
it[i][1] = lower_bound(D, D + 2 * n, it[i][1]) - D;
en[it[i][1]] = min(en[it[i][1]], ar{it[i][0], i});
loc[it[i][2]] = i;
}
RMQ::build(en, 2 * n);
FOR(i, 0, n) {
pr[i][0] = i;
auto [l, r, x] = it[i];
int idx = RMQ::get(l, r + 1);
if (idx == INF) exit(0);
assert(idx <= i);
if (idx != i) {
h[i] = h[idx] + 1;
pr[i][0] = idx;
FOR(l, 0, LG - 1)
pr[i][l + 1] = pr[pr[i][l]][l];
}
}
while (q--) {
int s, e;
cin >> s >> e;
--s, --e;
s = loc[s], e = loc[e];
if (it[e][1] < it[s][0] || it[s][1] > it[e][1]) cout << "impossible\n";
else if (s == e) cout << 0 << '\n';
else if (it[s][1] < it[e][0]) {
int sr = it[s][1], v = e;
for (int i = LG - 1; i >= 0; --i) {
int l = it[pr[v][i]][0];
if (l > sr)
v = pr[v][i];
}
v = pr[v][0];
if (it[v][0] > sr) cout << "impossible\n";
else cout << h[e] - h[v] + 1 << '\n';
}
else cout << "1\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
18780 KB |
Output is correct |
2 |
Correct |
111 ms |
87628 KB |
Output is correct |
3 |
Correct |
116 ms |
88568 KB |
Output is correct |
4 |
Correct |
155 ms |
88476 KB |
Output is correct |
5 |
Correct |
112 ms |
88716 KB |
Output is correct |
6 |
Correct |
110 ms |
88544 KB |
Output is correct |
7 |
Correct |
123 ms |
88636 KB |
Output is correct |
8 |
Correct |
105 ms |
88220 KB |
Output is correct |
9 |
Correct |
112 ms |
88140 KB |
Output is correct |
10 |
Correct |
202 ms |
89084 KB |
Output is correct |
11 |
Correct |
131 ms |
89992 KB |
Output is correct |
12 |
Correct |
84 ms |
88668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16848 KB |
Output is correct |
2 |
Correct |
3 ms |
18780 KB |
Output is correct |
3 |
Correct |
5 ms |
31068 KB |
Output is correct |
4 |
Correct |
6 ms |
31068 KB |
Output is correct |
5 |
Correct |
5 ms |
31068 KB |
Output is correct |
6 |
Correct |
5 ms |
31068 KB |
Output is correct |
7 |
Correct |
5 ms |
31068 KB |
Output is correct |
8 |
Correct |
6 ms |
31200 KB |
Output is correct |
9 |
Correct |
5 ms |
31068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16848 KB |
Output is correct |
2 |
Correct |
3 ms |
18780 KB |
Output is correct |
3 |
Correct |
5 ms |
31068 KB |
Output is correct |
4 |
Correct |
6 ms |
31068 KB |
Output is correct |
5 |
Correct |
5 ms |
31068 KB |
Output is correct |
6 |
Correct |
5 ms |
31068 KB |
Output is correct |
7 |
Correct |
5 ms |
31068 KB |
Output is correct |
8 |
Correct |
6 ms |
31200 KB |
Output is correct |
9 |
Correct |
5 ms |
31068 KB |
Output is correct |
10 |
Correct |
3 ms |
16732 KB |
Output is correct |
11 |
Correct |
3 ms |
18780 KB |
Output is correct |
12 |
Correct |
5 ms |
31068 KB |
Output is correct |
13 |
Correct |
5 ms |
31068 KB |
Output is correct |
14 |
Correct |
6 ms |
31272 KB |
Output is correct |
15 |
Correct |
5 ms |
31196 KB |
Output is correct |
16 |
Correct |
5 ms |
31068 KB |
Output is correct |
17 |
Correct |
6 ms |
31268 KB |
Output is correct |
18 |
Correct |
5 ms |
31292 KB |
Output is correct |
19 |
Correct |
41 ms |
40824 KB |
Output is correct |
20 |
Correct |
46 ms |
40856 KB |
Output is correct |
21 |
Correct |
39 ms |
41144 KB |
Output is correct |
22 |
Correct |
33 ms |
41196 KB |
Output is correct |
23 |
Correct |
34 ms |
40900 KB |
Output is correct |
24 |
Correct |
30 ms |
41012 KB |
Output is correct |
25 |
Correct |
36 ms |
40632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16848 KB |
Output is correct |
2 |
Correct |
3 ms |
18780 KB |
Output is correct |
3 |
Correct |
5 ms |
31068 KB |
Output is correct |
4 |
Correct |
6 ms |
31068 KB |
Output is correct |
5 |
Correct |
5 ms |
31068 KB |
Output is correct |
6 |
Correct |
5 ms |
31068 KB |
Output is correct |
7 |
Correct |
5 ms |
31068 KB |
Output is correct |
8 |
Correct |
6 ms |
31200 KB |
Output is correct |
9 |
Correct |
5 ms |
31068 KB |
Output is correct |
10 |
Correct |
3 ms |
16732 KB |
Output is correct |
11 |
Correct |
3 ms |
18896 KB |
Output is correct |
12 |
Correct |
6 ms |
31064 KB |
Output is correct |
13 |
Correct |
5 ms |
31068 KB |
Output is correct |
14 |
Correct |
5 ms |
31068 KB |
Output is correct |
15 |
Correct |
5 ms |
31064 KB |
Output is correct |
16 |
Correct |
5 ms |
31276 KB |
Output is correct |
17 |
Correct |
5 ms |
31068 KB |
Output is correct |
18 |
Correct |
5 ms |
31068 KB |
Output is correct |
19 |
Correct |
85 ms |
86248 KB |
Output is correct |
20 |
Correct |
82 ms |
86352 KB |
Output is correct |
21 |
Correct |
91 ms |
87628 KB |
Output is correct |
22 |
Correct |
81 ms |
87636 KB |
Output is correct |
23 |
Correct |
85 ms |
87120 KB |
Output is correct |
24 |
Correct |
87 ms |
87616 KB |
Output is correct |
25 |
Correct |
50 ms |
86984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
91668 KB |
Output is correct |
2 |
Correct |
119 ms |
92488 KB |
Output is correct |
3 |
Correct |
120 ms |
92456 KB |
Output is correct |
4 |
Correct |
96 ms |
92728 KB |
Output is correct |
5 |
Correct |
143 ms |
92756 KB |
Output is correct |
6 |
Correct |
163 ms |
92488 KB |
Output is correct |
7 |
Correct |
154 ms |
92620 KB |
Output is correct |
8 |
Correct |
120 ms |
92664 KB |
Output is correct |
9 |
Correct |
78 ms |
91412 KB |
Output is correct |
10 |
Correct |
108 ms |
92992 KB |
Output is correct |
11 |
Correct |
116 ms |
92664 KB |
Output is correct |
12 |
Correct |
110 ms |
92352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
18780 KB |
Output is correct |
2 |
Correct |
111 ms |
87628 KB |
Output is correct |
3 |
Correct |
116 ms |
88568 KB |
Output is correct |
4 |
Correct |
155 ms |
88476 KB |
Output is correct |
5 |
Correct |
112 ms |
88716 KB |
Output is correct |
6 |
Correct |
110 ms |
88544 KB |
Output is correct |
7 |
Correct |
123 ms |
88636 KB |
Output is correct |
8 |
Correct |
105 ms |
88220 KB |
Output is correct |
9 |
Correct |
112 ms |
88140 KB |
Output is correct |
10 |
Correct |
202 ms |
89084 KB |
Output is correct |
11 |
Correct |
131 ms |
89992 KB |
Output is correct |
12 |
Correct |
84 ms |
88668 KB |
Output is correct |
13 |
Correct |
3 ms |
16848 KB |
Output is correct |
14 |
Correct |
3 ms |
18780 KB |
Output is correct |
15 |
Correct |
5 ms |
31068 KB |
Output is correct |
16 |
Correct |
6 ms |
31068 KB |
Output is correct |
17 |
Correct |
5 ms |
31068 KB |
Output is correct |
18 |
Correct |
5 ms |
31068 KB |
Output is correct |
19 |
Correct |
5 ms |
31068 KB |
Output is correct |
20 |
Correct |
6 ms |
31200 KB |
Output is correct |
21 |
Correct |
5 ms |
31068 KB |
Output is correct |
22 |
Correct |
3 ms |
16732 KB |
Output is correct |
23 |
Correct |
3 ms |
18780 KB |
Output is correct |
24 |
Correct |
5 ms |
31068 KB |
Output is correct |
25 |
Correct |
5 ms |
31068 KB |
Output is correct |
26 |
Correct |
6 ms |
31272 KB |
Output is correct |
27 |
Correct |
5 ms |
31196 KB |
Output is correct |
28 |
Correct |
5 ms |
31068 KB |
Output is correct |
29 |
Correct |
6 ms |
31268 KB |
Output is correct |
30 |
Correct |
5 ms |
31292 KB |
Output is correct |
31 |
Correct |
41 ms |
40824 KB |
Output is correct |
32 |
Correct |
46 ms |
40856 KB |
Output is correct |
33 |
Correct |
39 ms |
41144 KB |
Output is correct |
34 |
Correct |
33 ms |
41196 KB |
Output is correct |
35 |
Correct |
34 ms |
40900 KB |
Output is correct |
36 |
Correct |
30 ms |
41012 KB |
Output is correct |
37 |
Correct |
36 ms |
40632 KB |
Output is correct |
38 |
Correct |
3 ms |
16732 KB |
Output is correct |
39 |
Correct |
3 ms |
18896 KB |
Output is correct |
40 |
Correct |
6 ms |
31064 KB |
Output is correct |
41 |
Correct |
5 ms |
31068 KB |
Output is correct |
42 |
Correct |
5 ms |
31068 KB |
Output is correct |
43 |
Correct |
5 ms |
31064 KB |
Output is correct |
44 |
Correct |
5 ms |
31276 KB |
Output is correct |
45 |
Correct |
5 ms |
31068 KB |
Output is correct |
46 |
Correct |
5 ms |
31068 KB |
Output is correct |
47 |
Correct |
85 ms |
86248 KB |
Output is correct |
48 |
Correct |
82 ms |
86352 KB |
Output is correct |
49 |
Correct |
91 ms |
87628 KB |
Output is correct |
50 |
Correct |
81 ms |
87636 KB |
Output is correct |
51 |
Correct |
85 ms |
87120 KB |
Output is correct |
52 |
Correct |
87 ms |
87616 KB |
Output is correct |
53 |
Correct |
50 ms |
86984 KB |
Output is correct |
54 |
Correct |
107 ms |
91668 KB |
Output is correct |
55 |
Correct |
119 ms |
92488 KB |
Output is correct |
56 |
Correct |
120 ms |
92456 KB |
Output is correct |
57 |
Correct |
96 ms |
92728 KB |
Output is correct |
58 |
Correct |
143 ms |
92756 KB |
Output is correct |
59 |
Correct |
163 ms |
92488 KB |
Output is correct |
60 |
Correct |
154 ms |
92620 KB |
Output is correct |
61 |
Correct |
120 ms |
92664 KB |
Output is correct |
62 |
Correct |
78 ms |
91412 KB |
Output is correct |
63 |
Correct |
108 ms |
92992 KB |
Output is correct |
64 |
Correct |
116 ms |
92664 KB |
Output is correct |
65 |
Correct |
110 ms |
92352 KB |
Output is correct |
66 |
Correct |
4 ms |
18780 KB |
Output is correct |
67 |
Correct |
107 ms |
90624 KB |
Output is correct |
68 |
Correct |
118 ms |
93172 KB |
Output is correct |
69 |
Correct |
126 ms |
93184 KB |
Output is correct |
70 |
Correct |
110 ms |
92500 KB |
Output is correct |
71 |
Correct |
108 ms |
93268 KB |
Output is correct |
72 |
Correct |
118 ms |
93360 KB |
Output is correct |
73 |
Correct |
94 ms |
92752 KB |
Output is correct |
74 |
Correct |
93 ms |
93012 KB |
Output is correct |
75 |
Correct |
175 ms |
93740 KB |
Output is correct |
76 |
Correct |
133 ms |
93524 KB |
Output is correct |
77 |
Correct |
72 ms |
93008 KB |
Output is correct |
78 |
Correct |
3 ms |
16732 KB |
Output is correct |
79 |
Correct |
7 ms |
31064 KB |
Output is correct |
80 |
Correct |
6 ms |
31068 KB |
Output is correct |
81 |
Correct |
6 ms |
31444 KB |
Output is correct |
82 |
Correct |
6 ms |
31068 KB |
Output is correct |
83 |
Correct |
6 ms |
31068 KB |
Output is correct |
84 |
Correct |
5 ms |
31068 KB |
Output is correct |
85 |
Correct |
5 ms |
31068 KB |
Output is correct |
86 |
Correct |
36 ms |
41052 KB |
Output is correct |
87 |
Correct |
37 ms |
41052 KB |
Output is correct |
88 |
Correct |
40 ms |
41380 KB |
Output is correct |
89 |
Correct |
31 ms |
41296 KB |
Output is correct |
90 |
Correct |
36 ms |
41304 KB |
Output is correct |
91 |
Correct |
30 ms |
41308 KB |
Output is correct |
92 |
Correct |
50 ms |
40788 KB |
Output is correct |
93 |
Correct |
75 ms |
91452 KB |
Output is correct |
94 |
Correct |
70 ms |
90640 KB |
Output is correct |
95 |
Correct |
80 ms |
91328 KB |
Output is correct |
96 |
Correct |
70 ms |
91512 KB |
Output is correct |
97 |
Correct |
75 ms |
91332 KB |
Output is correct |
98 |
Correct |
78 ms |
91444 KB |
Output is correct |
99 |
Correct |
56 ms |
90708 KB |
Output is correct |
100 |
Correct |
145 ms |
93268 KB |
Output is correct |
101 |
Correct |
172 ms |
93264 KB |
Output is correct |
102 |
Correct |
113 ms |
93600 KB |
Output is correct |
103 |
Correct |
115 ms |
93128 KB |
Output is correct |
104 |
Correct |
123 ms |
92876 KB |
Output is correct |
105 |
Correct |
105 ms |
93016 KB |
Output is correct |
106 |
Correct |
122 ms |
92752 KB |
Output is correct |
107 |
Correct |
156 ms |
92620 KB |
Output is correct |
108 |
Correct |
111 ms |
93264 KB |
Output is correct |
109 |
Correct |
120 ms |
93300 KB |
Output is correct |
110 |
Correct |
117 ms |
93180 KB |
Output is correct |
111 |
Correct |
125 ms |
93220 KB |
Output is correct |