#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, 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 |
6492 KB |
Output is correct |
2 |
Execution timed out |
1586 ms |
2652 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
4 ms |
6628 KB |
Output is correct |
4 |
Correct |
4 ms |
6492 KB |
Output is correct |
5 |
Correct |
4 ms |
6492 KB |
Output is correct |
6 |
Incorrect |
4 ms |
6492 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
4 ms |
6628 KB |
Output is correct |
4 |
Correct |
4 ms |
6492 KB |
Output is correct |
5 |
Correct |
4 ms |
6492 KB |
Output is correct |
6 |
Incorrect |
4 ms |
6492 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
4 ms |
6628 KB |
Output is correct |
4 |
Correct |
4 ms |
6492 KB |
Output is correct |
5 |
Correct |
4 ms |
6492 KB |
Output is correct |
6 |
Incorrect |
4 ms |
6492 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1507 ms |
2652 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Execution timed out |
1586 ms |
2652 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |