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>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> l(n), r(n);
vector<int> cs;
for (int i = 0; i < n; i++) {
cin >> l[i] >> r[i];
}
multiset<pair<int, int>> segs;
auto Check = [&](int L, int R) {
auto it = segs.lower_bound({L, -1});
if (it != segs.end()) {
if (it->first <= R) {
return false;
}
}
if (it != segs.begin()) {
it = prev(it);
if (it->second >= L) {
return false;
}
}
return true;
};
const int L = 20;
vector<vector<int>> jump(n + 1, vector<int>(L, n));
int ptr = n - 1;
for (int i = n - 1; i >= 0; i--) {
while (!Check(l[i], r[i])) {
segs.erase(segs.find({l[ptr], r[ptr]}));
ptr--;
}
segs.insert({l[i], r[i]});
jump[i][0] = ptr;
}
for (int j = 1; j < L; j++) {
for (int i = 0; i < n; i++) {
if (jump[i][j - 1] < n) {
jump[i][j] = jump[jump[i][j - 1] + 1][j - 1];
}
}
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
--l; --r;
int ans = 0;
for (int j = L - 1; j >= 0; j--) {
if (jump[l][j] <= r) {
l = jump[l][j] + 1;
ans += 1 << j;
}
if (l >= n) {
break;
}
}
cout << ans + (l <= r ? 1 : 0) << '\n';
}
return 0;
}
# | 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... |