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;
using ll = long long int;
const int maxn = 2e5 + 5;
const int oo = 1e9 + 7;
const ll inf = 1e18;
int n, q;
int l[maxn], r[maxn];
int up[maxn][22];
set<pair<int, int>> left_boud;
int can_add(int idx) {
if (left_boud.empty())
return 1;
auto it = left_boud.lower_bound({l[idx], 0});
if (it != left_boud.end()) {
int id = (*it).second;
if (r[idx] >= l[id])
return 0;
}
if (it != left_boud.begin()) {
it--;
int id = (*it).second;
if (r[id] >= l[idx])
return 0;
}
return 1;
}
int get_ans(int u, int v) {
int ans = 0;
for (int len = 21; len >= 0; len--) {
if (up[u][len] <= v) {
u = up[u][len];
ans += (1 << len);
}
}
return (ans + 1);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> l[i] >> r[i];
}
int nxt = 1;
for (int i = 1; i <= n; i++) {
while (nxt <= n && can_add(nxt))
left_boud.insert({l[nxt], nxt}), nxt++;
up[i][0] = nxt;
left_boud.erase({l[i], i});
}
up[n + 1][0] = n + 1;
for (int len = 1; len < 22; len++) {
for (int i = 1; i <= n + 1; i++) {
up[i][len] = up[up[i][len - 1]][len - 1];
}
}
cin >> q;
while (q--) {
int p, q;
cin >> p >> q;
cout << get_ans(p, q) << '\n';
}
}
# | 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... |