# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
206909 | LittleFlowers__ | Holiday (IOI14_holiday) | C++14 | 0 ms | 0 KiB |
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>
#include "holiday.h"
using namespace std;
#define int long long
#define in ({int x=0;int c=getchar(),n=0;for(;!isdigit(c);c=getchar()) n=(c=='-');for(;isdigit(c);c=getchar()) x=x*10+c-'0';n?-x:x;})
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l,int r){return l+rng()%(r-l+1);}
#define fasty ios_base::sync_with_stdio(0),cin.tie(0);
#define forinc(a,b,c) for(int a=b,_c=c;a<=_c;++a)
#define fordec(a,b,c) for(int a=b,_c=c;a>=_c;--a)
#define forv(a,b) for(auto&a:b)
#define fi first
#define se second
#define pb push_back
#define ii pair<int,int>
#define mt make_tuple
#define all(a) a.begin(),a.end()
#define reset(f, x) memset(f, x, sizeof(f))
#define bit(x,i) ((x>>(i-1))&1)
#define on(x,i) (x|(1ll<<(i-1)))
#define off(x,i) (x&~(1<<(i-1)))
#define gg exit(0);
const int N=100010,
M=500;
int n,s,d,ret;
int a[N],lf[N],rt[N],Lf[N],Rt[N];
vector<int> pval;
struct per{
per *l=nullptr,*r=nullptr;
int v=0,s=0;
void make(){
if(l) return;
l=new per;
r=new per;
}
}*T[N];
void upd(per *x,per *y,int l,int r,int i){
if(l==r){
x->v=y->v+1;
x->s=y->s+pval[i-1];
return;
}
x->make(); y->make();
int m=(l+r)/2;
if(m>=i){
x->r=y->r;
upd(x->l,y->l,l,m,i);
} else{
x->l=y->l;
upd(x->r,y->r,m+1,r,i);
}
x->v=x->l->v+x->r->v;
x->s=x->l->s+x->r->s;
}
int que(per *x,per *y,int l,int r,int k){
if(l==r)
return pval[l-1]*k;
x->make(); y->make();
int m=(l+r)/2;
int tmp=x->r->v-y->r->v;
if(tmp>=k)
return que(x->r,y->r,m+1,r,k);
else
return que(x->l,y->l,l,m,k-tmp)+x->r->s-y->r->s;
}
int findMaxAttraction(int n, int s, int d, int a[]){
s++;
fordec(i,n,1){
pval.pb(a[i]=a[i-1]);
}
/// ~~~~~ ///
sort(all(pval));
T[0]=new per;
forinc(i,1,n){
T[i]=new per;
a[i]=lower_bound(all(pval),a[i])-pval.begin()+1;
upd(T[i],T[i-1],1,n,a[i]);
}
forinc(i,2,d){
auto crt=[&](int j){
return que(T[min(n,s+i-j)],T[s],1,n,j);
};
auto Crt=[&](int j){
return que(T[min(n,s+(i-j)/2)],T[s],1,n,j);
};
auto clf=[&](int j){
return que(T[s-1],T[max(0ll,s-i+j-1)],1,n,j);
};
auto Clf=[&](int j){
return que(T[s-1],T[max(0ll,s-(i-j)/2-1)],1,n,j);
};
int le=1,ri=min(s-1,i/2),mi,f1,f2;
while(le<ri){
mi=(le+ri)/2;
f1=clf(mi), f2=clf(mi+1);
(f1<f2?le=mi+1:ri=mi);
}
forinc(j,max(1ll,le-M),min(min(s-1,i/2),le+M))
lf[i]=max(lf[i],clf(j));
le=1,ri=min(n-s,i/2);
while(le<ri){
mi=(le+ri)/2;
f1=crt(mi), f2=crt(mi+1);
(f1<f2?le=mi+1:ri=mi);
}
forinc(j,max(1ll,le-M),min(le+M,min(n-s,i/2)))
rt[i]=max(rt[i],crt(j));
le=1,ri=min(n-s,i/3);
while(le<ri){
mi=(le+ri)/2;
f1=Crt(mi), f2=Crt(mi+1);
(f1<f2?le=mi+1:ri=mi);
}
forinc(j,max(1ll,le-M),min(le+M,min(n-s,i/3)))
Rt[i]=max(Rt[i],Crt(j));
le=1,ri=min(s-1,i/3);
while(le<ri){
mi=(le+ri)/2;
f1=Clf(mi), f2=Clf(mi+1);
(f1<f2?le=mi+1:ri=mi);
}
forinc(j,max(1ll,le-M),min(min(s-1,i/3),le+M))
Lf[i]=max(Lf[i],Clf(j));
}
/// ~~~~~ ///
forinc(i,0,d){
ret=max({ret,Lf[i] + rt[d-i],Rt[i]+lf[d-i]});
if(i>1)
ret=max({ret,Lf[i-1]+rt[d-i]+pval[a[s]-1],Rt[i-1]+lf[d-i]+pval[a[s]-1]});
if(i<d)
ret=max({ret,Lf[i]+rt[d-i-1]+pval[a[s]-1],Rt[i]+lf[d-i-1]+pval[a[s]-1]});
}
return ret;
}