#include <bits/stdc++.h>
using namespace std;
#define dbg(x) x
#define prt(x) dbg(cerr << x)
#define pv(x) dbg(cerr << #x << " = " << x << '\n')
#define pv2(x) dbg(cerr << #x << " = " << x.first << ',' << x.second << '\n')
#define parr(x) dbg(prt(#x << " = { "); for (auto y : x) prt(y << ' '); prt("}\n");)
#define parr2(x) dbg(prt(#x << " = { "); for (auto [y, z] : x) prt(y << ',' << z << " "); prt("}\n");)
#define parr2d(x) dbg(prt(#x << ":\n"); for (auto arr : x) {parr(arr);} prt('\n'));
#define parr2d2(x) dbg(prt(#x << ":\n"); for (auto arr : x) {parr2(arr);} prt('\n'));
/*
sort by right bound
out of everything with l[i] <= r[j] <= r[i]
with leftmost left bound
*/
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n, q;
cin >> n >> q;
vector<array<int, 2>> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i][0] >> a[i][1];
}
vector<int> ord(n);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&] (int x, int y) {
return a[x][1] < a[y][1] || (a[x][1] == a[y][1] && a[x][0] < a[y][0]);
});
vector<int> at(n);
for (int i = 0; i < n; i++) {
at[ord[i]] = i;
}
sort(a.begin(), a.end(), [&] (array<int, 2> a1, array<int, 2> a2) {
return a1[1] < a2[1] || (a1[1] == a2[1] && a1[0] < a2[0]);
});
vector<int> l(n), r(n);
for (int i = 0; i < n; i++) {
l[i] = a[i][0];
r[i] = a[i][1];
}
vector<vector<array<int, 2>>> rmq(20, vector<array<int, 2>>(n));
for (int i = 0; i < n; i++) {
rmq[0][i] = {l[i], i};
}
for (int j = 1; j < 20; j++) {
for (int i = 0; i < n - (1 << (j - 1)); i++) {
rmq[j][i] = min(rmq[j - 1][i], rmq[j - 1][i + (1 << (j - 1))]);
}
}
function<array<int, 2>(int, int)> mnq = [&] (int lb, int rb) {
int len = rb - lb + 1, mxbit = 31 - __builtin_clz(len);
return min(rmq[mxbit][lb], rmq[mxbit][rb - (1 << mxbit) + 1]);
};
vector<int> to(n);
for (int i = 0; i < n; i++) {
int lb = lower_bound(r.begin(), r.end(), l[i]) - r.begin();
int rb = upper_bound(r.begin(), r.end(), r[i]) - r.begin() - 1;
array<int, 2> best = mnq(lb, rb);
to[i] = best[1];
}
vector<vector<int>> lift(20, vector<int>(n, 0));
lift[0] = to;
for (int j = 1; j < 20; j++) {
for (int i = 0; i < n; i++) {
lift[j][i] = lift[j - 1][lift[j - 1][i]];
}
}
while (q--) {
int st, en;
cin >> st >> en;
st--; en--;
st = at[st]; en = at[en];
if (r[en] < r[st]) {
cout << "impossible\n";
continue;
} else if (st == en) {
cout << 0 << '\n';
continue;
} else if (l[en] <= r[st]) {
cout << 1 << '\n';
continue;
}
int ans = 0;
for (int bit = 19; bit >= 0; bit--) {
if (l[lift[bit][en]] > r[st]) {
ans += 1 << bit;
en = lift[bit][en];
}
}
en = to[en];
ans++;
if (l[en] > r[st]) {
cout << "impossible\n";
} else {
cout << ans + 1 << '\n';
}
}
}
/*
any observations help
check every line
IF YOUR LINES AREN'T WRONG
CHECK IF YOUR LINES ARE IN THE RIGHT ORDER
NEVER GIVE UP
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
68 ms |
27688 KB |
Output is correct |
3 |
Correct |
67 ms |
27712 KB |
Output is correct |
4 |
Correct |
73 ms |
27712 KB |
Output is correct |
5 |
Correct |
80 ms |
27712 KB |
Output is correct |
6 |
Correct |
70 ms |
27608 KB |
Output is correct |
7 |
Correct |
70 ms |
27712 KB |
Output is correct |
8 |
Correct |
65 ms |
28228 KB |
Output is correct |
9 |
Correct |
60 ms |
31296 KB |
Output is correct |
10 |
Correct |
81 ms |
31420 KB |
Output is correct |
11 |
Correct |
83 ms |
31176 KB |
Output is correct |
12 |
Correct |
54 ms |
30560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
456 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
23 ms |
3264 KB |
Output is correct |
20 |
Correct |
19 ms |
3160 KB |
Output is correct |
21 |
Correct |
24 ms |
3416 KB |
Output is correct |
22 |
Correct |
23 ms |
3420 KB |
Output is correct |
23 |
Correct |
17 ms |
3420 KB |
Output is correct |
24 |
Correct |
16 ms |
3412 KB |
Output is correct |
25 |
Correct |
18 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
50 ms |
28984 KB |
Output is correct |
20 |
Correct |
47 ms |
28228 KB |
Output is correct |
21 |
Correct |
54 ms |
28740 KB |
Output is correct |
22 |
Correct |
53 ms |
28988 KB |
Output is correct |
23 |
Correct |
52 ms |
28872 KB |
Output is correct |
24 |
Correct |
49 ms |
28992 KB |
Output is correct |
25 |
Correct |
33 ms |
28356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
27736 KB |
Output is correct |
2 |
Correct |
66 ms |
27608 KB |
Output is correct |
3 |
Correct |
66 ms |
27608 KB |
Output is correct |
4 |
Correct |
63 ms |
28224 KB |
Output is correct |
5 |
Correct |
70 ms |
27888 KB |
Output is correct |
6 |
Correct |
82 ms |
27716 KB |
Output is correct |
7 |
Correct |
78 ms |
27620 KB |
Output is correct |
8 |
Correct |
76 ms |
27948 KB |
Output is correct |
9 |
Correct |
41 ms |
26956 KB |
Output is correct |
10 |
Correct |
63 ms |
27412 KB |
Output is correct |
11 |
Correct |
69 ms |
27200 KB |
Output is correct |
12 |
Correct |
66 ms |
27340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
68 ms |
27688 KB |
Output is correct |
3 |
Correct |
67 ms |
27712 KB |
Output is correct |
4 |
Correct |
73 ms |
27712 KB |
Output is correct |
5 |
Correct |
80 ms |
27712 KB |
Output is correct |
6 |
Correct |
70 ms |
27608 KB |
Output is correct |
7 |
Correct |
70 ms |
27712 KB |
Output is correct |
8 |
Correct |
65 ms |
28228 KB |
Output is correct |
9 |
Correct |
60 ms |
31296 KB |
Output is correct |
10 |
Correct |
81 ms |
31420 KB |
Output is correct |
11 |
Correct |
83 ms |
31176 KB |
Output is correct |
12 |
Correct |
54 ms |
30560 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
0 ms |
456 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
23 ms |
3264 KB |
Output is correct |
32 |
Correct |
19 ms |
3160 KB |
Output is correct |
33 |
Correct |
24 ms |
3416 KB |
Output is correct |
34 |
Correct |
23 ms |
3420 KB |
Output is correct |
35 |
Correct |
17 ms |
3420 KB |
Output is correct |
36 |
Correct |
16 ms |
3412 KB |
Output is correct |
37 |
Correct |
18 ms |
2908 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
1 ms |
600 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
50 ms |
28984 KB |
Output is correct |
48 |
Correct |
47 ms |
28228 KB |
Output is correct |
49 |
Correct |
54 ms |
28740 KB |
Output is correct |
50 |
Correct |
53 ms |
28988 KB |
Output is correct |
51 |
Correct |
52 ms |
28872 KB |
Output is correct |
52 |
Correct |
49 ms |
28992 KB |
Output is correct |
53 |
Correct |
33 ms |
28356 KB |
Output is correct |
54 |
Correct |
59 ms |
27736 KB |
Output is correct |
55 |
Correct |
66 ms |
27608 KB |
Output is correct |
56 |
Correct |
66 ms |
27608 KB |
Output is correct |
57 |
Correct |
63 ms |
28224 KB |
Output is correct |
58 |
Correct |
70 ms |
27888 KB |
Output is correct |
59 |
Correct |
82 ms |
27716 KB |
Output is correct |
60 |
Correct |
78 ms |
27620 KB |
Output is correct |
61 |
Correct |
76 ms |
27948 KB |
Output is correct |
62 |
Correct |
41 ms |
26956 KB |
Output is correct |
63 |
Correct |
63 ms |
27412 KB |
Output is correct |
64 |
Correct |
69 ms |
27200 KB |
Output is correct |
65 |
Correct |
66 ms |
27340 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
81 ms |
31184 KB |
Output is correct |
68 |
Correct |
73 ms |
30756 KB |
Output is correct |
69 |
Correct |
74 ms |
30788 KB |
Output is correct |
70 |
Correct |
73 ms |
30780 KB |
Output is correct |
71 |
Correct |
71 ms |
30784 KB |
Output is correct |
72 |
Correct |
71 ms |
30804 KB |
Output is correct |
73 |
Correct |
68 ms |
31188 KB |
Output is correct |
74 |
Correct |
69 ms |
31156 KB |
Output is correct |
75 |
Correct |
79 ms |
31180 KB |
Output is correct |
76 |
Correct |
85 ms |
31156 KB |
Output is correct |
77 |
Correct |
52 ms |
30532 KB |
Output is correct |
78 |
Correct |
0 ms |
344 KB |
Output is correct |
79 |
Correct |
1 ms |
604 KB |
Output is correct |
80 |
Correct |
2 ms |
604 KB |
Output is correct |
81 |
Correct |
1 ms |
716 KB |
Output is correct |
82 |
Correct |
1 ms |
600 KB |
Output is correct |
83 |
Correct |
1 ms |
604 KB |
Output is correct |
84 |
Correct |
1 ms |
604 KB |
Output is correct |
85 |
Correct |
1 ms |
604 KB |
Output is correct |
86 |
Correct |
21 ms |
3164 KB |
Output is correct |
87 |
Correct |
19 ms |
3200 KB |
Output is correct |
88 |
Correct |
24 ms |
3420 KB |
Output is correct |
89 |
Correct |
19 ms |
3416 KB |
Output is correct |
90 |
Correct |
17 ms |
3420 KB |
Output is correct |
91 |
Correct |
16 ms |
3416 KB |
Output is correct |
92 |
Correct |
23 ms |
3100 KB |
Output is correct |
93 |
Correct |
56 ms |
28924 KB |
Output is correct |
94 |
Correct |
47 ms |
28224 KB |
Output is correct |
95 |
Correct |
56 ms |
28928 KB |
Output is correct |
96 |
Correct |
52 ms |
28996 KB |
Output is correct |
97 |
Correct |
47 ms |
28992 KB |
Output is correct |
98 |
Correct |
50 ms |
29044 KB |
Output is correct |
99 |
Correct |
34 ms |
28220 KB |
Output is correct |
100 |
Correct |
85 ms |
30788 KB |
Output is correct |
101 |
Correct |
83 ms |
30788 KB |
Output is correct |
102 |
Correct |
77 ms |
31044 KB |
Output is correct |
103 |
Correct |
68 ms |
30528 KB |
Output is correct |
104 |
Correct |
66 ms |
30400 KB |
Output is correct |
105 |
Correct |
67 ms |
30532 KB |
Output is correct |
106 |
Correct |
77 ms |
30128 KB |
Output is correct |
107 |
Correct |
83 ms |
30148 KB |
Output is correct |
108 |
Correct |
86 ms |
31048 KB |
Output is correct |
109 |
Correct |
75 ms |
30800 KB |
Output is correct |
110 |
Correct |
77 ms |
30800 KB |
Output is correct |
111 |
Correct |
76 ms |
30784 KB |
Output is correct |