This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |