#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))
struct ST {
int n, logn;
vector<vector<int>> v;
ST(const vector<int>& arr) : n(sz(arr)), v(__lg(n) + 1, vector<int>(n)) {
v[0] = arr;
for (int i = 1; i <= logn; i++) {
for (int j = 0; j + (1 << i) <= n; j++) {
v[i][j] = min(v[i - 1][j], v[i - 1][j + (1 << (i - 1))]);
}
}
}
int query(int l, int r) const {
int k = __lg(r - l);
return min(v[k][l], v[k][r - (1 << k)]);
}
};
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> opt {-1, -1};
for (int j = 0; j < n; j++) {
if (arr[j].first <= arr[i].second &&
arr[i].second <= arr[j].second) {
opt = max(opt, {arr[j].second, j});
}
}
if (opt.first == arr[i].second) {
lift[0][i] = -1;
} else {
lift[0][i] = 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]];
}
}
}
int qans[q];
map<int, vector<array<int, 4>>> evts;
for (int qi = 0; qi < q; qi++) {
int u, v;
cin >> u >> v;
u--;
v--;
if (u == v) {
qans[qi] = 0;
continue;
}
int ans = 0;
for (int i = logn; i >= 0; i--) {
int nu = lift[i][u];
if (nu != -1 && arr[nu].second < arr[v].first) {
u = nu;
ans += 1 << i;
}
}
// int pos = arr[u].second, ans = 0;
// while (pos < arr[v].first) {
// int n_pos = -1;
// for (auto& [l, r] : arr) {
// if (l <= pos && pos <= r && r <= arr[v].second) {
// n_pos = max(n_pos, r);
// }
// }
// if (n_pos == -1 || pos == n_pos) {
// break;
// }
// pos = n_pos;
// ans++;
// }
int pos = arr[u].second;
if (pos < arr[v].first) {
evts[pos].push_back({arr[v].first, arr[v].second, qi, ans});
for (auto& [l, r] : arr) {
if (l <= pos && arr[v].first <= r && r <= arr[v].second) {
pos = r;
ans++;
break;
}
}
if (arr[v].first <= pos && pos <= arr[v].second) {
ans++;
qans[qi] = ans;
} else {
qans[qi] = -1;
}
} else {
if (arr[v].first <= pos && pos <= arr[v].second) {
ans++;
qans[qi] = ans;
} else {
qans[qi] = -1;
}
}
}
for (auto& a : qans) {
if (a == -1) {
cout << "impossible" << endl;
} else {
cout << a << 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 |
1594 ms |
8148 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
Incorrect |
3 ms |
340 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
Incorrect |
3 ms |
340 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
Incorrect |
3 ms |
340 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1560 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 |
1594 ms |
8148 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |