#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
const int M = 1e5+2;
int N = exp2(ceil(log2(M)));
struct rng {int l=INT_MAX, r, i;};
int nxt[M][30], L, R;
vector<rng> v;
rng seg[4*M];
rng comb(rng a, rng b) {
if (a.l < b.l) return a;
return b;
}
void update(int ind) {
while (ind /= 2) seg[ind] = comb(seg[ind*2], seg[ind*2+1]);
}
rng query(int l = 1, int r = N, int ind = 1) {
if (l > R || r < L) return {INT_MAX, 0, 0};
if (L <= l && r <= R) return seg[ind];
return comb(query(l, (l+r)/2, ind*2), query((l+r)/2+1, r, ind*2+1));
}
int lca(int p, int x) {
int l = 20, ans = 0;
while (v[p].l > x) {
while (l >= 0 && v[nxt[p][l]].l < x) l--;
if (l < 0) {
ans++;
break;
}
ans += (1<<l);
p = nxt[p][l];
} return ans;
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int n, q;
cin >> n >> q;
vector<int> rs;
vector<rng> cp; v.resize(n);
for (auto &[l, r, i]:v) cin >> l >> r;
for (int i = 0; i < n; i++) v[i].i = i;
cp = v;
sort(cp.begin(), cp.end(), [](rng a, rng b){return a.r < b.r;});
for (int i = 0; i < n; i++) {
rs.push_back(cp[i].r);
seg[i+N] = cp[i];
update(N+i);
}
for (auto [l, r, i]:v) {
nxt[i][0] = i;
L = lower_bound(rs.begin(), rs.end(), l)-rs.begin()+1;
R = upper_bound(rs.begin(), rs.end(), r)-rs.begin();
if (!R) continue;
auto tmp = query();
if (tmp.l < l) nxt[i][0] = tmp.i;
}
for (int i = 1; i <= 20; i++) {
for (int j = 0; j < n; j++) {
nxt[j][i] = nxt[nxt[j][i-1]][i-1];
}
}
while (q--) {
int a, b;
cin >> a >> b; a--; b--;
if (a == b) {
cout << 0 << endl;
continue;
}
if (v[b].r < v[a].r || v[a].r < v[nxt[b][20]].l) {
cout << "impossible" << endl;
} else {
cout << lca(b, v[a].r)+1 << endl;
}
}
return 0;
}
/*
8 5
1 2
3 4
1 5
6 7
5 10
10 20
15 20
999999999 1000000000
1 6
1 7
2 4
3 3
5 8
5 2
1 3
2 4
4 7
7 9
3 7
1 4
3 2
y = 5 \left\{1 < x < 3\right\}
y = 4 \left\{2 < x < 4\right\}
y = 3 \left\{4 < x < 7\right\}
y = 2 \left\{7 < x < 9\right\}
y = 1 \left\{3 < x < 7\right\}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
147 ms |
20428 KB |
Output is correct |
3 |
Correct |
144 ms |
21276 KB |
Output is correct |
4 |
Correct |
210 ms |
21604 KB |
Output is correct |
5 |
Correct |
151 ms |
21708 KB |
Output is correct |
6 |
Correct |
151 ms |
22632 KB |
Output is correct |
7 |
Correct |
161 ms |
21992 KB |
Output is correct |
8 |
Correct |
126 ms |
22204 KB |
Output is correct |
9 |
Correct |
127 ms |
22988 KB |
Output is correct |
10 |
Correct |
170 ms |
22768 KB |
Output is correct |
11 |
Correct |
160 ms |
22040 KB |
Output is correct |
12 |
Correct |
100 ms |
21416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
4 ms |
5184 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
4 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5172 KB |
Output is correct |
8 |
Correct |
4 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
4 ms |
5184 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
4 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5172 KB |
Output is correct |
8 |
Correct |
4 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
4 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
4 ms |
5076 KB |
Output is correct |
16 |
Correct |
4 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
4 ms |
5172 KB |
Output is correct |
19 |
Correct |
42 ms |
6636 KB |
Output is correct |
20 |
Correct |
33 ms |
6988 KB |
Output is correct |
21 |
Correct |
34 ms |
7120 KB |
Output is correct |
22 |
Correct |
27 ms |
7400 KB |
Output is correct |
23 |
Correct |
28 ms |
6988 KB |
Output is correct |
24 |
Correct |
26 ms |
6988 KB |
Output is correct |
25 |
Correct |
30 ms |
6604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
4 ms |
5184 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
4 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5172 KB |
Output is correct |
8 |
Correct |
4 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
4 ms |
5176 KB |
Output is correct |
16 |
Correct |
3 ms |
5176 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
128 ms |
20372 KB |
Output is correct |
20 |
Correct |
140 ms |
19916 KB |
Output is correct |
21 |
Correct |
127 ms |
20052 KB |
Output is correct |
22 |
Correct |
123 ms |
19848 KB |
Output is correct |
23 |
Correct |
102 ms |
20032 KB |
Output is correct |
24 |
Correct |
135 ms |
20384 KB |
Output is correct |
25 |
Correct |
69 ms |
19820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
135 ms |
20672 KB |
Output is correct |
2 |
Correct |
144 ms |
22628 KB |
Output is correct |
3 |
Correct |
185 ms |
21508 KB |
Output is correct |
4 |
Correct |
121 ms |
21228 KB |
Output is correct |
5 |
Correct |
182 ms |
21404 KB |
Output is correct |
6 |
Correct |
172 ms |
20840 KB |
Output is correct |
7 |
Correct |
195 ms |
20852 KB |
Output is correct |
8 |
Correct |
153 ms |
22924 KB |
Output is correct |
9 |
Correct |
101 ms |
20044 KB |
Output is correct |
10 |
Correct |
197 ms |
21664 KB |
Output is correct |
11 |
Correct |
142 ms |
21348 KB |
Output is correct |
12 |
Correct |
151 ms |
21528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
147 ms |
20428 KB |
Output is correct |
3 |
Correct |
144 ms |
21276 KB |
Output is correct |
4 |
Correct |
210 ms |
21604 KB |
Output is correct |
5 |
Correct |
151 ms |
21708 KB |
Output is correct |
6 |
Correct |
151 ms |
22632 KB |
Output is correct |
7 |
Correct |
161 ms |
21992 KB |
Output is correct |
8 |
Correct |
126 ms |
22204 KB |
Output is correct |
9 |
Correct |
127 ms |
22988 KB |
Output is correct |
10 |
Correct |
170 ms |
22768 KB |
Output is correct |
11 |
Correct |
160 ms |
22040 KB |
Output is correct |
12 |
Correct |
100 ms |
21416 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
4 ms |
5184 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
4 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5172 KB |
Output is correct |
20 |
Correct |
4 ms |
5076 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
2 ms |
4948 KB |
Output is correct |
23 |
Correct |
2 ms |
4948 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
4 ms |
5076 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5204 KB |
Output is correct |
30 |
Correct |
4 ms |
5172 KB |
Output is correct |
31 |
Correct |
42 ms |
6636 KB |
Output is correct |
32 |
Correct |
33 ms |
6988 KB |
Output is correct |
33 |
Correct |
34 ms |
7120 KB |
Output is correct |
34 |
Correct |
27 ms |
7400 KB |
Output is correct |
35 |
Correct |
28 ms |
6988 KB |
Output is correct |
36 |
Correct |
26 ms |
6988 KB |
Output is correct |
37 |
Correct |
30 ms |
6604 KB |
Output is correct |
38 |
Correct |
2 ms |
4948 KB |
Output is correct |
39 |
Correct |
2 ms |
4948 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
3 ms |
5076 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
4 ms |
5176 KB |
Output is correct |
44 |
Correct |
3 ms |
5176 KB |
Output is correct |
45 |
Correct |
3 ms |
5076 KB |
Output is correct |
46 |
Correct |
3 ms |
5076 KB |
Output is correct |
47 |
Correct |
128 ms |
20372 KB |
Output is correct |
48 |
Correct |
140 ms |
19916 KB |
Output is correct |
49 |
Correct |
127 ms |
20052 KB |
Output is correct |
50 |
Correct |
123 ms |
19848 KB |
Output is correct |
51 |
Correct |
102 ms |
20032 KB |
Output is correct |
52 |
Correct |
135 ms |
20384 KB |
Output is correct |
53 |
Correct |
69 ms |
19820 KB |
Output is correct |
54 |
Correct |
135 ms |
20672 KB |
Output is correct |
55 |
Correct |
144 ms |
22628 KB |
Output is correct |
56 |
Correct |
185 ms |
21508 KB |
Output is correct |
57 |
Correct |
121 ms |
21228 KB |
Output is correct |
58 |
Correct |
182 ms |
21404 KB |
Output is correct |
59 |
Correct |
172 ms |
20840 KB |
Output is correct |
60 |
Correct |
195 ms |
20852 KB |
Output is correct |
61 |
Correct |
153 ms |
22924 KB |
Output is correct |
62 |
Correct |
101 ms |
20044 KB |
Output is correct |
63 |
Correct |
197 ms |
21664 KB |
Output is correct |
64 |
Correct |
142 ms |
21348 KB |
Output is correct |
65 |
Correct |
151 ms |
21528 KB |
Output is correct |
66 |
Correct |
2 ms |
4948 KB |
Output is correct |
67 |
Correct |
141 ms |
20672 KB |
Output is correct |
68 |
Correct |
145 ms |
21292 KB |
Output is correct |
69 |
Correct |
194 ms |
20432 KB |
Output is correct |
70 |
Correct |
151 ms |
20832 KB |
Output is correct |
71 |
Correct |
149 ms |
20548 KB |
Output is correct |
72 |
Correct |
170 ms |
21208 KB |
Output is correct |
73 |
Correct |
121 ms |
21008 KB |
Output is correct |
74 |
Correct |
124 ms |
23332 KB |
Output is correct |
75 |
Correct |
161 ms |
21248 KB |
Output is correct |
76 |
Correct |
141 ms |
20804 KB |
Output is correct |
77 |
Correct |
95 ms |
20820 KB |
Output is correct |
78 |
Correct |
3 ms |
4948 KB |
Output is correct |
79 |
Correct |
3 ms |
5076 KB |
Output is correct |
80 |
Correct |
3 ms |
5076 KB |
Output is correct |
81 |
Correct |
3 ms |
5184 KB |
Output is correct |
82 |
Correct |
4 ms |
5076 KB |
Output is correct |
83 |
Correct |
3 ms |
5076 KB |
Output is correct |
84 |
Correct |
4 ms |
5076 KB |
Output is correct |
85 |
Correct |
3 ms |
5076 KB |
Output is correct |
86 |
Correct |
35 ms |
6864 KB |
Output is correct |
87 |
Correct |
37 ms |
7176 KB |
Output is correct |
88 |
Correct |
38 ms |
6880 KB |
Output is correct |
89 |
Correct |
33 ms |
7432 KB |
Output is correct |
90 |
Correct |
29 ms |
7372 KB |
Output is correct |
91 |
Correct |
28 ms |
7292 KB |
Output is correct |
92 |
Correct |
36 ms |
6816 KB |
Output is correct |
93 |
Correct |
123 ms |
21200 KB |
Output is correct |
94 |
Correct |
108 ms |
20524 KB |
Output is correct |
95 |
Correct |
132 ms |
20720 KB |
Output is correct |
96 |
Correct |
125 ms |
19864 KB |
Output is correct |
97 |
Correct |
126 ms |
20116 KB |
Output is correct |
98 |
Correct |
135 ms |
20044 KB |
Output is correct |
99 |
Correct |
80 ms |
19884 KB |
Output is correct |
100 |
Correct |
173 ms |
20820 KB |
Output is correct |
101 |
Correct |
195 ms |
20956 KB |
Output is correct |
102 |
Correct |
177 ms |
21980 KB |
Output is correct |
103 |
Correct |
198 ms |
21900 KB |
Output is correct |
104 |
Correct |
187 ms |
21052 KB |
Output is correct |
105 |
Correct |
203 ms |
22328 KB |
Output is correct |
106 |
Correct |
181 ms |
22348 KB |
Output is correct |
107 |
Correct |
216 ms |
22556 KB |
Output is correct |
108 |
Correct |
178 ms |
22484 KB |
Output is correct |
109 |
Correct |
183 ms |
21688 KB |
Output is correct |
110 |
Correct |
231 ms |
21092 KB |
Output is correct |
111 |
Correct |
194 ms |
20588 KB |
Output is correct |