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 "horses.h"
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define inside sl<=l&&r<=sr
#define outside r<sl||sr<l
#define orta ((l+r)>>1)
#define INF 1000000009
#define mod 1000000007
#define ppair(x); cerr << "(" << x.first << ", " << x.second << ")\n";
#define bas(x) #x << ": " << x << " "
#define prarr(x, n); cerr << #x << ": "; for(int qsd = 0; qsd < n; qsd++) cerr << x[qsd] << " "; cerr << "\n";
#define prarrv(x); cerr << #x << ": "; for(int qsd = 0; qsd < (int)x.size(); qsd++) cerr << x[qsd] << " "; cerr << "\n";
#define orta ((l+r)>>1)
using namespace std;
typedef long long ll;
double arr[500005];
ll arrmod[500005];
int st[4*500005];
ll stmod[4*500005];
double lazy[4*500005];
int n;
int x[500005];
int y[500005];
ll us(ll a, ll b){
ll ans = 1;
while (b){
if (b & 1){
ans *= a;
ans %= mod;
}
a *= a;
a %= mod;
b >>= 1;
}
return ans;
}
ll inv(ll a){
return us(a, mod-2);
}
void push(int node){
lazy[node*2] += lazy[node];
lazy[node*2+1] += lazy[node];
lazy[node] = 0;
}
int get(int node){
push(node);
return st[node];
}
double getval(int node, int l, int r, int ind){
if (l == r) return lazy[node] + arr[l];
else {
if (ind <= orta){
return getval(node*2, l, orta, ind) + lazy[node];
} else return getval(node*2+1, orta+1, r, ind) + lazy[node];
}
}
void stc(int node, int l, int r){
if (l == r){
lazy[node] = 0;
st[node] = l;
} else {
int m = orta;
stc(node*2, l, m);
stc(node*2+1, m+1, r);
double val1 = getval(1, 0, n-1, st[node*2]);
double val2 = getval(1, 0, n-1, st[node*2+1]);
st[node] = (val1 > val2) ? st[node*2] : st[node*2+1];
lazy[node] = 0;
}
}
int stq(int node, int l, int r, int sl, int sr){
if (inside) return get(node);
else if (outside) return 0;
else {
push(node);
int m = orta;
int i1 = stq(node*2, l, m, sl, sr);
int i2 = stq(node*2+1, m+1, r, sl, sr);
return getval(1, 0, n-1, i1) > getval(1, 0, n-1, i2) ? i1 : i2;
}
}
void stu(int node, int l, int r, int ind, double val){
if (l == r) arr[l] += val;
else {
push(node);
int m = orta;
if (ind <= m) stu(node*2, l, m, ind, val);
else stu(node*2+1, m+1, r, ind, val);
double val1 = getval(1, 0, n-1, st[node*2]);
double val2 = getval(1, 0, n-1, st[node*2+1]);
st[node] = (val1 > val2) ? st[node*2] : st[node*2+1];
}
}
void stul(int node, int l, int r, int sl, int sr, double val){
if (inside) lazy[node] += val;
else if (outside) return;
else {
int m = orta;
push(node);
stul(node*2, l, m, sl, sr, val);
stul(node*2+1, m+1, r, sl, sr, val);
double val1 = getval(1, 0, n-1, st[node*2]);
double val2 = getval(1, 0, n-1, st[node*2+1]);
st[node] = (val1 > val2) ? st[node*2] : st[node*2+1];
}
}
void stcmod(int node, int l, int r){
if (l == r) stmod[node] = arrmod[l];
else {
int m = orta;
stcmod(node*2, l, m);
stcmod(node*2+1, m+1, r);
stmod[node] = 1;
}
}
void stumod(int node, int l, int r, int sl, int sr, ll val){
if (inside) stmod[node] = (stmod[node]*val)%mod;
else if (outside) return;
else {
int m = orta;
stumod(node*2, l, m, sl, sr, val);
stumod(node*2+1, m+1, r, sl, sr, val);
}
}
ll stqmod(int node, int l, int r, int ind){
if (l == r) return stmod[node];
else {
if (ind <= orta) return (stmod[node]*stqmod(node*2, l, orta, ind))%mod;
else return (stmod[node]*stqmod(node*2+1, orta+1, r, ind))%mod;
}
}
int init(int N, int X[], int Y[]) {
n = N;
for (int i = 0; i < n; i++) x[i] = X[i];
for (int i = 0; i < n; i++) y[i] = Y[i];
double sum = 0;
ll summod = 1;
for (int i = 0; i < n; i++){
sum += log2(X[i]);
arr[i] = sum + log2(Y[i]);
summod *= X[i];
summod %= mod;
arrmod[i] = (summod*Y[i])%mod;
}
//prarr(arr, n);
stc(1, 0, n-1);
stcmod(1, 0, n-1);
//for (int i = 0; i < 3; i++) cout << stqmod(1, 0, n-1, i) << " "; cout << endl;
//for (int i = 0; i < 3; i++) cout << getval(1, 0, n-1, i) << " "; cout << endl;
return stqmod(1, 0, n-1, stq(1, 0, n-1, 0, n-1));
}
int updateX(int pos, int val){
stul(1, 0, n-1, pos, n-1, log2(val)-log2(x[pos]));
stumod(1, 0, n-1, pos, n-1, ((ll)val*inv(x[pos]))%mod);
/*for (int i = pos; i < n; i++){
arrmod[i] *= inv(x[pos]);
arrmod[i] %= mod;
arrmod[i] *= (ll)val;
arrmod[i] %= mod;
}*/
x[pos] = val;
return stqmod(1, 0, n-1, stq(1, 0, n-1, 0, n-1));
}
int updateY(int pos, int val) {
//cout << "update " << pos << " -> " << log2(val) - log2(y[pos]) << endl;
stu(1, 0, n-1, pos, log2(val) - log2(y[pos]));
//prarr(arr, 3);
//for (int i = 0; i < 3; i++) cout << getval(1, 0, n-1, i) << " "; cout << endl;
arrmod[pos] *= inv(y[pos]);
arrmod[pos] %= mod;
arrmod[pos] *= val;
arrmod[pos] %= mod;
y[pos] = val;
return stqmod(1, 0, n-1, stq(1, 0, n-1, 0, n-1));
}
Compilation message (stderr)
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:174:15: warning: conversion to 'int' from 'll {aka long long int}' may alter its value [-Wconversion]
return stqmod(1, 0, n-1, stq(1, 0, n-1, 0, n-1));
~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:187:15: warning: conversion to 'int' from 'll {aka long long int}' may alter its value [-Wconversion]
return stqmod(1, 0, n-1, stq(1, 0, n-1, 0, n-1));
~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:201:15: warning: conversion to 'int' from 'll {aka long long int}' may alter its value [-Wconversion]
return stqmod(1, 0, n-1, stq(1, 0, n-1, 0, n-1));
~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |