This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//
// Created by adavy on 2/11/2023.
//
#include <bits/stdc++.h>
#include "horses.h"
using namespace std;
using ll = long long;
using ld = long double;
using db = double;
using str = string; // yay python!
using ii = pair<int,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vii = vector<ii>;
using vpl = vector<pl>;
using vpd = vector<pd>;
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>;
tcT, size_t SZ> using AR = array<T,SZ>;
tcT> using PR = pair<T,T>;
// pairs
#define mp make_pair
#define f first
#define s second
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
#define len(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define eb emplace_back
#define pf push_front
const int MOD = 1e9+7; // 998244353;
const int MX = 2e5+5;
const ll INF = 1e18; // not too close to LLONG_MAX
const ld PI = acos((ld)-1);
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!!
#include <stdio.h>
#include <assert.h>
struct Modular {
int value;
static const int MOD_value = MOD;
Modular(long long v = 0) { value = v % MOD; if (value < 0) value += MOD;}
Modular(long long a, long long b) : value(0){ *this += a; *this /= b;}
Modular& operator+=(Modular const& b) {value += b.value; if (value >= MOD) value -= MOD; return *this;}
Modular& operator-=(Modular const& b) {value -= b.value; if (value < 0) value += MOD;return *this;}
Modular& operator*=(Modular const& b) {value = (long long)value * b.value % MOD;return *this;}
friend Modular mexp(Modular a, long long e) {
Modular res = 1; while (e) { if (e&1) res *= a; a *= a; e >>= 1; }
return res;
}
friend Modular inverse(Modular a) { return mexp(a, MOD - 2); }
Modular& operator/=(Modular const& b) { return *this *= inverse(b); }
friend Modular operator+(Modular a, Modular const b) { return a += b; }
friend Modular operator-(Modular a, Modular const b) { return a -= b; }
friend Modular operator-(Modular const a) { return 0 - a; }
friend Modular operator*(Modular a, Modular const b) { return a *= b; }
friend Modular operator/(Modular a, Modular const b) { return a /= b; }
friend std::ostream& operator<<(std::ostream& os, Modular const& a) {return os << a.value;}
friend bool operator==(Modular const& a, Modular const& b) {return a.value == b.value;}
friend bool operator!=(Modular const& a, Modular const& b) {return a.value != b.value;}
};
vl X, Y;
ll sz;
vl seg;
set<int> brks;
Modular totalX = 1;
void update(int i, ll x){
i += sz;
seg[i] = x;
i >>= 1;
//cout << i << endl;
while (i > 0){
seg[i] = max(seg[2*i], seg[2*i+1]);
i >>= 1;
}
}
ll query(int l, int r){
l += sz; r += sz;
ll ans = 0;
while(l<=r){
if (l&1) ans = max(ans, seg[l++]);
if (!(r&1)) ans = max(ans, seg[r--]);
l>>=1;
r>>=1;
}
return ans;
}
ll solve(){
//cout << totalX << endl;
ll neg_amount = 1;
ll pos_amount = 1;
ll best = 0;
auto ptr = brks.end();
ptr--; ptr--;
int dist = 0;
//cout << *ptr << endl;
while(ptr != brks.begin() && neg_amount*X[*ptr] <= 1e9+3){
neg_amount *= X[*ptr];
ptr--; dist++;
}
//cout << dist << endl;
//cout << *ptr << " " << *next(ptr)-1 << endl;
//cout << sz << " "<< query(0, 4) << endl;
best = max(best, pos_amount*query(*ptr, *next(ptr)-1));
//cout << best << endl;
F0R(_, dist){
ptr++;
pos_amount*= X[*ptr];
best = max(best, pos_amount*query(*ptr, *next(ptr)-1));
}
//cout << best%MOD << endl;
//cout << totalX << endl;
//cout << neg_amount << " "<< pos_amount << endl;
return (totalX*best/neg_amount).value;
}
int init(int N, int Xi[], int Yi[]) {
sz = 1<<(ll)(log2(N)+1);
seg.rsz(2*sz);
fill(all(seg),0);
/*
ll myHorses = 1;
ll bestScr = 0;*/
brks.insert(N);
brks.insert(0);
X = vl(Xi, Xi+N);
Y = vl(Yi, Yi+N);
F0R(i, N){
totalX *= X[i];
update(i, Y[i]);
if (X[i] > 1) brks.insert(i);
}
/*
F0R(i, N){
myHorses *= X[i];
bestScr = max(bestScr, myHorses*Y[i]);
}*/
return solve();
}
int updateX(int pos, int val) {
totalX /= X[pos];
if (pos==0){
X[pos] = val;
totalX *= X[pos];
return solve();
}
if (val==1){
if (X[pos]!=1) brks.erase(pos);
X[pos] = val;
}
else {
brks.insert(pos);
X[pos] = val;
}
totalX *= X[pos];
return solve();
}
int updateY(int pos, int val) {
Y[pos] = val;
update(pos, Y[pos]);
return solve();
}
/*
int main(){
int n; cin >> n;
int x[n], y[n];
F0R(i, n) cin >> x[i];
F0R(i, n) cin >> y[i];
cout << init(n, x, y) << endl;
//cout << updateX(2, 5) << endl;
//cout << updateY(2, 2) << endl;
}
*/
Compilation message (stderr)
horses.cpp: In constructor 'Modular::Modular(long long int)':
horses.cpp:71:42: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
71 | Modular(long long v = 0) { value = v % MOD; if (value < 0) value += MOD;}
| ~~^~~~~
horses.cpp: In member function 'Modular& Modular::operator*=(const Modular&)':
horses.cpp:76:79: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
76 | Modular& operator*=(Modular const& b) {value = (long long)value * b.value % MOD;return *this;}
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
horses.cpp: In function 'void update(int, ll)':
horses.cpp:103:7: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
103 | i += sz;
| ~~^~~~~
horses.cpp: In function 'll query(int, int)':
horses.cpp:114:7: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
114 | l += sz; r += sz;
| ~~^~~~~
horses.cpp:114:16: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
114 | l += sz; r += sz;
| ~~^~~~~
horses.cpp: In function 'll solve()':
horses.cpp:137:44: warning: conversion from 'll' {aka 'long long int'} to 'double' may change value [-Wconversion]
137 | while(ptr != brks.begin() && neg_amount*X[*ptr] <= 1e9+3){
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:188:17: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
188 | return solve();
| ~~~~~^~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:196:21: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
196 | return solve();
| ~~~~~^~
horses.cpp:209:17: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
209 | return solve();
| ~~~~~^~
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:216:17: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
216 | return solve();
| ~~~~~^~
# | 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... |