#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 25;
const int B = 18;
int dp[B][MAXN];
int n, q;
pair <int, int> a[MAXN];
int jump (int a, int b) {
for (int i = B - 1; i >= 0; i--) {
if (b & (1 << i)) {
a = dp[i][a];
}
}
return a;
}
int main () {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i].first >> a[i].second;
}
for (int i = 1; i <= n; i++) {
dp[0][i] = i;
for (int j = 1; j <= n; j++) {
if (a[j].second >= a[i].first && a[j].second <= a[i].second) {
if (a[j].first < a[dp[0][i]].first) dp[0][i] = j;
}
}
}
for (int i = 1; i < B; i++) {
for (int j = 1; j <= n; j++) {
dp[i][j] = dp[i - 1][dp[i - 1][j]];
}
}
while (q--) {
int x, y; cin >> x >> y;
if (x == y) {
cout << "0\n";
continue;
}
int ans = 0;
if (a[y].first <= a[x].second) {
ans = -1;
} else {
for (int i = B - 1; i >= 0; i--) {
auto g = dp[i][y];
if (!g) continue;
if (a[g].first > a[x].second) {
ans ^= 1 << i;
y = g;
}
}
}
y = jump(y, ans + 1);
if (a[x].second <= a[y].second && a[x].second >= a[y].first) {
cout << ans + 2 << '\n';
} else {
cout << "impossible\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6744 KB |
Output is correct |
2 |
Execution timed out |
1587 ms |
2652 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Incorrect |
5 ms |
6492 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Incorrect |
5 ms |
6492 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Incorrect |
5 ms |
6492 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1554 ms |
2652 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6744 KB |
Output is correct |
2 |
Execution timed out |
1587 ms |
2652 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |