#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], L, R, trn[M];
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));
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int n, q;
cin >> n >> q;
vector<int> rs;
vector<rng> v(n);
for (auto &[l, r, i]:v) cin >> l >> r;
for (int i = 0; i < n; i++) v[i].i = i;
sort(v.begin(), v.end(), [](rng a, rng b){return a.r < b.r;});
for (int i = 0; i < n; i++) {
rs.push_back(v[i].r);
trn[v[i].i] = i;
seg[i+N] = v[i];
update(N+i);
}
for (auto [l, r, i]:v) {
nxt[trn[i]] = trn[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[trn[i]] = trn[tmp.i];
}
bitset<100005> vis;
while (q--) {
int a, b;
cin >> a >> b; a--; b--;
if (a == b) {
cout << 0 << endl;
continue;
} a = trn[a], b = trn[b];
int ans = 1;
auto tmp = v[b];
while (v[a].r < tmp.l) {
if (nxt[tmp.i] == tmp.i) break;
tmp = v[nxt[tmp.i]];
ans++;
}
if (tmp.l <= v[a].r && v[a].r <= tmp.r) {
cout << ans << endl;
} else {
cout << "impossible" << 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 |
3 ms |
4948 KB |
Output is correct |
2 |
Execution timed out |
1536 ms |
7660 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Execution timed out |
1577 ms |
4948 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Execution timed out |
1577 ms |
4948 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Execution timed out |
1577 ms |
4948 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1514 ms |
7868 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Execution timed out |
1536 ms |
7660 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |