#include "horses.h"
#include <limits>
#include <vector>
#include <set>
using namespace std;
using llong = long long;
const llong mod = 1000'000'007;
#define ROOT 1, 0, n - 1
llong Pow(llong a, llong p)
{
if (p == 0) return 1;
if (p % 2) return (a * Pow(a, p - 1)) % mod;
return Pow((a * a) % mod, p / 2);
}
inline llong Inv(llong a) { return Pow(a % mod, mod - 2); }
int n;
vector<int> x, c;
set<int> chng;
llong xmult;
int t[4 * 500'000];
void Set(int v, int vl, int vr, int i, int val)
{
if (vl == vr)
{
t[v] = val;
return;
}
int vm = (vl + vr) / 2;
if (i <= vm) Set(v * 2, vl, vm, i, val);
else Set(v * 2 + 1, vm + 1, vr, i, val);
t[v] = max(t[v * 2], t[v * 2 + 1]);
}
int Max(int v, int vl, int vr, int l, int r)
{
if (l > vr || vl > r)
return numeric_limits<int>::min();
if (l <= vl && vr <= r)
return t[v];
int vm = (vl + vr) / 2;
return max(Max(v * 2, vl, vm, l, r), Max(v * 2 + 1, vm + 1, vr, l, r));
}
struct Rat
{
llong a, b;
};
inline bool operator<(Rat l, Rat r) { return l.a * r.b < r.a * l.b; }
int Solve()
{
Rat mx{0, 1};
llong curxmult = 1;
for (auto it = chng.rbegin(); it != chng.rend() && curxmult < 1000'000'000LL; ++it)
{
int i = *it;
int j = (it == chng.rbegin() ? n : *prev(it));
mx = max(mx, Rat{Max(ROOT, i, j - 1), curxmult});
curxmult *= x[i];
}
llong res = (mx.a * Inv(mx.b)) % mod;
return res = (res * xmult) % mod;
}
int init(int N, int X[], int Y[]) {
n = N;
x.assign(X, X + N);
c.assign(Y, Y + N);
chng.clear();
for (int i = 0; i < n; ++i)
Set(ROOT, i, c[i]);
xmult = 1;
chng.insert(0);
for (int i = 0; i < n; ++i)
if (x[i] > 1)
{
chng.insert(i);
xmult = (xmult * x[i]) % mod;
}
return Solve();
}
int updateX(int pos, int val) {
if (pos && x[pos] > 1 && val == 1)
chng.erase(pos);
if (pos && x[pos] == 1 && val > 1)
chng.insert(pos);
xmult = (xmult * Inv(x[pos])) % mod;
xmult = (xmult * (x[pos] = val)) % mod;
return Solve();
}
int updateY(int pos, int val) {
Set(ROOT, pos, c[pos] = val);
return Solve();
}
Compilation message
horses.cpp: In function 'int Solve()':
horses.cpp:62:16: warning: conversion to 'int' from 'llong {aka long long int}' may alter its value [-Wconversion]
return res = (res * xmult) % mod;
~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
256 KB |
Output is correct |
6 |
Correct |
4 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
256 KB |
Output is correct |
8 |
Correct |
4 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
256 KB |
Output is correct |
10 |
Correct |
4 ms |
256 KB |
Output is correct |
11 |
Correct |
4 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
4 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
256 KB |
Output is correct |
17 |
Correct |
5 ms |
256 KB |
Output is correct |
18 |
Correct |
4 ms |
256 KB |
Output is correct |
19 |
Correct |
4 ms |
256 KB |
Output is correct |
20 |
Correct |
4 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
256 KB |
Output is correct |
7 |
Correct |
4 ms |
256 KB |
Output is correct |
8 |
Correct |
4 ms |
256 KB |
Output is correct |
9 |
Correct |
4 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
4 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
256 KB |
Output is correct |
15 |
Correct |
4 ms |
256 KB |
Output is correct |
16 |
Correct |
4 ms |
256 KB |
Output is correct |
17 |
Correct |
5 ms |
256 KB |
Output is correct |
18 |
Correct |
5 ms |
256 KB |
Output is correct |
19 |
Correct |
5 ms |
256 KB |
Output is correct |
20 |
Correct |
4 ms |
256 KB |
Output is correct |
21 |
Correct |
4 ms |
256 KB |
Output is correct |
22 |
Correct |
4 ms |
256 KB |
Output is correct |
23 |
Correct |
6 ms |
384 KB |
Output is correct |
24 |
Correct |
6 ms |
384 KB |
Output is correct |
25 |
Correct |
6 ms |
384 KB |
Output is correct |
26 |
Correct |
7 ms |
384 KB |
Output is correct |
27 |
Correct |
8 ms |
384 KB |
Output is correct |
28 |
Correct |
6 ms |
384 KB |
Output is correct |
29 |
Correct |
6 ms |
384 KB |
Output is correct |
30 |
Correct |
6 ms |
384 KB |
Output is correct |
31 |
Correct |
7 ms |
384 KB |
Output is correct |
32 |
Correct |
8 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
812 ms |
37160 KB |
Output is correct |
2 |
Correct |
338 ms |
36984 KB |
Output is correct |
3 |
Correct |
370 ms |
36984 KB |
Output is correct |
4 |
Correct |
411 ms |
37344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
4 ms |
256 KB |
Output is correct |
4 |
Correct |
4 ms |
256 KB |
Output is correct |
5 |
Correct |
4 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
368 KB |
Output is correct |
7 |
Correct |
4 ms |
256 KB |
Output is correct |
8 |
Correct |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
4 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
5 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
4 ms |
256 KB |
Output is correct |
14 |
Correct |
4 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
256 KB |
Output is correct |
17 |
Correct |
5 ms |
256 KB |
Output is correct |
18 |
Correct |
4 ms |
256 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
4 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
256 KB |
Output is correct |
23 |
Correct |
6 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
6 ms |
512 KB |
Output is correct |
26 |
Correct |
6 ms |
512 KB |
Output is correct |
27 |
Correct |
8 ms |
384 KB |
Output is correct |
28 |
Correct |
6 ms |
384 KB |
Output is correct |
29 |
Correct |
6 ms |
384 KB |
Output is correct |
30 |
Correct |
6 ms |
384 KB |
Output is correct |
31 |
Correct |
7 ms |
384 KB |
Output is correct |
32 |
Correct |
9 ms |
384 KB |
Output is correct |
33 |
Correct |
118 ms |
16504 KB |
Output is correct |
34 |
Correct |
117 ms |
16376 KB |
Output is correct |
35 |
Correct |
267 ms |
42872 KB |
Output is correct |
36 |
Correct |
249 ms |
42744 KB |
Output is correct |
37 |
Correct |
180 ms |
14584 KB |
Output is correct |
38 |
Correct |
180 ms |
27384 KB |
Output is correct |
39 |
Correct |
112 ms |
14328 KB |
Output is correct |
40 |
Correct |
232 ms |
41848 KB |
Output is correct |
41 |
Correct |
136 ms |
14456 KB |
Output is correct |
42 |
Correct |
163 ms |
14456 KB |
Output is correct |
43 |
Correct |
231 ms |
42208 KB |
Output is correct |
44 |
Correct |
232 ms |
42164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
256 KB |
Output is correct |
2 |
Correct |
4 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
256 KB |
Output is correct |
7 |
Correct |
4 ms |
256 KB |
Output is correct |
8 |
Correct |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
256 KB |
Output is correct |
10 |
Correct |
6 ms |
256 KB |
Output is correct |
11 |
Correct |
4 ms |
256 KB |
Output is correct |
12 |
Correct |
4 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
256 KB |
Output is correct |
16 |
Correct |
4 ms |
256 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
256 KB |
Output is correct |
19 |
Correct |
4 ms |
256 KB |
Output is correct |
20 |
Correct |
5 ms |
256 KB |
Output is correct |
21 |
Correct |
5 ms |
256 KB |
Output is correct |
22 |
Correct |
4 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
6 ms |
384 KB |
Output is correct |
25 |
Correct |
7 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
9 ms |
512 KB |
Output is correct |
28 |
Correct |
6 ms |
384 KB |
Output is correct |
29 |
Correct |
6 ms |
384 KB |
Output is correct |
30 |
Correct |
6 ms |
512 KB |
Output is correct |
31 |
Correct |
9 ms |
384 KB |
Output is correct |
32 |
Correct |
8 ms |
384 KB |
Output is correct |
33 |
Correct |
808 ms |
40568 KB |
Output is correct |
34 |
Correct |
329 ms |
43644 KB |
Output is correct |
35 |
Correct |
368 ms |
40568 KB |
Output is correct |
36 |
Correct |
414 ms |
43896 KB |
Output is correct |
37 |
Correct |
119 ms |
16504 KB |
Output is correct |
38 |
Correct |
116 ms |
16468 KB |
Output is correct |
39 |
Correct |
263 ms |
42872 KB |
Output is correct |
40 |
Correct |
255 ms |
42744 KB |
Output is correct |
41 |
Correct |
179 ms |
14584 KB |
Output is correct |
42 |
Correct |
179 ms |
27384 KB |
Output is correct |
43 |
Correct |
111 ms |
14448 KB |
Output is correct |
44 |
Correct |
234 ms |
41720 KB |
Output is correct |
45 |
Correct |
135 ms |
14456 KB |
Output is correct |
46 |
Correct |
152 ms |
14456 KB |
Output is correct |
47 |
Correct |
230 ms |
42072 KB |
Output is correct |
48 |
Correct |
227 ms |
42104 KB |
Output is correct |
49 |
Correct |
249 ms |
19652 KB |
Output is correct |
50 |
Correct |
221 ms |
19448 KB |
Output is correct |
51 |
Correct |
429 ms |
43512 KB |
Output is correct |
52 |
Correct |
323 ms |
43512 KB |
Output is correct |
53 |
Correct |
819 ms |
17912 KB |
Output is correct |
54 |
Correct |
399 ms |
31480 KB |
Output is correct |
55 |
Correct |
216 ms |
15480 KB |
Output is correct |
56 |
Correct |
344 ms |
43512 KB |
Output is correct |
57 |
Correct |
502 ms |
16052 KB |
Output is correct |
58 |
Correct |
703 ms |
16632 KB |
Output is correct |
59 |
Correct |
228 ms |
42104 KB |
Output is correct |