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))
template <typename Cb>
struct Cmp {
Cb cb;
Cmp(Cb cb) : cb(cb) {}
template <typename T>
bool operator()(const T& a, const T& b) const {
return cb(a) < cb(b);
}
};
template <typename T>
struct ST {
int n, logn;
vector<vector<T>> v;
ST(const vector<T>& arr)
: n(sz(arr)), logn(__lg(n)), v(logn + 1, vector<T>(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))]);
}
}
}
T query(int l, int r) const {
int k = __lg(r - l);
return min(v[k][l], v[k][r - (1 << k)]);
}
};
int coord_comp(const vector<int*>& arr) {
vector<int> vals;
for (auto& a : arr) {
vals.push_back(*a);
}
sort(begin(vals), end(vals));
vals.erase(unique(begin(vals), end(vals)), vals.end());
for (auto& a : arr) {
*a = int(lower_bound(begin(vals), end(vals), *a) - vals.begin());
}
return sz(vals);
}
void solve() {
int n, q;
cin >> n >> q;
vector<pair<int, int>> arr(n);
for (auto& [l, r] : arr) {
cin >> l >> r;
}
int m = coord_comp(({
vector<int*> v;
for (auto& [l, r] : arr) {
v.push_back(&l);
v.push_back(&r);
}
v;
}));
ST<pair<int, int>> st_arr(({
vector<pair<int, int>> v(m, {int(1e9), -1});
for (int i = 0; i < n; i++) {
auto& [l, r] = arr[i];
v[r] = min(v[r], {l, i});
}
v;
}));
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});
}
}
c_opt = st_arr.query(arr[i].first, arr[i].second + 1);
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... |