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>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template<typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;}
template<typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;}
int n,a[100005],fnw[100005],sl[200005],sr[200005],fa[200005],dep[200005],pos=10000000;
vector<int> g[200005];
int getf(int x){return fa[x]==x?x:fa[x]=getf(fa[x]);}
void modify(int x,int v){
for(;x<100005;x+=x&-x)fnw[x]+=v;
}
int getp(int x){
int i=0;
for(int b=20;b>=0;b--)if(i+(1<<b)<100005&&x>fnw[i+(1<<b)])i+=(1<<b),x-=fnw[i];
return i+1;
}
void dfs(int x){
if(x<=n)return (void)(pos=min(pos,x));
sort(g[x].begin(),g[x].end(),[&](int i,int j){return dep[i]>dep[j]||(dep[i]==dep[j]&&sl[i]<sl[j]);});
dfs(g[x][0]);
}
int GetBestPosition(int N,int C,int R,int*K,int*S,int*E){
a[1]=R;n=N;
for(int i=2;i<=N;i++)a[i]=K[i-2];
for(int i=1;i<=N;i++)a[i]=(a[i]>R?1:0);
set<int> st;
for(int i=1;i<=N;i++)modify(i,1),st.insert(i);
for(int i=1;i<=N;i++)sl[i]=sr[i]=fa[i]=i;
for(int i=N+1;i<=N+C;i++)fa[i]=i,sl[i]=N+C,sr[i]=0;
for(int i=1;i<=C;i++){
int l=getp(S[i-1]+1),r=getp(E[i-1]+1);
assert(l>=1&&r<=n);
while(1){
auto it=st.lower_bound(l);
if(it==st.end()||*it>r)break;
chkmax(dep[N+i],dep[getf(*it)]);
chkmin(sl[N+i],sl[getf(*it)]);
chkmax(sr[N+i],sr[getf(*it)]);
g[N+i].pb(getf(*it));
modify(*it,-1);
st.erase(it);
}
modify(l,1);
st.insert(l);
fa[getf(l)]=N+i;
dep[N+i]++;
// for(int j=1;j<=N;j++)printf("%d ",query(j)-query(j-1));puts("");
}
int ans=0;
for(int i=1;i<=N;i++)a[i]=a[i-1]+a[i];
for(int i=N+1;i<=N+C;i++)if(a[sr[i]]==a[sl[i]]&&ans<dep[i])ans=dep[i];
for(int i=N+1;i<=N+C;i++)if(a[sr[i]]==a[sl[i]]&&ans==dep[i])dfs(i);
return (pos>n?0:pos-1);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |