#include <iostream>
#include <vector>
#include <set>
using namespace std;
vector<vector<int>> lift;
int findLift(int from, int mn){
int i = 0;
int par = 1;
while(lift[from][i] <= mn) i++, par*=2;
return par/2 + (i>0?findLift(lift[from][i-1], mn):0);
}
int main(void){
// freopen("input.txt", "r", stdin);
vector<pair<int, int>> h;
int n;
cin>>n;
h.assign(n, {});
for(int i = 0; i<n; i++) cin>>h[i].first>>h[i].second;
vector<int> step(n, 0);
set<pair<int, pair<int, int>>> s;
int nextSt = n;
int p2 = n;
for(int i = n-1; i>=0; i--){
auto e = s.lower_bound({h[i].first, {0, 0}});
while(e!=s.end() && e->first <= h[i].second){
p2--;
s.erase({h[p2].first, {h[p2].second, p2}});
e = s.lower_bound({h[i].first, {0, 0}});
}
while(e!= s.end() && e!= s.begin() && prev(e)->second.first >= h[i].first){
p2--;
s.erase({h[p2].first, {h[p2].second, p2}});
e = s.lower_bound({h[i].first, {0, 0}});
}
s.insert({h[i].first, {h[i].second, i}});
step[i] = p2;
}
lift.assign(n+1, vector<int>(20, -1));
for(int i = 0; i<20; i++) lift[n][i] = n;
for(int i = 0; i<n; i++) lift[i][0] = step[i];
for(int i = n-1; i>=0; i--){
for(int j = 1; j<20; j++){
lift[i][j] = lift[lift[i][j-1]][j-1];
}
}
int q;
cin>>q;
for(int i = 0; i<q; i++){
int l, r;
cin>>l>>r;
cout<<findLift(l-1, r-1)+1<<endl;
}
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... |