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 <algorithm>
#include <iostream>
#include <iomanip>
#include <numeric>
#include <cassert>
#include <vector>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P,int> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-8;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
class Segment_Tree{
public:
int n;
vi date,ind;
Segment_Tree(int n_){
n=1;
while(n<n_) n*=2;
date=vi(2*n-1);
ind=vi(n);
for(int i=0;i<n;i++){
Update(i,1);
ind[i]=i;
}
}
void Update(int k,int x){
k+=n-1;
date[k]+=x;
while(k>0){
k=(k-1)/2;
date[k]=date[k*2+1]+date[k*2+2];
}
}
int Query(int x){
/* for(int i=0;i<n;i++) cout<<date[i+n-1]<<' ';
cout<<endl;*/
int k=0;
while(k<n-1){
if(date[2*k+1]<x) x-=date[2*k+1],k=2*k+2;
else k=2*k+1;
}
return k-n+1;
}
int Open(int k){return date[k+n-1];}
};
const int M=20;
vvi g,par;
vi lt,rt;
int n;
void dfs(int v){
if(v>=n) lt[v]=inf,rt[v]=-inf;
for(auto u:g[v]){
dfs(u);
// if(v==n+1) cout<<u<<' '<<lt[u]<<' '<<rt[u]<<endl;
lt[v]=min(lt[v],lt[u]);
rt[v]=max(rt[v],rt[u]);
}
}
int GetBestPosition(int N,int C,int R,int *K,int *S,int *E){
n=N;
g=vvi(N+C);
lt=rt=vi(N+C);
par=vvi(N+C,vi(M,-1));
Segment_Tree st(N);
for(int i=0;i<C;i++){
int tmp=0;
for(int j=0;j<E[i]-S[i]+1;j++){
int id=st.Query(S[i]+1);
st.Update(id,-1);
g[N+i].push_back(st.ind[id]);
// cout<<N+i<<' '<<id<<' '<<st.ind[id]<<endl;
par[st.ind[id]][0]=N+i;
if(!j) tmp=id;
}
st.Update(tmp,1);
st.ind[tmp]=N+i;
}
for(int i=0;i<N;i++) lt[i]=i,rt[i]=i+1;
dfs(N+C-1);
for(int i=1;i<M;i++) for(int j=0;j<N+C;j++){
int v=par[j][i-1];
if(v>=0) par[j][i]=par[v][i-1];
}
int mx=0,id=0;
int il=0,ir=0;
auto f=[&](int v,int m){
// cout<<v<<' ';
for(int i=0;i<M;i++) if(m&1<<i&&v>=0) v=par[v][i];
// cout<<v<<' '<<lt[v]<<' '<<rt[v]<<' '<<m<<endl;
return v>=0&&il<=lt[v]&&rt[v]<=ir+1;
};
// for(int i=0;i<N+C;i++) cout<<lt[i]<<','<<rt[i]<<endl;
for(int i=0;i<N;i++){
ir=max(i,ir);
while(ir<N-1&&K[ir]<R) ir++;
if(i&&R<K[i-1]) il=i;
int l=0,r=n+1;
// cout<<'['<<il<<','<<ir<<']'<<endl;
while(r-l>1){
int m=(l+r)/2;
if(f(i,m)) l=m;
else r=m;
}
// cout<<i<<' '<<l<<endl;
if(mx<l) mx=l,id=i;
}
return id;
}
/*
int main(){
int N,C,R,K[1000],S[1000],E[1000];
cin>>N>>C>>R;
for(int i=0;i<N;i++) cin>>K[i];
for(int i=0;i<C;i++) cin>>S[i]>>E[i];
cout<<GetBestPosition(N,C,R,K,S,E)<<endl;
}*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |