# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
380085 | idk321 | Horses (IOI15_horses) | C++11 | 1408 ms | 17232 KiB |
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>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const int N = 500000;
const int R = 710;
int n;
int* x;
int* y;
int best[R];
ll left1[R][R];
ll right1[R][R];
int f1;
ll left2[N];
void spMod(ll& num)
{
if (num >= mod)
{
num %= mod;
num += mod;
}
}
ll getBigBest()
{
int ci = max(0, n - 100);
ll cbest = left2[ci] * y[ci];
cbest %= mod;
ll fac = 1;
for (int i = max(0, n - 100) + 1; i < n; i++)
{
fac *= x[i];
spMod(fac);
if (fac > y[ci] || fac * y[i] > y[ci])
{
fac = 1;
ci = i;
cbest = left2[i] * y[i];
cbest %= mod;
}
}
return cbest;
}
ll getBest()
{
if (!f1) return getBigBest();
ll cbest = left1[0][best[0]] % mod;
cbest *= y[best[0]];
cbest %= mod;
int ci = best[0];
ll fac = 1;
if (ci != min(R - 1, n - 1)) fac *= right1[0][ci + 1];
spMod(fac);
ll facAll = left1[0][min(R - 1, n - 1)];
facAll %= mod;
for (int a = R; a < n; a += R)
{
int i = best[a / R];
fac *= left1[a / R][i % R];
spMod(fac);
facAll *= left1[a / R][i % R];
facAll %= mod;
if (fac * y[i] > y[ci])
{
cbest = facAll * y[i];
cbest %= mod;
ci = i;
fac = 1;
}
if (i != min(n - 1, a + R - 1))
{
fac *= right1[a / R][(i + 1) % R];
spMod(fac);
facAll *= right1[a / R][(i + 1) % R];
facAll %= mod;
}
}
return cbest;
}
void makeBlock(int block)
{
int ci = block * R;
ll fac = 1;
for (int i = block * R + 1; i < min(n, (block + 1) * R); i++)
{
fac *= x[i];
spMod(fac);
if (fac > y[ci] || fac * y[i] > y[ci])
{
ci = i;
fac = 1;
}
}
fac = 1;
for (int i = 0; i < R; i++)
{
if (i + block * R >= n) continue;
fac *= x[i + block * R];
spMod(fac);
left1[block][i] = fac;
if (block == 0)
{
left2[i] = fac;
} else
{
left2[block * R + i] = fac;
left2[block * R + i] *= left2[block * R - 1];
spMod(left2[block * R + i]);
}
}
fac = 1;
for (int i = R - 1; i >= 0; i--)
{
if (i + block * R >= n) continue;
fac *= x[i + block * R];
spMod(fac);
right1[block][i] = fac;
}
best[block] = ci;
}
int init(int N, int X[], int Y[]) {
n = N;
x = X;
y = Y;
f1 = 0;
for (int i = 0; i < n; i++)
{
if (x[i] == 1) f1++;
}
for (int a = 0; a < R && a * R < n; a++)
{
makeBlock(a);
}
ll fac = 1;
for (int i = 0; i < n; i++)
{
fac *= x[i];
spMod(fac);
left2[i] = fac;
}
//cout << best[0] << endl;
return getBest() % mod;
}
int updateX(int pos, int val) {
if (x[pos] == 1) f1--;
x[pos] = val;
if (x[pos] == 1) f1++;
makeBlock(pos / R);
return getBest() % mod;
}
int updateY(int pos, int val) {
y[pos] = val;
makeBlock(pos / R);
return getBest() % mod;
}
Compilation message (stderr)
# | 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... |