#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
const int maxn = 2e5 + 2;
struct nd {
int x;
int y;
int idx;
int l;
int r;
bool operator < (const nd& oth) const {
return make_pair(x, y) < make_pair(oth.x, oth.y);
}
} a [maxn];
vector <int> comp;
int n,q;
int ps[maxn];
vector <int> prec (int x) {
vector <int> dist(n, 1e9);
set <int> els;
queue <int> q;
q.push(x);
dist[x] = 0;
for (int i = 0; i < n; i++) if (i != x) els.insert(i);
while (!q.empty()) {
int u = q.front();
q.pop();
while (1) {
auto it = els.lower_bound(a[u].l);
if (it == els.end() || *it > a[u].r) break;
dist[*it] = dist[u] + 1;
q.push(*it);
els.erase(it);
}
}
return dist;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> q;
for (int i = 0; i < n; i++){
cin >> a[i].x >> a[i].y;
a[i].idx = i;
}
sort(a, a + n);
for (int i = 0; i < n; i++){
ps[a[i].idx] = i;
int l = i + 1, r = n - 1, rsr = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (a[mid].x <= a[i].y) {
rsr = mid;
l = mid + 1;
}
else {
r = mid - 1;
}
}
a[i].l = i + 1, a[i].r = rsr;
}
int ct = 0;
while (q--){
int x,y;
cin >> x >> y;
x = ps[x - 1], y = ps[y - 1];
vector <int> v = prec(x);
cout << (v[y] == 1e9 ? "impossible": to_string(v[y])) << "\n";
}
return 0;
}
Compilation message
vault.cpp: In function 'int main()':
vault.cpp:64:9: warning: unused variable 'ct' [-Wunused-variable]
64 | int ct = 0;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |