#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 |
324 KB |
Output is correct |
2 |
Correct |
84 ms |
14288 KB |
Output is correct |
3 |
Correct |
92 ms |
14164 KB |
Output is correct |
4 |
Correct |
97 ms |
14248 KB |
Output is correct |
5 |
Correct |
97 ms |
14284 KB |
Output is correct |
6 |
Correct |
91 ms |
14296 KB |
Output is correct |
7 |
Correct |
104 ms |
14288 KB |
Output is correct |
8 |
Correct |
108 ms |
16728 KB |
Output is correct |
9 |
Correct |
103 ms |
16708 KB |
Output is correct |
10 |
Correct |
88 ms |
14584 KB |
Output is correct |
11 |
Correct |
93 ms |
16668 KB |
Output is correct |
12 |
Correct |
54 ms |
13912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
532 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
532 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
25 ms |
2352 KB |
Output is correct |
20 |
Correct |
41 ms |
2380 KB |
Output is correct |
21 |
Correct |
41 ms |
2636 KB |
Output is correct |
22 |
Correct |
22 ms |
2720 KB |
Output is correct |
23 |
Correct |
20 ms |
2704 KB |
Output is correct |
24 |
Correct |
20 ms |
2764 KB |
Output is correct |
25 |
Correct |
29 ms |
2404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
532 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
58 ms |
12540 KB |
Output is correct |
20 |
Correct |
55 ms |
11608 KB |
Output is correct |
21 |
Correct |
68 ms |
12316 KB |
Output is correct |
22 |
Correct |
62 ms |
14460 KB |
Output is correct |
23 |
Correct |
75 ms |
14444 KB |
Output is correct |
24 |
Correct |
72 ms |
14564 KB |
Output is correct |
25 |
Correct |
21 ms |
9700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
14280 KB |
Output is correct |
2 |
Correct |
99 ms |
14284 KB |
Output is correct |
3 |
Correct |
111 ms |
14228 KB |
Output is correct |
4 |
Correct |
105 ms |
16720 KB |
Output is correct |
5 |
Correct |
95 ms |
14664 KB |
Output is correct |
6 |
Correct |
112 ms |
16420 KB |
Output is correct |
7 |
Correct |
116 ms |
16416 KB |
Output is correct |
8 |
Correct |
126 ms |
16584 KB |
Output is correct |
9 |
Correct |
77 ms |
14552 KB |
Output is correct |
10 |
Correct |
122 ms |
16072 KB |
Output is correct |
11 |
Correct |
99 ms |
15792 KB |
Output is correct |
12 |
Correct |
126 ms |
16072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
84 ms |
14288 KB |
Output is correct |
3 |
Correct |
92 ms |
14164 KB |
Output is correct |
4 |
Correct |
97 ms |
14248 KB |
Output is correct |
5 |
Correct |
97 ms |
14284 KB |
Output is correct |
6 |
Correct |
91 ms |
14296 KB |
Output is correct |
7 |
Correct |
104 ms |
14288 KB |
Output is correct |
8 |
Correct |
108 ms |
16728 KB |
Output is correct |
9 |
Correct |
103 ms |
16708 KB |
Output is correct |
10 |
Correct |
88 ms |
14584 KB |
Output is correct |
11 |
Correct |
93 ms |
16668 KB |
Output is correct |
12 |
Correct |
54 ms |
13912 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
532 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
25 ms |
2352 KB |
Output is correct |
32 |
Correct |
41 ms |
2380 KB |
Output is correct |
33 |
Correct |
41 ms |
2636 KB |
Output is correct |
34 |
Correct |
22 ms |
2720 KB |
Output is correct |
35 |
Correct |
20 ms |
2704 KB |
Output is correct |
36 |
Correct |
20 ms |
2764 KB |
Output is correct |
37 |
Correct |
29 ms |
2404 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
460 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
58 ms |
12540 KB |
Output is correct |
48 |
Correct |
55 ms |
11608 KB |
Output is correct |
49 |
Correct |
68 ms |
12316 KB |
Output is correct |
50 |
Correct |
62 ms |
14460 KB |
Output is correct |
51 |
Correct |
75 ms |
14444 KB |
Output is correct |
52 |
Correct |
72 ms |
14564 KB |
Output is correct |
53 |
Correct |
21 ms |
9700 KB |
Output is correct |
54 |
Correct |
86 ms |
14280 KB |
Output is correct |
55 |
Correct |
99 ms |
14284 KB |
Output is correct |
56 |
Correct |
111 ms |
14228 KB |
Output is correct |
57 |
Correct |
105 ms |
16720 KB |
Output is correct |
58 |
Correct |
95 ms |
14664 KB |
Output is correct |
59 |
Correct |
112 ms |
16420 KB |
Output is correct |
60 |
Correct |
116 ms |
16416 KB |
Output is correct |
61 |
Correct |
126 ms |
16584 KB |
Output is correct |
62 |
Correct |
77 ms |
14552 KB |
Output is correct |
63 |
Correct |
122 ms |
16072 KB |
Output is correct |
64 |
Correct |
99 ms |
15792 KB |
Output is correct |
65 |
Correct |
126 ms |
16072 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
90 ms |
14280 KB |
Output is correct |
68 |
Correct |
102 ms |
14204 KB |
Output is correct |
69 |
Correct |
108 ms |
14160 KB |
Output is correct |
70 |
Correct |
108 ms |
14284 KB |
Output is correct |
71 |
Correct |
107 ms |
14324 KB |
Output is correct |
72 |
Correct |
107 ms |
14284 KB |
Output is correct |
73 |
Correct |
113 ms |
16744 KB |
Output is correct |
74 |
Correct |
106 ms |
16808 KB |
Output is correct |
75 |
Correct |
94 ms |
14544 KB |
Output is correct |
76 |
Correct |
113 ms |
16716 KB |
Output is correct |
77 |
Correct |
57 ms |
13916 KB |
Output is correct |
78 |
Correct |
0 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
464 KB |
Output is correct |
80 |
Correct |
1 ms |
468 KB |
Output is correct |
81 |
Correct |
1 ms |
468 KB |
Output is correct |
82 |
Correct |
1 ms |
468 KB |
Output is correct |
83 |
Correct |
1 ms |
468 KB |
Output is correct |
84 |
Correct |
1 ms |
464 KB |
Output is correct |
85 |
Correct |
1 ms |
468 KB |
Output is correct |
86 |
Correct |
27 ms |
2436 KB |
Output is correct |
87 |
Correct |
25 ms |
2388 KB |
Output is correct |
88 |
Correct |
31 ms |
2676 KB |
Output is correct |
89 |
Correct |
26 ms |
2696 KB |
Output is correct |
90 |
Correct |
21 ms |
2724 KB |
Output is correct |
91 |
Correct |
21 ms |
2728 KB |
Output is correct |
92 |
Correct |
22 ms |
2396 KB |
Output is correct |
93 |
Correct |
65 ms |
12500 KB |
Output is correct |
94 |
Correct |
57 ms |
11620 KB |
Output is correct |
95 |
Correct |
71 ms |
12340 KB |
Output is correct |
96 |
Correct |
71 ms |
14560 KB |
Output is correct |
97 |
Correct |
81 ms |
14448 KB |
Output is correct |
98 |
Correct |
73 ms |
14448 KB |
Output is correct |
99 |
Correct |
19 ms |
9700 KB |
Output is correct |
100 |
Correct |
100 ms |
16472 KB |
Output is correct |
101 |
Correct |
102 ms |
16396 KB |
Output is correct |
102 |
Correct |
102 ms |
16476 KB |
Output is correct |
103 |
Correct |
98 ms |
16048 KB |
Output is correct |
104 |
Correct |
101 ms |
15880 KB |
Output is correct |
105 |
Correct |
99 ms |
16048 KB |
Output is correct |
106 |
Correct |
86 ms |
13628 KB |
Output is correct |
107 |
Correct |
96 ms |
13644 KB |
Output is correct |
108 |
Correct |
92 ms |
14392 KB |
Output is correct |
109 |
Correct |
104 ms |
14280 KB |
Output is correct |
110 |
Correct |
103 ms |
16336 KB |
Output is correct |
111 |
Correct |
104 ms |
16356 KB |
Output is correct |