#include "horses.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using lld = long double;
using ii = pair<int,int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vii = vector<ii>;
using vpll = vector<pll>;
using vlld = vector<lld>;
#define all(x) x.begin(),x.end()
#define lsb(x) x&(-x)
#define gcd(a,b) __gcd(a,b)
#define sz(x) (int)x.size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fls cout.flush()
#define fore(i, l, r) for (auto i = l; i < r; i++)
#define fo(i, n) fore (i, 0, n)
#define forex(i, r, l) for (auto i = r-1; i >= l; i--)
#define ffo(i, n) forex (i, n, 0)
bool cmin(ll &a, ll b) { if (b < a) { a=b; return 1; } return 0; }
bool cmax(ll &a, ll b) { if (b > a) { a=b; return 1; } return 0; }
const ll INF = 1e18;
const int N = 5e5 + 7, mod = 1e9 + 7;
int x[N], y[N], n;
struct SegTree {
vll st;
ll n;
SegTree () {}
SegTree (ll n):n(n), st(4*n+4, 1) {}
void update(ll i, ll v){update(1,0,n-1,i,v);}
void update(ll id,ll l,ll r,ll i,ll v){
if(l==r)st[id] = v;
else{
ll m = (l+r)/2;
if(i<=m) update(id*2,l,m,i,v);
else update(id*2+1,m+1,r,i,v);
st[id] = (st[id*2] * st[id*2+1])%mod;
}
}
ll query(ll l,ll r){return query(1,0,n-1,l,r);}
ll query(ll id,ll l,ll r,ll i,ll j){
if(l>j || r<i)return 1;
if(l>=i && r<=j)return st[id];
ll m = (l+r)/2;
return (query(id*2,l,m,i,j) * query(id*2+1,m+1,r,i,j))%mod;
}
};
struct SegTreemx {
vector<array<ll, 2>> st;
ll n;
SegTreemx () {}
SegTreemx (ll n):n(n), st(4*n+4, array<ll, 2>{-INF, 0}) {}
void update(ll i, array<ll, 2> v){update(1,0,n-1,i,v);}
void update(ll id,ll l,ll r,ll i,array<ll, 2> v){
if(l==r)st[id] = v;
else{
ll m = (l+r)/2;
if(i<=m) update(id*2,l,m,i,v);
else update(id*2+1,m+1,r,i,v);
st[id] = max(st[id*2], st[id*2+1]);
}
}
array<ll, 2> query(ll l,ll r){return query(1,0,n-1,l,r);}
array<ll, 2> query(ll id,ll l,ll r,ll i,ll j){
if(l>j || r<i)return {-INF, -INF};
if(l>=i && r<=j)return st[id];
ll m = (l+r)/2;
return max(query(id*2,l,m,i,j), query(id*2+1,m+1,r,i,j));
}
};
SegTree st;
SegTreemx sty;
set<ll> imps;
int calc () {
map<int, array<ll, 2>> mx;
ll suf = 1, ls = n-1;
for (ll _: imps) {
ll i = -_;
mx[suf] = sty.query(i, ls);
suf *= x[i];
if (suf >= 1e9) break;
ls = i - 1;
}
if (suf < 1e9) {
mx[suf] = sty.query(0, ls);
}
if (imps.size() == 0) {
return sty.query(0, n-1)[0];
}
// ffo (i, n-1) {
// suf *= x[i+1];
// if (suf >= 1e9) break;
// mx[suf] = max(mx[suf], array<int, 2>{y[i], i});
// }
ll bst = n-1;
suf = 1;
for (auto [act_suf, v]: mx) {
if (1ll*v[0] > 1ll*(act_suf/suf)*y[bst]) {
bst = v[1];
suf = act_suf;
}
}
ll ans = st.query(0, bst);
(ans *= y[bst]) %= mod;
return int(ans);
}
void add (ll i) { if (x[i]>1) imps.insert(-i); }
void rem (ll i) { if (x[i]>1) imps.erase(-i); }
int init(int NN, int X[], int Y[]) {
n = NN;
st = SegTree(n);
sty = SegTreemx(n);
fo (i, n) {
x[i] = X[i];
y[i] = Y[i];
}
fo (i, n) st.update(i, x[i]);
fo (i, n) sty.update(i, array<ll, 2> {y[i], i});
fo (i, n) add(i);
return calc();
}
int updateX(int pos, int val) {
rem(pos);
x[pos] = val;
add(pos);
st.update(pos, val);
return calc();
}
int updateY(int pos, int val) {
sty.update(pos, array<ll, 2>{val, pos});
y[pos] = val;
return calc();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |