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;
#define sp << " " <<
#define int long long
#define vi vector<int>
#define pb push_back
#define F(xxx,yyy) for (int xxx=1;xxx<=yyy;xxx++)
const int N = 2e5+1;
const int MOD = 1e9;
void solve() {
int n;
cin >> n;
vector<pair<int,int>> rng(n+1);
for (int i=1;i<=n;i++) cin >> rng[i].first >> rng[i].second;
int go[n+1];
set<pair<int,int>> rs;
auto no_intersections= [](set<pair<int,int>>& rs,pair<int,int>& q) {
if (rs.empty()) return 1;
auto it = rs.lower_bound({q.first,0});
if (it != rs.end()) {
if (it->first <= q.second) return 0;
}
if (it == rs.begin()) return 1;
--it;
if (it->second >= q.first) return 0;
return 1;
};
int j = 1;
for (int i=1;i<=n;i++) {
while (j<=n && no_intersections(rs,rng[j])) {
rs.insert(rng[j]);
j++;
}
go[i] = j-1;
rs.erase(rng[i]);
}
int dp[n+1][25];
for (int i=1;i<=n;i++) dp[i][0] = go[i];
for (int i=1;i<25;i++) {
for (int j=1;j<=n;j++) {
dp[j][i] = dp[dp[j][i-1]+1][i-1];
}
}
auto upq = [&](int node,int k) {
for (int i=24;i>=0;i--) {
if (k&(1<<i)) node = dp[node][i];
}
return node;
};
int q;
cin >> q;
while (q--) {
int a,b;
cin >> a >> b;
for (int mv = 1;mv<25;mv++) {
if (upq(a,mv) >= b) {
cout << mv << endl;
break;
}
}
}
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t = 1;
//cin >> t;
while (t --> 0) 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... |