#include "bits/extc++.h"
using namespace std;
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t;
((cerr << " | " << u), ...);
cerr << endl;
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
dbgh(__VA_ARGS__)
#else
#define dbg(...)
#define cerr \
if (false) \
cerr
#endif
#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))
void solve() {
int n, q;
cin >> n >> q;
vector<pair<int, int>> arr(n);
for (auto& [l, r] : arr) {
cin >> l >> r;
}
int logn = __lg(n);
vector<vector<int>> lift(logn + 1, vector<int>(n));
for (int i = 0; i < n; i++) {
pair<int, int> c_opt {int(1e9), -1};
for (int j = 0; j < n; j++) {
if (arr[i].first <= arr[j].second &&
arr[j].second <= arr[i].second) {
c_opt = min(c_opt, {arr[j].first, j});
}
}
if (c_opt.first >= arr[i].first) {
lift[0][i] = -1;
} else {
lift[0][i] = c_opt.second;
}
}
for (int i = 1; i <= logn; i++) {
for (int j = 0; j < n; j++) {
if (lift[i - 1][j] == -1) {
lift[i][j] = -1;
} else {
lift[i][j] = lift[i - 1][lift[i - 1][j]];
}
}
}
while (q--) {
int u, v;
cin >> u >> v;
u--;
v--;
if (u == v) {
cout << 0 << endl;
continue;
}
int ans = 0;
for (int i = logn; i >= 0; i--) {
int nv = lift[i][v];
if (nv != -1 && arr[u].second < arr[nv].first) {
v = nv;
ans += 1 << i;
}
}
if (arr[u].second < arr[v].first) {
v = lift[0][v];
ans++;
}
// int ql = arr[v].first, qr = arr[v].second, ans = 0;
// while (ql > arr[u].second) {
// int n_opt = 1e9;
// for (auto& [l, r] : arr) {
// if (ql <= r && r <= qr) {
// n_opt = min(n_opt, l);
// }
// }
// if (n_opt >= ql) {
// break;
// }
// ans++;
// ql = n_opt;
// }
if (v != -1 && arr[v].first <= arr[u].second &&
arr[u].second <= arr[v].second) {
ans++;
cout << ans << endl;
} else {
cout << "impossible" << endl;
}
}
}
int main() {
cin.tie(nullptr);
cin.exceptions(ios::failbit);
ios_base::sync_with_stdio(false);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1578 ms |
8152 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
5 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
5 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
3 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
340 KB |
Output is correct |
15 |
Correct |
4 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
109 ms |
1864 KB |
Output is correct |
20 |
Correct |
95 ms |
2132 KB |
Output is correct |
21 |
Correct |
96 ms |
2272 KB |
Output is correct |
22 |
Correct |
106 ms |
2408 KB |
Output is correct |
23 |
Correct |
121 ms |
2256 KB |
Output is correct |
24 |
Correct |
105 ms |
2256 KB |
Output is correct |
25 |
Correct |
94 ms |
1872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
5 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
3 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
340 KB |
Output is correct |
15 |
Correct |
4 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
380 KB |
Output is correct |
18 |
Correct |
2 ms |
372 KB |
Output is correct |
19 |
Execution timed out |
1581 ms |
8156 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1574 ms |
8148 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 |
1578 ms |
8152 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |