# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
206232 | awlintqaa | 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.
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#include <bits/stdc++.h>
using namespace std;
#define sqr 200
#define mid (l+r)/2
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define era erase
#define C continue
#define mem(dp,i) memset(dp,i,sizeof(dp))
#define mset multiset
typedef long long ll;
typedef short int si;
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
const ll mod=1e9+7;
const ll inf= 4e18;
const ld pai=acos(-1);
#include "horses.h"
ll n ;
ll a[500009],cost[500009];
//
ll tree[2000009];
ll merge ( ll x , ll y ){
x%=mod;
y%=mod;
return (x*y)%mod;
}
void build(ll node,ll l,ll r){
if ( l == r ){
tree[node] = a[l];
return ;
}
build(node*2,l,mid);
build(node*2+1,mid+1,r);
tree[node] = merge ( tree[node*2] , tree[node*2+1] ) ;
}
void upd(ll node,ll l,ll r,ll id){
if ( l==r ){
tree[node] = a[l];
return ;
}
if ( id<=mid ) upd(node*2,l,mid,id);
else upd(node*2+1,mid+1,r,id);
tree[node] = merge ( tree[node*2] , tree[node*2+1] ) ;
}
ll query(ll node,ll l,ll r,ll s,ll e){
if ( l>r || s>r || e<l ) return 1;
if ( s<=l && e>=r ) return tree[node];
return merge( query(node*2,l,mid,s,e) , query(node*2+1,mid+1,r,s,e) );
}
//
ll MX [ 2000009];
void buildRMQ(int node,int l,int r){
if ( l==r ) {
MX [ node ] = cost[l];
return ;
}
buildRMQ(node*2,l,mid);
buildRMQ(node*2+1,mid+1,r);
MX [node] = max ( MX[node*2] , MX[node*2+1] );
}
void updRMQ(int node,int l,int r,int id){
if ( l==r ){
MX [ node ] = cost[l];
return ;
}
if ( id <= mid ) updRMQ(node*2,l,mid,id);
else updRMQ(node*2+1,mid+1,r,id);
MX [node] = max ( MX[node*2] , MX[node*2+1] );
}
ll RMQ ( int node,int l,int r,int s,int e){
if ( l>r || s>r || e<l ) return 0;
if ( s<=l && e>=r ) return MX[node];
return max ( RMQ ( node*2,l,mid,s,e ) , RMQ ( node*2+1,mid+1,r,s,e) );
}
//
set< pi > s;
void buildIntervals(){
for ( int i =0 ;i < n;i ++ ){
int j =i+1 ;
while ( j<n && a[j]==1 )j ++ ;
j -- ;
s.ins ( { i , j } ) ;
i = j ;
}
}
void add ( int pos ){
auto it = s.ub ( { pos , 1e9 } );
it -- ;
int l = it->fi , r = it->se;
if ( l == pos ) return ;
s.era ( it ) ;
s.ins ( { l , pos-1 } );
s.ins ( {pos, r } );
}
void del ( int pos ){
auto it = s.ub ( { pos , 1e9 } );
it -- ;
int l = it->fi , r = it->se;
if ( it == s.begin() ) return ;
it -- ;
s.era ( {l,r} ) ;
int L = it->fi , R = it->se;
s.era( it ) ;
s.ins ( { L , r } ) ;
}
ll check ( int l, int r ){
int num = 0 ;
for ( int i =l ;i <=r ;i ++ ) num += (a[l]>1);
return (num>=2);
}
ll solve(){
auto it = -- s.end();
ll crnt =1 ;
for ( auto u :s ){
if ( check(u->fi , u->se ) ){
while ( 1) {
}
}
}
for ( ; ; it--){
int l = it->fi , r = it->se;
crnt *= query(1,0,n-1,l,r);
if ( crnt > 2e9 ){
it++;
break;
}
if ( it == s.begin() ) break;
}crnt = 1;
ll ans = 0;
int ST = it->fi ;
if ( ST ) ans = cost[ ST - 1 ];
for( ; it!=s.end() ; it ++ ){
int l = it->fi , r =it->se;
ll A = query ( 1,0,n-1 , l, r);
ll COST = RMQ ( 1,0,n-1 , l, r);
crnt *= A;
ans = max ( ans, crnt * COST);
}
ans %= mod ;
ans *= query ( 1 , 0 , n-1 , 0 , ST-1 ) ;
ans %= mod ;
return ans;
}
int init(int N, int X[], int Y[]) {
n = N;
for ( int i =0 ;i < n ;i ++ ) a[i]=X[i];
for ( int i =0 ;i < n ;i ++ ) cost [i] = Y [i];
build(1,0,n-1);
buildRMQ(1,0,n-1);
buildIntervals();
return solve();
}
int updateX(int pos, int val) {
if ( a[pos] == 1 ) add(pos);
if ( val == 1 ) del(pos);
a[pos]=val;
upd(1,0,n-1,pos);
return solve();
}
int updateY(int pos, int val) {
cost[pos]=val;
updRMQ(1,0,n-1,pos);
return solve();
}