# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
929202 |
2024-02-17T22:49:19 Z |
OAleksa |
Horses (IOI15_horses) |
C++14 |
|
0 ms |
0 KB |
#include "horses.h"
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;
const int inf = 2e9 + 69;
const int N = 5e5 + 69;
int n, a[N], b[N], c[N], p[4 * N], g[4 * N];
set<int> st;
int mul(int a, int b) {
long long r = 1ll * a * b;
if (r >= mod)
r %= mod;
return r;
}
void modify(int v, int tl, int tr, int pos, int x) {
if (tl == tr)
p[v] = g[v] = x;
else {
int mid = (tl + tr) / 2;
if (pos <= mid)
modify(v * 2, tl, mid, pos, x);
else
modify(v * 2 + 1, mid + 1, tr, pos, x);
if (p[v * 2] == -1 || p[v * 2 + 1] == -1 || p[v * 2] > inf / p[v * 2 + 1])
p[v] = -1;
else
p[v] = p[v * 2] * p[v * 2 + 1];
g[v] = mul(g[v * 2], g[v * 2 + 1]);
}
}
int get(int v, int tl, int tr, int l, int r) {
if (tl > r || tr < l)
return 1;
else if (tl >= l && tr <= r)
return p[v];
else {
int mid = (tl + tr) / 2;
auto l1 = get(v * 2, tl, mid, l, r);
auto r1 = get(v * 2 + 1, mid + 1, tr, l, r);
if (l1 == -1 || r1 == -1 || l1 > inf / r1)
return -1;
return l1 * r1;
}
}
int Get(int v, int tl, int tr, int l, int r) {
if (tl > r || tr < l)
return 1;
else if (tl >= l && tr <= r)
return g[v];
else {
int mid = (tl + tr) / 2;
return mul(Get(v * 2, tl, mid, l, r), Get(v * 2 + 1, mid + 1, tr, l, r));
}
}
int Better(int i, int j) {
if (i > j)
swap(i, j);
int x = get(1, 1, n, i + 1, j);
if (x < 0 || x > inf / b[j])
return j;
return (b[i] > x * b[j] ? i : j);
}
int Solve() {
long long ans = 1, x = a[1];
int bst = 1;
st.insert(1);
for (int i = 2;i <= n;i++) {
if (x > inf / a[i]) {
x = 1;
bst = i;
st.insert(i);
}
else {
x *= a[i];
if (b[i] > b[bst] / x) {
x = 1;
bst = i;
st.insert(i);
}
}
}
for (int i = 1;i <= bst;i++)
ans = mul(ans, a[i]);
return mul(ans, b[bst]);
}
int Result(int i) {
return mul(Get(1, 1, n, 1, i), b[i]);
}
int init(int N, int X[], int Y[]) {
n = N;
fill(p, p + 4 * n + 10, 1);
fill(g, g + 4 * n + 10, 1);
for (int i = 1;i <= n;i++) {
a[i] = X[i - 1], b[i] = Y[i - 1];
modify(1, 1, n, i, a[i]);
}
return Solve();
}
int updateX(int pos, int val) {
pos++;
a[pos] = val;
modify(1, 1, n, pos, a[pos]);
auto u = st.lower_bound(pos);
if (u != st.begin()) {
if (Better(*--u, pos) == pos)
st.insert(pos);
else if (st.count(pos))
st.erase(pos);
}
u = st.upper_bound(pos);
while (u != st.end() && Better(*u, pos) == pos) {
st.erase(u);
u = st.upper_bound(pos);
}
assert(!st.empty());
return Result(*st.rbegin());
}
int updateY(int pos, int val) {
pos++;
b[pos] = val;
auto u = st.lower_bound(pos);
if (u != st.begin()) {
if (Better(*--u, pos) == pos)
st.insert(pos);
else if (st.count(pos))
st.erase(pos);
}
u = st.upper_bound(pos);
while (u != st.end() && Better(*u, pos) == pos) {
st.erase(u);
u = st.upper_bound(pos);
}
assert(!st.empty());
return Result(*st.rbegin());
}
/*
3
2 1 3
3 4 1
1
2 1 2
*/
Compilation message
horses.cpp: In function 'long long int mul(long long int, long long int)':
horses.cpp:10:20: warning: declaration of 'b' shadows a global declaration [-Wshadow]
10 | int mul(int a, int b) {
| ^
horses.cpp:8:14: note: shadowed declaration is here
8 | int n, a[N], b[N], c[N], p[4 * N], g[4 * N];
| ^
horses.cpp:10:13: warning: declaration of 'a' shadows a global declaration [-Wshadow]
10 | int mul(int a, int b) {
| ^
horses.cpp:8:8: note: shadowed declaration is here
8 | int n, a[N], b[N], c[N], p[4 * N], g[4 * N];
| ^
horses.cpp: In function 'long long int init(long long int, long long int*, long long int*)':
horses.cpp:90:14: warning: declaration of 'N' shadows a global declaration [-Wshadow]
90 | int init(int N, int X[], int Y[]) {
| ^
horses.cpp:7:11: note: shadowed declaration is here
7 | const int N = 5e5 + 69;
| ^
/usr/bin/ld: /tmp/ccBPWn3Q.o: in function `main':
grader.c:(.text.startup+0xaa): undefined reference to `init(int, int*, int*)'
/usr/bin/ld: grader.c:(.text.startup+0x113): undefined reference to `updateX(int, int)'
/usr/bin/ld: grader.c:(.text.startup+0x16d): undefined reference to `updateY(int, int)'
collect2: error: ld returned 1 exit status