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 rep(i,n) for(int i=0;i<n;i++)
#define rng(i,x,n) for(int i=x;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3HspL4A ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vec(int);
void print(){cout<<"\n";}
template<class T,class...E>
void print(const T&v,const E&...u){cout<<v<<' ',print(u...);}
// e
using vp=vec(pii);
const int inf=1e9+11;
int cross(int a,int b,int c,int d){
if(c>=a and c<=b) return 1;
if(a>=c and a<=d) return 1;
if(d>=a and d<=b) return 1;
if(b>=c and b<=d) return 1;
return 0;
}
signed main(){
_3HspL4A;
int n;
cin>>n;
vp a(n);
rep(i,n){
cin>>a[i].fi>>a[i].se;
}
vi tmp;
rep(i,n){
tmp.pb(a[i].fi);
tmp.pb(a[i].se);
}
sort(tmp.begin(),tmp.end());
tmp.erase(unique(tmp.begin(),tmp.end()),tmp.end());
rep(i,n){
a[i].fi=lower_bound(tmp.begin(),tmp.end(),a[i].fi)-tmp.begin();
a[i].se=lower_bound(tmp.begin(),tmp.end(),a[i].se)-tmp.begin();
}
const int m=sz(tmp);
vi nxt(n,-1);
{
multiset<pii> mst;
int j=0;
rep(i,n){
pii p=a[i];
nxt[i]=i+1;
if(!sz(mst)){
mst.insert(p);
}else{
vi c={p.fi,p.se};
while(1){
bool pok=1;
for(auto v:c){
auto it=mst.lower_bound({v,-inf});
if(it!=mst.end()){
if(cross(it->fi,it->se,p.fi,p.se)) pok=0;
}
if(it!=mst.begin()){
it=prev(it);
if(cross(it->fi,it->se,p.fi,p.se)) pok=0;
}
}
if(pok) break;
mst.erase(mst.find({a[j].fi,a[j].se}));
nxt[j]=i;
j=j+1;
}
mst.insert(p);
}
}
rng(k,j,n){
nxt[k]=n;
}
}
vec(vi) f(25,vi(n+1));
rep(i,n){
f[0][i]=nxt[i];
}
f[0][n]=n;
rng(j,1,25){
rep(i,n+1){
f[j][i]=f[j-1][f[j-1][i]];
}
}
int q;
cin>>q;
rep(_,q){
int s,t;
cin>>s>>t;
s-=1;
int now=0,ans=inf;
per(j,25){
if(f[j][s]>=t){
ans=now+(1<<j);
}else{
s=f[j][s];
now+=(1<<j);
}
}
print(ans);
}
//
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:52:12: warning: unused variable 'm' [-Wunused-variable]
52 | const int m=sz(tmp);
| ^
# | 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... |