#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include "horses.h"
using namespace std;
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define pb push_back
#define ll long long
#define ld long double
#define ui uint64_t
#define ar array
#define us unordered_set
#define cont(set, element) ((set).find(element) != (set).end())
/********* DEBUG *********/
template <typename T>
void outvec(const vector<T>& Z){
for (const T& x : Z)
cout << x << ' ';
cout << "\n";
}
void printVariable(const any& var) {
if (!var.has_value()) {
cout << "null";
return;
}
if (var.type() == typeid(int)) {
cout << any_cast<int>(var);
} else if (var.type() == typeid(double)) {
cout << any_cast<double>(var);
} else if (var.type() == typeid(float)) {
cout << any_cast<float>(var);
} else if (var.type() == typeid(char)) {
cout << any_cast<char>(var);
} else if (var.type() == typeid(bool)) {
cout << (any_cast<bool>(var) ? "true" : "false");
} else if (var.type() == typeid(string)) {
cout << any_cast<string>(var);
} else if (var.type() == typeid(const char*)) {
cout << any_cast<const char*>(var);
} else if (var.type() == typeid(long long)) {
cout << any_cast<long long>(var);
} else if (var.type() == typeid(long double)) {
cout << any_cast<long double>(var);
} else {
cout << "[unknown type]";
}
}
template<typename... Args>
void outval(Args... args) {
vector<any> variables = {args...};
for (size_t i = 0; i < variables.size(); ++i) {
printVariable(variables[i]);
if (i != variables.size() - 1) {
cout << " ";
}
}
cout << "\n";
}
#define nl "\n"
#define sp << " " <<
/********* DEBUG *********/
const int MOD = 1000000007;
const ll MOD2 = 998244353;
const ll MOD3 = 1000000000;
const ll needMOD = 987654321;
const ll mxN = 500005;
const ll inf = 1e18;
/* Idea
Segtree, point update, for each node keep track of C[i], cumuluative product
of X[i], and highest Y[i] * C[i]. Use logarithms to change multiplication
into addition, but keep track of original values.
*/
struct info {
ld realProd, realHi;
ll modProd, modHi;
};
vector<ll> x(mxN),y(mxN);
vector<info> tree(mxN);
ll n;
info merge(info a, info b){
info temp;
temp.realProd = a.realProd + b.realProd;
temp.modProd = (a.modProd * b.modProd) % MOD;
if (a.realHi > a.realProd + b.realHi){
// take left
temp.realHi = a.realHi;
temp.modHi = a.modHi;
}
else{
// take right
temp.realHi = a.realProd + b.realHi;
temp.modHi = (a.modProd*b.modHi)%MOD;
}
return temp;
}
void build(ll node, ll start, ll end){
if (start == end){
info temp;
temp.realProd = log10(x[start]);
temp.realHi = log10(y[start]) + temp.realProd;
temp.modProd = x[start] % MOD;
temp.modHi = (x[start] * y[start]) % MOD;
//outval("build,idx,realProd,realHi,modProd,modHi:",start,temp.realProd,temp.realHi,temp.modProd,temp.modHi);
tree[node] = temp;
return;
}
ll m = (start+end)/2;
build(2 * node + 1, start, m);
build(2 * node + 2, m + 1, end);
tree[node] = merge(tree[2 * node + 1], tree[2 * node + 2]);
}
void upd(ll idx, ll node, ll start, ll end){
if (start == end){
info temp;
temp.realProd = log10(x[idx]);
temp.realHi = log10(y[idx]) + temp.realProd;
temp.modProd = x[idx] % MOD;
temp.modHi = (x[idx] * y[idx]) % MOD;
//outval("upd:idx,realp,realh,modp,modh:",idx,temp.realProd,temp.realHi,temp.modProd,temp.modHi);
//outval("y[idx], x[idx]:",y[idx],x[idx]);
tree[node] = temp;
return;
}
ll m = (start+end)/2;
if (m < idx){
upd(idx, 2 * node + 1, start, m);
}
else{
upd(idx, 2 * node + 2, m + 1, end);
}
tree[node] = merge(tree[2*node+1], tree[2*node+2]);
//outval("idx,realp,realh,modp,modh:",idx,tree[node*2+1].realProd,tree[node*2+1].realHi,tree[node*2+1].modProd,tree[node*2+1].modHi);
}
// int anginit(int N, vector<int> X, vector<int> Y){
// tree.resize(4*N, {0,0,0,0});
// n=N;
// for (int i = 0; i < N; i++){
// x[i] = X[i];
// y[i] = Y[i];
// }
// build(0, 0, N-1);
// return tree[0].modHi;
// }
int init(int N, int X[], int Y[]){
tree.resize(4*N, {0,0,0,0});
n=N;
for (int i = 0; i < N; i++){
x[i] = X[i];
y[i] = Y[i];
}
build(0, 0, N-1);
return tree[0].modHi;
}
int updateX(int pos, int val){
x[pos]=val;
upd(pos,0,0,n-1);
return tree[0].modHi;
}
int updateY(int pos, int val){
y[pos]=val;
upd(pos,0,0,n-1);
return tree[0].modHi;
}
# | 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... |