#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];
if (x > y) swap(x,y);
vector <int> v = prec(x);
cout << (v[y] == 1e9 ? "impossible": to_string(v[y])) << "\n";
}
return 0;
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:64:9: warning: unused variable 'ct' [-Wunused-variable]
64 | int ct = 0;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1590 ms |
7764 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1591 ms |
7760 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1590 ms |
7764 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |