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/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e5 + 2;
const int inf = 1e9;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
vector<int> l(n), r(n);
for (int i = 0; i < n; i++) {
cin >> l[i] >> r[i];
}
vector<int> order(n);
iota(order.begin(), order.end(), 0);
sort(order.begin(), order.end(), [&] (int i, int j) {
if (r[i] != r[j]) return r[i] < r[j];
return l[i] < l[j];
});
vector<int> gdesibreti(n);
for (int i = 0; i < n; i++) gdesibreti[order[i]] = i;
vector<int> tl(n), tr(n);
for (int i = 0; i < n; i++) {
tl[i] = l[order[i]];
tr[i] = r[order[i]];
}
swap(l, tl); swap(r, tr);
vector<vector<pair<int, int>>> rmq(n, vector<pair<int, int>>(17));
for (int i = 0; i < n; i++) rmq[i][0] = {l[i], i};
for (int j = 1; j < 17; j++) {
for (int i = 0; i + (1 << j) <= n; i++) {
rmq[i][j] = min(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
}
}
vector<int> logs(n + 1);
int z = 0;
for (int i = 1; i <= n; i++) {
if (i == (1 << (z + 1))) z += 1;
logs[i] = z;
}
auto Query = [&] (int l, int r) {
int o = logs[r - l + 1];
return min(rmq[l][o], rmq[r - (1 << o) + 1][o]);
};
vector<int> dokle(n, -1);
for (int i = 0; i < n; i++) {
int lo = 0, hi = i - 1, idx = i;
while (lo <= hi) {
int mid = lo + hi >> 1;
if (l[i] <= r[mid]) {
hi = mid - 1;
idx = mid;
}
else lo = mid + 1;
}
dokle[i] = idx;
}
vector<vector<int>> lift(n, vector<int>(17));
for (int i = 0; i < n; i++) lift[i][0] = dokle[i];
for (int j = 1; j < 17; j++) {
for (int i = 0; i < n; i++) {
lift[i][j] = lift[Query(lift[i][j - 1], i).second][j - 1];
}
}
while (q--) {
int a, b;
cin >> a >> b;
a -= 1; b -= 1;
a = gdesibreti[a]; b = gdesibreti[b];
if (a == b) {
cout << 0 << en;
continue;
}
if (l[b] <= r[a] && r[a] <= r[b]) {
cout << 1 << en;
continue;
}
if (a > b) {
cout << "impossible" << en;
continue;
}
int ans = 0;
for (int i = 16; i >= 0; i--) {
if (a < lift[b][i]) {
ans += (1 << i);
b = Query(lift[b][i], b).second;
}
}
if (a < lift[b][0]) cout << "impossible" << en;
else cout << ans + 1 << en;
}
return 0;
}
Compilation message (stderr)
events.cpp: In function 'int main()':
events.cpp:56:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | int mid = lo + hi >> 1;
| ~~~^~~~
# | 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... |