#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr ll INFLL = numeric_limits<ll>::max() / 2;
template<class T>
istream &operator>>(istream &is, vector<T> &a) {
for (auto &i : a) {
is >> i;
}
return is;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
#ifdef __APPLE__
freopen("input.txt", "r", stdin);
#endif
int n, q;
cin >> n >> q;
vector<array<int, 3>> lr(n);
for (int i = 0; i < n; ++i) {
cin >> lr[i][0] >> lr[i][1];
lr[i][2] = i;
}
sort(lr.begin(), lr.end());
vector<int> p(n);
for (int i = 0; i < n; ++i) {
p[lr[i][2]] = i;
}
auto can = [&](int i, int j) {// i -> j
if (i == -1) {
return true;
}
auto [li, ri, ii] = lr[i];
auto [lj, rj, jj] = lr[j];
return lj <= ri && ri <= rj;
};
vector<int> prv(n);
for (int i = 0; i < n; ++i) {
// int ind = -1;
// for (int j = 0; j < i; ++j) {
// if (can(j, i)) {
// ind = j;
// break;
// }
// }
// prv[i] = ind;
int l = -1, r = i - 1;
while (l + 1 < r) {
int m = (l + r) / 2;
if (can(m, i)) {
r = m;
} else {
l = m;
}
}
if (can(r, i)) {
prv[i] = r;
}
}
while (q--) {
int a, b;
cin >> a >> b;
--a;
--b;
a = p[a];
b = p[b];
int ans = 0;
while (!can(a, b)) {
if (prv[b] == -1) {
ans = -1;
break;
}
b = prv[b];
++ans;
}
if (a != b && ans != -1) {
++ans;
}
if (ans == -1) {
cout << "impossible\n";
} else {
cout << ans << "\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1579 ms |
2308 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1572 ms |
2436 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1579 ms |
2308 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |