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;
const int N = 2e5+5,M=18;
struct Node{
int l=-1,r=-1,mx=0,lzy=-1;
};
vector<Node>t(1);
void prop(int i){
if(t[i].lzy==-1)return;
if(t[i].l==-1){
t[i].l=t.size();
t.emplace_back();
}
if(t[i].r==-1){
t[i].r=t.size();
t.emplace_back();
}
t[t[i].l].mx=max(t[t[i].l].mx,t[i].lzy);
t[t[i].r].mx=max(t[t[i].r].mx,t[i].lzy);
t[t[i].l].lzy=max(t[t[i].l].lzy,t[i].lzy);
t[t[i].r].lzy=max(t[t[i].r].lzy,t[i].lzy);
t[i].lzy=-1;
}
int get(int i,int l,int r,int s,int e){
if(l>=s&&r<=e)return t[i].mx;
if(l>e||r<s)return 0;
int mid=(l+r)/2;
prop(i);
if(t[i].l==-1){
t[i].l=t.size();
t.emplace_back();
}
if(t[i].r==-1){
t[i].r=t.size();
t.emplace_back();
}
return max(get(t[i].l,l,mid,s,e),get(t[i].r,mid+1,r,s,e));
}
void upd(int i,int l,int r,int s,int e,int v){
if(l>=s&&r<=e){
t[i].mx=max(t[i].mx,v);
t[i].lzy=v;
return;
}
if(l>e||r<s)return;
int mid=(l+r)/2;
if(t[i].l==-1){
t[i].l=t.size();
t.emplace_back();
}
if(t[i].r==-1){
t[i].r=t.size();
t.emplace_back();
}
upd(t[i].l,l,mid,s,e,v);upd(t[i].r,mid+1,r,s,e,v);
t[i].mx=max(t[t[i].l].mx,t[t[i].r].mx);
}
int x,q,l[N],r[N],nxt[N][20];
int main(){
ios::sync_with_stdio(0);cin.tie(0);
cin>>x;
for(int i=1;i<=x;i++)cin>>l[i]>>r[i];
for(int i=1;i<=x;i++){
nxt[i][0]=max(nxt[i-1][0],get(0,1,1e9,l[i],r[i]));
upd(0,1,1e9,l[i],r[i],i);
for(int j=1;j<=M;j++)
nxt[i][j]=nxt[nxt[i][j-1]][j-1];
}
cin>>q;
while(q--){
int c1,c2,ans=1;cin>>c1>>c2;
for(int i=M;i>=0;i--)
if(nxt[c2][i]>=c1)
c2=nxt[c2][i],ans+=(1<<i);
cout<<ans<<"\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... |