#include <algorithm>
#include <array>
#include <iostream>
using namespace std;
const int N = 1e5, N_ = 1 << 18, L = 17;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n, q;
cin >> n >> q;
static int l[N], r[N], c[N * 2];
for (int i = 0; i < n; i++) {
cin >> l[i] >> r[i];
c[i << 1 | 0] = l[i], c[i << 1 | 1] = r[i];
}
sort(c, c + n * 2);
int m = unique(c, c + n * 2) - c;
static array<int, 2> t[N_ * 2];
int n_;
n_ = 1;
while (n_ < m) n_ <<= 1;
for (int i = 1; i < n_ * 2; i++) t[i] = {N_, -1};
for (int i = 0; i < n; i++) {
l[i] = lower_bound(c, c + m, l[i]) - c;
r[i] = lower_bound(c, c + m, r[i]) - c;
int p = n_ + r[i];
while (p > 0) t[p] = min(t[p], {l[i], i}), p >>= 1;
}
static int p[L][N];
for (int i = 0; i < n; i++) {
array<int, 2> x = {N_, -1};
for (int l_ = l[i] + n_, r_ = r[i] + n_; l_ <= r_; l_ >>= 1, r_ >>= 1) {
if ((l_ & 1) == 1) x = min(x, t[l_++]);
if ((r_ & 1) == 0) x = min(x, t[r_--]);
}
p[0][i] = x[1];
}
for (int lg = 1; lg < L; lg++)
for (int i = 0; i < n; i++) p[lg][i] = p[lg - 1][p[lg - 1][i]];
while (q--) {
int i, j;
cin >> i >> j, i--, j--;
if (r[j] < r[i]) {
cout << "impossible\n";
continue;
}
if (i == j) {
cout << 0 << '\n';
continue;
}
if (l[j] <= r[i] && r[i] <= r[j]) {
cout << 1 << '\n';
continue;
}
int k = 0;
for (int lg = L - 1; lg >= 0; lg--)
if (l[p[lg][j]] > r[i]) k += 1 << lg, j = p[lg][j];
if (k > n) {
cout << "impossible\n";
continue;
}
cout << k + 2 << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8904 KB |
Output is correct |
2 |
Correct |
113 ms |
15948 KB |
Output is correct |
3 |
Correct |
91 ms |
15944 KB |
Output is correct |
4 |
Correct |
108 ms |
15952 KB |
Output is correct |
5 |
Correct |
103 ms |
16196 KB |
Output is correct |
6 |
Correct |
91 ms |
16140 KB |
Output is correct |
7 |
Correct |
90 ms |
16208 KB |
Output is correct |
8 |
Correct |
104 ms |
16800 KB |
Output is correct |
9 |
Correct |
107 ms |
17048 KB |
Output is correct |
10 |
Correct |
85 ms |
16464 KB |
Output is correct |
11 |
Correct |
97 ms |
16776 KB |
Output is correct |
12 |
Correct |
49 ms |
15860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8708 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8536 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8708 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8536 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
1 ms |
8540 KB |
Output is correct |
12 |
Correct |
2 ms |
8704 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8660 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
8672 KB |
Output is correct |
17 |
Correct |
2 ms |
8540 KB |
Output is correct |
18 |
Correct |
2 ms |
8540 KB |
Output is correct |
19 |
Correct |
29 ms |
10064 KB |
Output is correct |
20 |
Correct |
26 ms |
10064 KB |
Output is correct |
21 |
Correct |
26 ms |
10324 KB |
Output is correct |
22 |
Correct |
21 ms |
10432 KB |
Output is correct |
23 |
Correct |
20 ms |
10376 KB |
Output is correct |
24 |
Correct |
32 ms |
10328 KB |
Output is correct |
25 |
Correct |
24 ms |
10072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8708 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8536 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
8540 KB |
Output is correct |
12 |
Correct |
2 ms |
8716 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8704 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
8540 KB |
Output is correct |
17 |
Correct |
2 ms |
8540 KB |
Output is correct |
18 |
Correct |
2 ms |
8540 KB |
Output is correct |
19 |
Correct |
73 ms |
12668 KB |
Output is correct |
20 |
Correct |
70 ms |
13400 KB |
Output is correct |
21 |
Correct |
68 ms |
14108 KB |
Output is correct |
22 |
Correct |
68 ms |
14472 KB |
Output is correct |
23 |
Correct |
82 ms |
14508 KB |
Output is correct |
24 |
Correct |
76 ms |
14504 KB |
Output is correct |
25 |
Correct |
28 ms |
11452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
80 ms |
15956 KB |
Output is correct |
2 |
Correct |
90 ms |
16084 KB |
Output is correct |
3 |
Correct |
123 ms |
16132 KB |
Output is correct |
4 |
Correct |
113 ms |
16796 KB |
Output is correct |
5 |
Correct |
88 ms |
16528 KB |
Output is correct |
6 |
Correct |
110 ms |
16524 KB |
Output is correct |
7 |
Correct |
115 ms |
16536 KB |
Output is correct |
8 |
Correct |
92 ms |
16536 KB |
Output is correct |
9 |
Correct |
78 ms |
14516 KB |
Output is correct |
10 |
Correct |
103 ms |
16112 KB |
Output is correct |
11 |
Correct |
112 ms |
16028 KB |
Output is correct |
12 |
Correct |
95 ms |
16204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8904 KB |
Output is correct |
2 |
Correct |
113 ms |
15948 KB |
Output is correct |
3 |
Correct |
91 ms |
15944 KB |
Output is correct |
4 |
Correct |
108 ms |
15952 KB |
Output is correct |
5 |
Correct |
103 ms |
16196 KB |
Output is correct |
6 |
Correct |
91 ms |
16140 KB |
Output is correct |
7 |
Correct |
90 ms |
16208 KB |
Output is correct |
8 |
Correct |
104 ms |
16800 KB |
Output is correct |
9 |
Correct |
107 ms |
17048 KB |
Output is correct |
10 |
Correct |
85 ms |
16464 KB |
Output is correct |
11 |
Correct |
97 ms |
16776 KB |
Output is correct |
12 |
Correct |
49 ms |
15860 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
1 ms |
8540 KB |
Output is correct |
15 |
Correct |
2 ms |
8708 KB |
Output is correct |
16 |
Correct |
2 ms |
8540 KB |
Output is correct |
17 |
Correct |
2 ms |
8540 KB |
Output is correct |
18 |
Correct |
2 ms |
8536 KB |
Output is correct |
19 |
Correct |
2 ms |
8540 KB |
Output is correct |
20 |
Correct |
2 ms |
8540 KB |
Output is correct |
21 |
Correct |
1 ms |
8540 KB |
Output is correct |
22 |
Correct |
1 ms |
8540 KB |
Output is correct |
23 |
Correct |
1 ms |
8540 KB |
Output is correct |
24 |
Correct |
2 ms |
8704 KB |
Output is correct |
25 |
Correct |
2 ms |
8540 KB |
Output is correct |
26 |
Correct |
2 ms |
8660 KB |
Output is correct |
27 |
Correct |
2 ms |
8540 KB |
Output is correct |
28 |
Correct |
2 ms |
8672 KB |
Output is correct |
29 |
Correct |
2 ms |
8540 KB |
Output is correct |
30 |
Correct |
2 ms |
8540 KB |
Output is correct |
31 |
Correct |
29 ms |
10064 KB |
Output is correct |
32 |
Correct |
26 ms |
10064 KB |
Output is correct |
33 |
Correct |
26 ms |
10324 KB |
Output is correct |
34 |
Correct |
21 ms |
10432 KB |
Output is correct |
35 |
Correct |
20 ms |
10376 KB |
Output is correct |
36 |
Correct |
32 ms |
10328 KB |
Output is correct |
37 |
Correct |
24 ms |
10072 KB |
Output is correct |
38 |
Correct |
1 ms |
8540 KB |
Output is correct |
39 |
Correct |
2 ms |
8540 KB |
Output is correct |
40 |
Correct |
2 ms |
8716 KB |
Output is correct |
41 |
Correct |
2 ms |
8540 KB |
Output is correct |
42 |
Correct |
2 ms |
8704 KB |
Output is correct |
43 |
Correct |
2 ms |
8540 KB |
Output is correct |
44 |
Correct |
2 ms |
8540 KB |
Output is correct |
45 |
Correct |
2 ms |
8540 KB |
Output is correct |
46 |
Correct |
2 ms |
8540 KB |
Output is correct |
47 |
Correct |
73 ms |
12668 KB |
Output is correct |
48 |
Correct |
70 ms |
13400 KB |
Output is correct |
49 |
Correct |
68 ms |
14108 KB |
Output is correct |
50 |
Correct |
68 ms |
14472 KB |
Output is correct |
51 |
Correct |
82 ms |
14508 KB |
Output is correct |
52 |
Correct |
76 ms |
14504 KB |
Output is correct |
53 |
Correct |
28 ms |
11452 KB |
Output is correct |
54 |
Correct |
80 ms |
15956 KB |
Output is correct |
55 |
Correct |
90 ms |
16084 KB |
Output is correct |
56 |
Correct |
123 ms |
16132 KB |
Output is correct |
57 |
Correct |
113 ms |
16796 KB |
Output is correct |
58 |
Correct |
88 ms |
16528 KB |
Output is correct |
59 |
Correct |
110 ms |
16524 KB |
Output is correct |
60 |
Correct |
115 ms |
16536 KB |
Output is correct |
61 |
Correct |
92 ms |
16536 KB |
Output is correct |
62 |
Correct |
78 ms |
14516 KB |
Output is correct |
63 |
Correct |
103 ms |
16112 KB |
Output is correct |
64 |
Correct |
112 ms |
16028 KB |
Output is correct |
65 |
Correct |
95 ms |
16204 KB |
Output is correct |
66 |
Correct |
1 ms |
8552 KB |
Output is correct |
67 |
Correct |
82 ms |
15960 KB |
Output is correct |
68 |
Correct |
94 ms |
16036 KB |
Output is correct |
69 |
Correct |
106 ms |
15940 KB |
Output is correct |
70 |
Correct |
94 ms |
16212 KB |
Output is correct |
71 |
Correct |
104 ms |
16180 KB |
Output is correct |
72 |
Correct |
112 ms |
16200 KB |
Output is correct |
73 |
Correct |
112 ms |
16780 KB |
Output is correct |
74 |
Correct |
110 ms |
16780 KB |
Output is correct |
75 |
Correct |
128 ms |
16720 KB |
Output is correct |
76 |
Correct |
89 ms |
16776 KB |
Output is correct |
77 |
Correct |
48 ms |
15860 KB |
Output is correct |
78 |
Correct |
1 ms |
8540 KB |
Output is correct |
79 |
Correct |
2 ms |
8840 KB |
Output is correct |
80 |
Correct |
2 ms |
8540 KB |
Output is correct |
81 |
Correct |
1 ms |
8540 KB |
Output is correct |
82 |
Correct |
2 ms |
8536 KB |
Output is correct |
83 |
Correct |
2 ms |
8696 KB |
Output is correct |
84 |
Correct |
2 ms |
8716 KB |
Output is correct |
85 |
Correct |
2 ms |
8540 KB |
Output is correct |
86 |
Correct |
31 ms |
10016 KB |
Output is correct |
87 |
Correct |
33 ms |
10284 KB |
Output is correct |
88 |
Correct |
31 ms |
10300 KB |
Output is correct |
89 |
Correct |
24 ms |
10580 KB |
Output is correct |
90 |
Correct |
21 ms |
10320 KB |
Output is correct |
91 |
Correct |
29 ms |
10320 KB |
Output is correct |
92 |
Correct |
35 ms |
10068 KB |
Output is correct |
93 |
Correct |
63 ms |
14220 KB |
Output is correct |
94 |
Correct |
53 ms |
13404 KB |
Output is correct |
95 |
Correct |
63 ms |
14112 KB |
Output is correct |
96 |
Correct |
61 ms |
14676 KB |
Output is correct |
97 |
Correct |
72 ms |
14696 KB |
Output is correct |
98 |
Correct |
70 ms |
14704 KB |
Output is correct |
99 |
Correct |
28 ms |
11420 KB |
Output is correct |
100 |
Correct |
107 ms |
16404 KB |
Output is correct |
101 |
Correct |
100 ms |
16540 KB |
Output is correct |
102 |
Correct |
103 ms |
16524 KB |
Output is correct |
103 |
Correct |
100 ms |
16012 KB |
Output is correct |
104 |
Correct |
112 ms |
16008 KB |
Output is correct |
105 |
Correct |
102 ms |
16012 KB |
Output is correct |
106 |
Correct |
84 ms |
15440 KB |
Output is correct |
107 |
Correct |
89 ms |
15448 KB |
Output is correct |
108 |
Correct |
97 ms |
16076 KB |
Output is correct |
109 |
Correct |
100 ms |
16052 KB |
Output is correct |
110 |
Correct |
109 ms |
16484 KB |
Output is correct |
111 |
Correct |
111 ms |
16328 KB |
Output is correct |