# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
787570 | vnm06 | Horses (IOI15_horses) | 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 "horses.h"
#include<bits/stdc++.h>
using namespace std;
long long mod=1e9+7;
long long n;
long long a[500005];
set<long long> st;
long long ost[2000005];
long long tree[2000005];
void update(long long v, long long le, long long ri, long long idx, long long val)
{
if(le==ri) {tree[v]=val; return;}
long long mid=(le+ri)/2;
if(idx<=mid) update(2*v, le, mid, idx, val);
else update(2*v+1, mid+1, ri, idx, val);
tree[v]=max(tree[2*v], tree[2*v+1]);
}
long long query(long long v, long long le, long long ri, long long be, long long en)
{
if(en<be) return 0;
if(le>en || ri<be) return 0;
if(be<=le && ri<=en) return tree[v];
long long mid=(le+ri)/2;
return max(query(2*v, le, mid, be, en), query(2*v+1, mid+1, ri, be, en));
}
void update2(long long v, long long le, long long ri, long long idx, long long val)
{
if(le==ri) {ost[v]=val; return;}
long long mid=(le+ri)/2;
if(idx<=mid) update2(2*v, le, mid, idx, val);
else update2(2*v+1, mid+1, ri, idx, val);
ost[v]=(ost[2*v]*ost[2*v+1])%mod;
}
long long query2(long long v, long long le, long long ri, long long be, long long en)
{
if(en<be) return 1;
if(le>en || ri<be) return 1;
if(be<=le && ri<=en) return ost[v];
long long mid=(le+ri)/2;
return (query2(2*v, le, mid, be, en)*query2(2*v+1, mid+1, ri, be, en))%mod;
}
long long spc[50], cf[50], brt=0;
long long solve()
{
brt=0;
long long ccf=1;
if(st.empty()) return query(1, 0, n-1, 0, n-1);
set<long long>::iterator it=st.begin();
while(it!=st.end())
{
spc[brt]=-(*it);
cf[brt]=a[-(*it)];
brt++;
ccf*=a[-(*it)];
it++;
if(ccf>1e9) break;
}
reverse(spc, spc+brt);
reverse(cf, cf+brt);
long long ans=0, id=0;
if(ccf<=1e9) ans=query(1, 0, n-1, 0, n-1);
long long rc=1;
for(long long i=0; i<brt; i++)
{
if(i || ccf<1e9) rc*=a[i];
long long tz=query(1, 0, n-1, spc[i], n-1);
if(ans<tz*rc) ans=tz*rc;
}
if(ccf<=1e9) return ans%mod;
return ((ans%mod)*query2(1, 0, n-1, 0, spc[0]))%mod;
}
long long init(long long N, long long X[], long long Y[])
{
n=N;
for(long long i=0; i<n; i++)
{
update(1, 0, n-1, i, Y[i]);
update2(1, 0, n-1, i, X[i]);
a[i]=X[i];
if(a[i]>1) st.insert(-i);
}
return solve();
}
long long updateX(long long pos, long long val)
{
if(a[pos]==1 && val>1) st.insert(-pos);
else if(a[pos]>1 && val==1) st.erase(-pos);
a[pos]=val;
return solve();
}
long long updateY(long long pos, long long val)
{
update(1, 0, n-1, pos, val);
return solve();
}