#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 != 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 |
99 ms |
90236 KB |
Output is correct |
3 |
Correct |
116 ms |
89976 KB |
Output is correct |
4 |
Correct |
119 ms |
89936 KB |
Output is correct |
5 |
Correct |
110 ms |
90072 KB |
Output is correct |
6 |
Correct |
102 ms |
90252 KB |
Output is correct |
7 |
Correct |
105 ms |
90156 KB |
Output is correct |
8 |
Correct |
88 ms |
89816 KB |
Output is correct |
9 |
Correct |
88 ms |
89936 KB |
Output is correct |
10 |
Correct |
124 ms |
90524 KB |
Output is correct |
11 |
Correct |
117 ms |
90492 KB |
Output is correct |
12 |
Correct |
71 ms |
90708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16728 KB |
Output is correct |
2 |
Correct |
3 ms |
18780 KB |
Output is correct |
3 |
Correct |
5 ms |
31252 KB |
Output is correct |
4 |
Correct |
5 ms |
31068 KB |
Output is correct |
5 |
Correct |
5 ms |
31064 KB |
Output is correct |
6 |
Correct |
5 ms |
31192 KB |
Output is correct |
7 |
Correct |
5 ms |
31068 KB |
Output is correct |
8 |
Correct |
5 ms |
31232 KB |
Output is correct |
9 |
Correct |
6 ms |
31064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16728 KB |
Output is correct |
2 |
Correct |
3 ms |
18780 KB |
Output is correct |
3 |
Correct |
5 ms |
31252 KB |
Output is correct |
4 |
Correct |
5 ms |
31068 KB |
Output is correct |
5 |
Correct |
5 ms |
31064 KB |
Output is correct |
6 |
Correct |
5 ms |
31192 KB |
Output is correct |
7 |
Correct |
5 ms |
31068 KB |
Output is correct |
8 |
Correct |
5 ms |
31232 KB |
Output is correct |
9 |
Correct |
6 ms |
31064 KB |
Output is correct |
10 |
Correct |
3 ms |
16732 KB |
Output is correct |
11 |
Correct |
3 ms |
18780 KB |
Output is correct |
12 |
Correct |
6 ms |
31068 KB |
Output is correct |
13 |
Correct |
5 ms |
31068 KB |
Output is correct |
14 |
Correct |
6 ms |
31188 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 |
31236 KB |
Output is correct |
18 |
Correct |
5 ms |
31320 KB |
Output is correct |
19 |
Correct |
36 ms |
40068 KB |
Output is correct |
20 |
Correct |
36 ms |
40020 KB |
Output is correct |
21 |
Correct |
37 ms |
40528 KB |
Output is correct |
22 |
Correct |
29 ms |
40280 KB |
Output is correct |
23 |
Correct |
25 ms |
40276 KB |
Output is correct |
24 |
Correct |
22 ms |
40160 KB |
Output is correct |
25 |
Correct |
36 ms |
39760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16728 KB |
Output is correct |
2 |
Correct |
3 ms |
18780 KB |
Output is correct |
3 |
Correct |
5 ms |
31252 KB |
Output is correct |
4 |
Correct |
5 ms |
31068 KB |
Output is correct |
5 |
Correct |
5 ms |
31064 KB |
Output is correct |
6 |
Correct |
5 ms |
31192 KB |
Output is correct |
7 |
Correct |
5 ms |
31068 KB |
Output is correct |
8 |
Correct |
5 ms |
31232 KB |
Output is correct |
9 |
Correct |
6 ms |
31064 KB |
Output is correct |
10 |
Correct |
3 ms |
16728 KB |
Output is correct |
11 |
Correct |
3 ms |
18780 KB |
Output is correct |
12 |
Correct |
6 ms |
31324 KB |
Output is correct |
13 |
Correct |
6 ms |
31068 KB |
Output is correct |
14 |
Correct |
5 ms |
31064 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 |
31256 KB |
Output is correct |
18 |
Correct |
6 ms |
31068 KB |
Output is correct |
19 |
Correct |
71 ms |
89580 KB |
Output is correct |
20 |
Correct |
68 ms |
89428 KB |
Output is correct |
21 |
Correct |
77 ms |
89424 KB |
Output is correct |
22 |
Correct |
69 ms |
89424 KB |
Output is correct |
23 |
Correct |
70 ms |
89424 KB |
Output is correct |
24 |
Correct |
72 ms |
89732 KB |
Output is correct |
25 |
Correct |
48 ms |
89564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
90348 KB |
Output is correct |
2 |
Correct |
101 ms |
90068 KB |
Output is correct |
3 |
Correct |
118 ms |
90196 KB |
Output is correct |
4 |
Correct |
94 ms |
89748 KB |
Output is correct |
5 |
Correct |
144 ms |
90816 KB |
Output is correct |
6 |
Correct |
132 ms |
90196 KB |
Output is correct |
7 |
Correct |
137 ms |
90196 KB |
Output is correct |
8 |
Correct |
115 ms |
90496 KB |
Output is correct |
9 |
Correct |
73 ms |
89612 KB |
Output is correct |
10 |
Correct |
100 ms |
89940 KB |
Output is correct |
11 |
Correct |
105 ms |
89808 KB |
Output is correct |
12 |
Correct |
104 ms |
89940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
18780 KB |
Output is correct |
2 |
Correct |
99 ms |
90236 KB |
Output is correct |
3 |
Correct |
116 ms |
89976 KB |
Output is correct |
4 |
Correct |
119 ms |
89936 KB |
Output is correct |
5 |
Correct |
110 ms |
90072 KB |
Output is correct |
6 |
Correct |
102 ms |
90252 KB |
Output is correct |
7 |
Correct |
105 ms |
90156 KB |
Output is correct |
8 |
Correct |
88 ms |
89816 KB |
Output is correct |
9 |
Correct |
88 ms |
89936 KB |
Output is correct |
10 |
Correct |
124 ms |
90524 KB |
Output is correct |
11 |
Correct |
117 ms |
90492 KB |
Output is correct |
12 |
Correct |
71 ms |
90708 KB |
Output is correct |
13 |
Correct |
3 ms |
16728 KB |
Output is correct |
14 |
Correct |
3 ms |
18780 KB |
Output is correct |
15 |
Correct |
5 ms |
31252 KB |
Output is correct |
16 |
Correct |
5 ms |
31068 KB |
Output is correct |
17 |
Correct |
5 ms |
31064 KB |
Output is correct |
18 |
Correct |
5 ms |
31192 KB |
Output is correct |
19 |
Correct |
5 ms |
31068 KB |
Output is correct |
20 |
Correct |
5 ms |
31232 KB |
Output is correct |
21 |
Correct |
6 ms |
31064 KB |
Output is correct |
22 |
Correct |
3 ms |
16732 KB |
Output is correct |
23 |
Correct |
3 ms |
18780 KB |
Output is correct |
24 |
Correct |
6 ms |
31068 KB |
Output is correct |
25 |
Correct |
5 ms |
31068 KB |
Output is correct |
26 |
Correct |
6 ms |
31188 KB |
Output is correct |
27 |
Correct |
5 ms |
31068 KB |
Output is correct |
28 |
Correct |
6 ms |
31068 KB |
Output is correct |
29 |
Correct |
5 ms |
31236 KB |
Output is correct |
30 |
Correct |
5 ms |
31320 KB |
Output is correct |
31 |
Correct |
36 ms |
40068 KB |
Output is correct |
32 |
Correct |
36 ms |
40020 KB |
Output is correct |
33 |
Correct |
37 ms |
40528 KB |
Output is correct |
34 |
Correct |
29 ms |
40280 KB |
Output is correct |
35 |
Correct |
25 ms |
40276 KB |
Output is correct |
36 |
Correct |
22 ms |
40160 KB |
Output is correct |
37 |
Correct |
36 ms |
39760 KB |
Output is correct |
38 |
Correct |
3 ms |
16728 KB |
Output is correct |
39 |
Correct |
3 ms |
18780 KB |
Output is correct |
40 |
Correct |
6 ms |
31324 KB |
Output is correct |
41 |
Correct |
6 ms |
31068 KB |
Output is correct |
42 |
Correct |
5 ms |
31064 KB |
Output is correct |
43 |
Correct |
5 ms |
31068 KB |
Output is correct |
44 |
Correct |
6 ms |
31068 KB |
Output is correct |
45 |
Correct |
5 ms |
31256 KB |
Output is correct |
46 |
Correct |
6 ms |
31068 KB |
Output is correct |
47 |
Correct |
71 ms |
89580 KB |
Output is correct |
48 |
Correct |
68 ms |
89428 KB |
Output is correct |
49 |
Correct |
77 ms |
89424 KB |
Output is correct |
50 |
Correct |
69 ms |
89424 KB |
Output is correct |
51 |
Correct |
70 ms |
89424 KB |
Output is correct |
52 |
Correct |
72 ms |
89732 KB |
Output is correct |
53 |
Correct |
48 ms |
89564 KB |
Output is correct |
54 |
Correct |
107 ms |
90348 KB |
Output is correct |
55 |
Correct |
101 ms |
90068 KB |
Output is correct |
56 |
Correct |
118 ms |
90196 KB |
Output is correct |
57 |
Correct |
94 ms |
89748 KB |
Output is correct |
58 |
Correct |
144 ms |
90816 KB |
Output is correct |
59 |
Correct |
132 ms |
90196 KB |
Output is correct |
60 |
Correct |
137 ms |
90196 KB |
Output is correct |
61 |
Correct |
115 ms |
90496 KB |
Output is correct |
62 |
Correct |
73 ms |
89612 KB |
Output is correct |
63 |
Correct |
100 ms |
89940 KB |
Output is correct |
64 |
Correct |
105 ms |
89808 KB |
Output is correct |
65 |
Correct |
104 ms |
89940 KB |
Output is correct |
66 |
Correct |
3 ms |
18780 KB |
Output is correct |
67 |
Correct |
99 ms |
90200 KB |
Output is correct |
68 |
Correct |
101 ms |
90236 KB |
Output is correct |
69 |
Correct |
112 ms |
89980 KB |
Output is correct |
70 |
Correct |
130 ms |
90236 KB |
Output is correct |
71 |
Correct |
100 ms |
90272 KB |
Output is correct |
72 |
Correct |
99 ms |
90244 KB |
Output is correct |
73 |
Correct |
88 ms |
89936 KB |
Output is correct |
74 |
Correct |
92 ms |
89772 KB |
Output is correct |
75 |
Correct |
118 ms |
90452 KB |
Output is correct |
76 |
Correct |
126 ms |
90656 KB |
Output is correct |
77 |
Correct |
72 ms |
90708 KB |
Output is correct |
78 |
Correct |
3 ms |
16732 KB |
Output is correct |
79 |
Correct |
6 ms |
31064 KB |
Output is correct |
80 |
Correct |
5 ms |
31068 KB |
Output is correct |
81 |
Correct |
6 ms |
31068 KB |
Output is correct |
82 |
Correct |
7 ms |
31320 KB |
Output is correct |
83 |
Correct |
6 ms |
31064 KB |
Output is correct |
84 |
Correct |
5 ms |
31068 KB |
Output is correct |
85 |
Correct |
6 ms |
31068 KB |
Output is correct |
86 |
Correct |
35 ms |
40056 KB |
Output is correct |
87 |
Correct |
37 ms |
40028 KB |
Output is correct |
88 |
Correct |
34 ms |
40272 KB |
Output is correct |
89 |
Correct |
29 ms |
40284 KB |
Output is correct |
90 |
Correct |
26 ms |
40284 KB |
Output is correct |
91 |
Correct |
22 ms |
40284 KB |
Output is correct |
92 |
Correct |
35 ms |
39772 KB |
Output is correct |
93 |
Correct |
70 ms |
89424 KB |
Output is correct |
94 |
Correct |
66 ms |
89424 KB |
Output is correct |
95 |
Correct |
76 ms |
89488 KB |
Output is correct |
96 |
Correct |
74 ms |
89424 KB |
Output is correct |
97 |
Correct |
75 ms |
89504 KB |
Output is correct |
98 |
Correct |
71 ms |
89428 KB |
Output is correct |
99 |
Correct |
53 ms |
89560 KB |
Output is correct |
100 |
Correct |
161 ms |
90152 KB |
Output is correct |
101 |
Correct |
134 ms |
90256 KB |
Output is correct |
102 |
Correct |
118 ms |
90508 KB |
Output is correct |
103 |
Correct |
103 ms |
89984 KB |
Output is correct |
104 |
Correct |
100 ms |
89680 KB |
Output is correct |
105 |
Correct |
101 ms |
89936 KB |
Output is correct |
106 |
Correct |
121 ms |
90192 KB |
Output is correct |
107 |
Correct |
135 ms |
90336 KB |
Output is correct |
108 |
Correct |
121 ms |
90292 KB |
Output is correct |
109 |
Correct |
108 ms |
90240 KB |
Output is correct |
110 |
Correct |
128 ms |
90192 KB |
Output is correct |
111 |
Correct |
126 ms |
90192 KB |
Output is correct |