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<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#define ll long long
using namespace std;
struct node{
ll val,tag;
};
vector<node> seg;
void push(ll l,ll r,int ind){
if(l==r) return;
ll mid=(l+r)>>1;
seg[ind*2].val+=(mid-l+1)*seg[ind].tag;
seg[ind*2+1].val+=(r-mid)*seg[ind].tag;
seg[ind*2].tag+=seg[ind].tag;
seg[ind*2+1].tag+=seg[ind].tag;
seg[ind].tag=0;
}
ll query(ll l,ll r,int start,int end,int ind){
if(r<start||end<l) return 0;
if(start<=l&&r<=end){
return seg[ind].val;
}
ll mid=(l+r)>>1;
push(l,r,ind);
return query(l,mid,start,end,ind*2)+query(mid+1,r,start,end,ind*2+1);
}
void modify(ll l,ll r,int start,int end,ll num,int ind){
if(r<start||end<l) return;
if(start<=l&&r<=end){
seg[ind].val+=(r-l+1)*num;
seg[ind].tag+=num;
return;
}
ll mid=(l+r)>>1;
push(l,r,ind);
modify(l,mid,start,end,num,ind*2);
modify(mid+1,r,start,end,num,ind*2+1);
seg[ind].val=(seg[ind*2].val+seg[ind*2+1].val);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n,q;
cin>>n;
vector<pair<ll,ll>> vec(n+1);
for(int i=1;i<=n;i++) cin>>vec[i].first>>vec[i].second;
vector<ll> cc;
for(int i=1;i<=n;i++){
cc.push_back(vec[i].first);
cc.push_back(vec[i].second);
}
sort(cc.begin(),cc.end());
cc.resize(unique(cc.begin(),cc.end())-cc.begin());
for(int i=1;i<=n;i++){
vec[i].first=lower_bound(cc.begin(),cc.end(),vec[i].first)-cc.begin()+1;
vec[i].second=lower_bound(cc.begin(),cc.end(),vec[i].second)-cc.begin()+1;
}
vector<int> far(n+1);
seg.resize(4*cc.size()+4);
ll len=cc.size()+1;
int ii=1;
for(int i=1;i<=n;i++){
while(ii<=n&&query(1,len,vec[ii].first,vec[ii].second,1)==0) modify(1,len,vec[ii].first,vec[ii].second,1,1),ii++;
far[i]=ii;
modify(1,len,vec[i].first,vec[i].second,-1,1);
}
if(n<=5000){
vector<vector<ll>> preans(n+1,vector<ll>(n+1));
for(int i=1;i<=n;i++){
int cur=1,j=i,tmp=i;
while(j<=n){
for(;j<far[tmp];j++){
preans[i][j]=cur;
}
cur++;
tmp=far[tmp];
}
}
int a,b;
cin>>q;
for(int i=0;i<q;i++){
cin>>a>>b;
cout<<preans[a][b]<<"\n";
}
return 0;
}
int a,b;
cin>>q;
for(int i=0;i<q;i++){
cin>>a>>b;
int ans=0;
while(a<=b){
a=far[a];
ans++;
}
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... |