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;
set<pair<int,int>> rs;
auto no_intersections= [](set<pair<int,int>>& rs,pair<int,int>& q) -> bool {
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;
return !(it->second >= q.first);
};
int j = 1;
int dp[n+1][25];
for (int i=1;i<=n;i++) {
while (j<=n && no_intersections(rs,rng[j])) {
rs.insert(rng[j]);
j++;
}
dp[i][0] = j-1;
rs.erase(rng[i]);
}
for (int i=1;i<25;i++) {
for (int j=1;j<=n;j++) {
dp[j][i] = dp[min(n,dp[j][i-1]+1)][i-1];
}
}
int q;
cin >> q;
while (q--) {
int a,b;
cin >> a >> b;
int haha = 0;
for (int i=24;i>=0;i--) {
if (dp[a][i] < b) {
a = dp[a][i]+1;
haha+=(1<<i);
}
}
cout << haha+1 << endl;
}
}
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... |