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 "horses.h"
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
int N; vector<ll> X, Y;
int nt = 1;
vector<ll> st;
vector<ll> st_mod;
int mx_index = 0;
ll get_range(int l, int r, int k, int x, int y)
{
if (y < l || x > r) return 1;
if (x >= l && y <= r) return st[k];
int c = (x + y) / 2;
return get_range(l, r, k*2, x, c) * get_range(l, r, k*2|1, c+1, y);
}
ll get_mod_range(int l, int r, int k, int x, int y)
{
if (y < l || x > r) return 1;
if (x >= l && y <= r) return st_mod[k];
int c = (x + y) / 2;
return (get_mod_range(l, r, k*2, x, c) * get_mod_range(l, r, k*2|1, c+1, y)) % mod;
}
int get_full_max()
{
ll res = (X[0] * Y[0]) % mod, cnt = X[0];
mx_index = 0;
for (int i = 1; i < N; i++)
{
cnt = (cnt * X[i]) % mod;
ll last_mult = Y[mx_index];
ll mult_range = get_range(mx_index + 1, i, 1, 0, nt - 1);
if (mult_range >= last_mult || mult_range * Y[i] >= last_mult)
{
res = (cnt * Y[i]) % mod;
mx_index = i;
}
}
return (int)res;
}
int get_max()
{
int s = max(0, N - 31);
mx_index = s;
ll cnt = get_mod_range(0, s, 1, 0, nt - 1);
ll res = (cnt * Y[s]) % mod;
for (int i = s + 1; i < min(N - 1, s + 60); i++)
{
cnt = (cnt * X[i]) % mod;
ll last_mult = Y[mx_index];
ll mult_range = get_range(mx_index + 1, i, 1, 0, nt - 1);
if (mult_range >= last_mult || mult_range * Y[i] >= last_mult)
{
res = (cnt * Y[i]) % mod;
mx_index = i;
}
}
return res;
}
int init(int n, int x[], int y[])
{
N = n;
X.resize(N);
Y.resize(N);
for (int i = 0; i < N; i++)
{
X[i] = x[i];
Y[i] = y[i];
}
while (nt < N) nt <<= 1;
st = st_mod = vector<ll>(2 * nt, 1);
for (int i = 0; i < N; i++)
{
st[nt + i] = X[i];
st_mod[nt + i] = X[i];
}
for (int i = nt-1; i >= 1; i--)
{
st[i] = st[i*2] * st[i*2|1];
st_mod[i] = (st_mod[i*2] * st_mod[i*2|1]) % mod;
}
return get_full_max();
}
int updateX(int pos, int val)
{
X[pos] = val;
pos += nt;
st[pos] = val;
st_mod[pos] = val;
for (pos /= 2; pos >= 1; pos /= 2)
{
st[pos] = st[pos*2] * st[pos*2|1];
st_mod[pos] = (st_mod[pos*2] * st_mod[pos*2|1]) % mod;
}
return get_max();
}
int updateY(int pos, int val)
{
Y[pos] = val;
return get_max();
}
Compilation message (stderr)
horses.cpp: In function 'int get_max()':
horses.cpp:75:9: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
75 | return res;
| ^~~
# | 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... |