#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <array>
#include <algorithm>
#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
88 ms |
11104 KB |
Output is correct |
3 |
Correct |
103 ms |
11100 KB |
Output is correct |
4 |
Correct |
135 ms |
11068 KB |
Output is correct |
5 |
Correct |
110 ms |
11232 KB |
Output is correct |
6 |
Correct |
103 ms |
11232 KB |
Output is correct |
7 |
Correct |
108 ms |
11252 KB |
Output is correct |
8 |
Correct |
120 ms |
13696 KB |
Output is correct |
9 |
Correct |
120 ms |
16788 KB |
Output is correct |
10 |
Correct |
124 ms |
14552 KB |
Output is correct |
11 |
Correct |
125 ms |
16676 KB |
Output is correct |
12 |
Correct |
58 ms |
13900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 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 |
496 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 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 |
496 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 |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
496 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
31 ms |
2384 KB |
Output is correct |
20 |
Correct |
29 ms |
2456 KB |
Output is correct |
21 |
Correct |
33 ms |
2604 KB |
Output is correct |
22 |
Correct |
25 ms |
2772 KB |
Output is correct |
23 |
Correct |
24 ms |
2760 KB |
Output is correct |
24 |
Correct |
27 ms |
2716 KB |
Output is correct |
25 |
Correct |
28 ms |
2316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 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 |
496 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 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 |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
500 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
66 ms |
12512 KB |
Output is correct |
20 |
Correct |
64 ms |
11616 KB |
Output is correct |
21 |
Correct |
79 ms |
12432 KB |
Output is correct |
22 |
Correct |
74 ms |
14544 KB |
Output is correct |
23 |
Correct |
86 ms |
14536 KB |
Output is correct |
24 |
Correct |
81 ms |
14560 KB |
Output is correct |
25 |
Correct |
28 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
11084 KB |
Output is correct |
2 |
Correct |
107 ms |
11164 KB |
Output is correct |
3 |
Correct |
120 ms |
11100 KB |
Output is correct |
4 |
Correct |
110 ms |
13648 KB |
Output is correct |
5 |
Correct |
110 ms |
11468 KB |
Output is correct |
6 |
Correct |
116 ms |
13388 KB |
Output is correct |
7 |
Correct |
122 ms |
13300 KB |
Output is correct |
8 |
Correct |
107 ms |
13412 KB |
Output is correct |
9 |
Correct |
85 ms |
12504 KB |
Output is correct |
10 |
Correct |
109 ms |
13000 KB |
Output is correct |
11 |
Correct |
110 ms |
12756 KB |
Output is correct |
12 |
Correct |
107 ms |
12980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
88 ms |
11104 KB |
Output is correct |
3 |
Correct |
103 ms |
11100 KB |
Output is correct |
4 |
Correct |
135 ms |
11068 KB |
Output is correct |
5 |
Correct |
110 ms |
11232 KB |
Output is correct |
6 |
Correct |
103 ms |
11232 KB |
Output is correct |
7 |
Correct |
108 ms |
11252 KB |
Output is correct |
8 |
Correct |
120 ms |
13696 KB |
Output is correct |
9 |
Correct |
120 ms |
16788 KB |
Output is correct |
10 |
Correct |
124 ms |
14552 KB |
Output is correct |
11 |
Correct |
125 ms |
16676 KB |
Output is correct |
12 |
Correct |
58 ms |
13900 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 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 |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
496 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 |
592 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
496 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
31 ms |
2384 KB |
Output is correct |
32 |
Correct |
29 ms |
2456 KB |
Output is correct |
33 |
Correct |
33 ms |
2604 KB |
Output is correct |
34 |
Correct |
25 ms |
2772 KB |
Output is correct |
35 |
Correct |
24 ms |
2760 KB |
Output is correct |
36 |
Correct |
27 ms |
2716 KB |
Output is correct |
37 |
Correct |
28 ms |
2316 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
468 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 |
460 KB |
Output is correct |
44 |
Correct |
1 ms |
500 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
66 ms |
12512 KB |
Output is correct |
48 |
Correct |
64 ms |
11616 KB |
Output is correct |
49 |
Correct |
79 ms |
12432 KB |
Output is correct |
50 |
Correct |
74 ms |
14544 KB |
Output is correct |
51 |
Correct |
86 ms |
14536 KB |
Output is correct |
52 |
Correct |
81 ms |
14560 KB |
Output is correct |
53 |
Correct |
28 ms |
9728 KB |
Output is correct |
54 |
Correct |
85 ms |
11084 KB |
Output is correct |
55 |
Correct |
107 ms |
11164 KB |
Output is correct |
56 |
Correct |
120 ms |
11100 KB |
Output is correct |
57 |
Correct |
110 ms |
13648 KB |
Output is correct |
58 |
Correct |
110 ms |
11468 KB |
Output is correct |
59 |
Correct |
116 ms |
13388 KB |
Output is correct |
60 |
Correct |
122 ms |
13300 KB |
Output is correct |
61 |
Correct |
107 ms |
13412 KB |
Output is correct |
62 |
Correct |
85 ms |
12504 KB |
Output is correct |
63 |
Correct |
109 ms |
13000 KB |
Output is correct |
64 |
Correct |
110 ms |
12756 KB |
Output is correct |
65 |
Correct |
107 ms |
12980 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
90 ms |
14272 KB |
Output is correct |
68 |
Correct |
102 ms |
14204 KB |
Output is correct |
69 |
Correct |
140 ms |
14352 KB |
Output is correct |
70 |
Correct |
113 ms |
14288 KB |
Output is correct |
71 |
Correct |
109 ms |
14344 KB |
Output is correct |
72 |
Correct |
113 ms |
14352 KB |
Output is correct |
73 |
Correct |
116 ms |
16812 KB |
Output is correct |
74 |
Correct |
114 ms |
16840 KB |
Output is correct |
75 |
Correct |
108 ms |
14660 KB |
Output is correct |
76 |
Correct |
108 ms |
16732 KB |
Output is correct |
77 |
Correct |
60 ms |
14008 KB |
Output is correct |
78 |
Correct |
1 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
468 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 |
468 KB |
Output is correct |
85 |
Correct |
1 ms |
468 KB |
Output is correct |
86 |
Correct |
31 ms |
2372 KB |
Output is correct |
87 |
Correct |
30 ms |
2388 KB |
Output is correct |
88 |
Correct |
29 ms |
2600 KB |
Output is correct |
89 |
Correct |
25 ms |
2756 KB |
Output is correct |
90 |
Correct |
32 ms |
2720 KB |
Output is correct |
91 |
Correct |
27 ms |
2764 KB |
Output is correct |
92 |
Correct |
25 ms |
2388 KB |
Output is correct |
93 |
Correct |
66 ms |
12420 KB |
Output is correct |
94 |
Correct |
64 ms |
11672 KB |
Output is correct |
95 |
Correct |
83 ms |
12364 KB |
Output is correct |
96 |
Correct |
78 ms |
14496 KB |
Output is correct |
97 |
Correct |
85 ms |
14480 KB |
Output is correct |
98 |
Correct |
76 ms |
14532 KB |
Output is correct |
99 |
Correct |
24 ms |
9704 KB |
Output is correct |
100 |
Correct |
125 ms |
16400 KB |
Output is correct |
101 |
Correct |
131 ms |
16376 KB |
Output is correct |
102 |
Correct |
111 ms |
16584 KB |
Output is correct |
103 |
Correct |
118 ms |
16012 KB |
Output is correct |
104 |
Correct |
116 ms |
15872 KB |
Output is correct |
105 |
Correct |
135 ms |
16172 KB |
Output is correct |
106 |
Correct |
103 ms |
13712 KB |
Output is correct |
107 |
Correct |
124 ms |
13672 KB |
Output is correct |
108 |
Correct |
115 ms |
14284 KB |
Output is correct |
109 |
Correct |
104 ms |
14316 KB |
Output is correct |
110 |
Correct |
116 ms |
16260 KB |
Output is correct |
111 |
Correct |
121 ms |
16336 KB |
Output is correct |