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 erorp(x) cout<<#x<<"={"<<(x.F)<<" , "<<x.S<<"}"<<endl
#define print(v,r) f(i,0,r) cout<<v[i]<<" "; cout<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define Add(x,y) x=(x+y)%mod
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
const int N=2e5+99;
int n,m,Rank,ans,max_ans,id[N],a[N],s[N],t[N],vl[N],vr[N],fa[N],h[N],seg[N],NN,C,R,K[N],S[N],E[N];
vector<int> g[N];
int Fen[N];
void update(int x,int val) { x++; while(x<N) { Fen[x]+=val; x+=(x&-x); } }
int query(int x) { x++; int res=0; while(x>0) { res+=Fen[x]; x-=(x&-x); } return res; }
void build(){
f(i,0,n){
seg[i+n]=a[i];
}
f_(i,n-1,1){
seg[i]=max(seg[i*2+0],seg[i*2+1]);
}
}
int Get(int l,int r){
int ans=0;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l&1) maxm(ans,seg[l++]);
if (r&1) maxm(ans,seg[--r]);
}
return ans;
}
int find(int x){
int l=-1,r=n;
while(l+1<r){
int mid=(l+r)/2;
if(query(mid)<=x) l=mid;
else r=mid;
}
return r;
}
void dfs(int x){
fa[x]=x;
vl[x]=N,vr[x]=0;
f(i,0,g[x].size()){
h[g[x][i]]=h[x]+1;
dfs(g[x][i]);
minm(vl[x],vl[g[x][i]]);
maxm(vr[x],vr[g[x][i]]);
if(h[fa[g[x][i]]]>h[fa[x]]){
fa[x]=fa[g[x][i]];
}
}
if(x<n){
vl[x]=x;
vr[x]=x;
}
//cout<<x<<" "<<fa[x]<<" "<<vl[x]<<" "<<vr[x]<<endl;
//eror(Get(vl[x],vr[x]));
if(Get(vl[x],vr[x])<Rank && h[fa[x]]-h[x]+1>max_ans){
//eror(x);
ans=fa[x];
max_ans=h[fa[x]]-h[x]+1;
}
}
void make_tree(){
f(i,0,n){
id[i]=i;
update(i,1);
}
f(i,0,m){
vector<int> v;
int x,len=t[i]-s[i]+1;
f(T,0,len){
x=find(s[i]);
update(x,-1);
g[n+i].pb(id[x]);
//cout<<n+i<<" "<<id[x]<<endl;
}
id[x]=n+i;
update(x,1);
}
}
int GetBestPosition(int N, int C, int R, int *K, int *S, int *E){
n=N,m=C,Rank=R;
f(i,0,n-1) a[i]=K[i];
f(i,0,C){
s[i]=S[i];
t[i]=E[i];
}
build();
make_tree();
dfs(n+m-1);
//eror(max_ans);
return ans;
}
/*
int main(){
cin>>NN>>C>>R;
f(i,0,NN-1) cin>>K[i];
f(i,0,C){
cin>>S[i]>>E[i];
}
cout<<GetBestPosition(NN,C,R,K,S,E);
}
*/
/*
5 3 3
1 0 2 4
1 3
0 1
0 1
5 3 2
1 0 3 4
1 3
0 1
0 1
1 0 0
*/
Compilation message (stderr)
tournament.cpp: In function 'void dfs(int)':
tournament.cpp:7:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
7 | #define f(i,a,b) for(int i=a;i<b;i++)
......
57 | f(i,0,g[x].size()){
| ~~~~~~~~~~~~~~~
tournament.cpp:57:2: note: in expansion of macro 'f'
57 | f(i,0,g[x].size()){
| ^
tournament.cpp: In function 'void make_tree()':
tournament.cpp:26:31: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
26 | void update(int x,int val) { x++; while(x<N) { Fen[x]+=val; x+=(x&-x); } }
| ~^~
tournament.cpp:85:7: note: 'x' was declared here
85 | int x,len=t[i]-s[i]+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... |