/*************************************
* author: marvinthang *
* created: 08.03.2023 15:25:43 *
*************************************/
#include "horses.h"
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
template <class T> T invGeneral(T a, T b) {
a %= b;
if (!a) return b == 1 ? 0 : -1;
T x = invGeneral(b, a);
return x == -1 ? -1 : ((1 - 1LL * b * x) / a + b) % b;
}
const int MOD = 1e9 + 7;
const int MAX = 5e5 + 5;
int N, X[MAX], Y[MAX], bit[MAX], st[MAX << 2];
set <int, greater <int>> pos;
void build(int i, int l, int r) {
if (r - l == 1) {
st[i] = Y[l];
return;
}
int m = l + r >> 1;
build(i << 1, l, m);
build(i << 1 | 1, m, r);
st[i] = max(st[i << 1], st[i << 1 | 1]);
}
void update(int i, int l, int r, int p) {
if (r - l == 1) {
st[i] = Y[l];
return;
}
int m = l + r >> 1;
if (p < m) update(i << 1, l, m, p);
else update(i << 1 | 1, m, r, p);
st[i] = max(st[i << 1], st[i << 1 | 1]);
}
int getMax(int i, int l, int r, int u, int v) {
if (l >= v || r <= u) return 0;
if (u <= l && r <= v) return st[i];
int m = l + r >> 1;
return max(getMax(i << 1, l, m, u, v), getMax(i << 1 | 1, m, r, u, v));
}
void update(int i, int v) {
for (++i; i <= N; i += i & -i) bit[i] = 1LL * bit[i] * v % MOD;
}
int getMul(int i) {
int res = 1;
for (++i; i > 0; i &= i - 1) res = 1LL * res * bit[i] % MOD;
return res;
}
int solve(void) {
int res = -1;
long long cb = 1;
int r = N, ma = 0, mb = 0;
for (int l: pos) {
int ca = getMax(1, 0, N, l, r);
if (1LL * ca * mb >= 1LL * ma * cb) {
res = 1LL * getMul(l) * ca % MOD;
ma = ca; mb = cb;
}
cb *= X[l];
if (cb > MOD) break;
r = l;
}
return res;
}
int init(int n, int x[], int y[]) {
N = n;
fill(bit, bit + N + 1, 1);
REP(i, N) {
X[i] = x[i]; Y[i] = y[i];
if (!i || X[i] != 1) {
update(i, X[i]);
pos.insert(i);
}
}
build(1, 0, N);
return solve();
}
int updateX(int i, int val) {
if (X[i] != 1) {
update(i, invGeneral(X[i], MOD));
pos.erase(i);
}
X[i] = val;
if (!i || X[i] != 1) {
update(i, X[i]);
pos.insert(i);
}
return solve();
}
int updateY(int i, int val) {
Y[i] = val;
update(1, 0, N, i);
return solve();
}
Compilation message
horses.cpp: In function 'void build(int, int, int)':
horses.cpp:31:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
31 | int m = l + r >> 1;
| ~~^~~
horses.cpp: In function 'void update(int, int, int, int)':
horses.cpp:42:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
42 | int m = l + r >> 1;
| ~~^~~
horses.cpp: In function 'int getMax(int, int, int, int, int)':
horses.cpp:51:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
51 | int m = l + r >> 1;
| ~~^~~
horses.cpp: In function 'void update(int, int)':
horses.cpp:56:59: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
56 | for (++i; i <= N; i += i & -i) bit[i] = 1LL * bit[i] * v % MOD;
| ~~~~~~~~~~~~~~~~~^~~~~
horses.cpp: In function 'int getMul(int)':
horses.cpp:61:56: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
61 | for (++i; i > 0; i &= i - 1) res = 1LL * res * bit[i] % MOD;
| ~~~~~~~~~~~~~~~~~~~^~~~~
horses.cpp: In function 'int solve()':
horses.cpp:72:31: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
72 | res = 1LL * getMul(l) * ca % MOD;
| ~~~~~~~~~~~~~~~~~~~~~^~~~~
horses.cpp:73:18: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
73 | ma = ca; mb = cb;
| ^~
horses.cpp: In instantiation of 'T invGeneral(T, T) [with T = int]':
horses.cpp:98:33: required from here
horses.cpp:17:20: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
17 | return x == -1 ? -1 : ((1 - 1LL * b * x) / a + b) % b;
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
304 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
308 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
308 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
3 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
308 KB |
Output is correct |
30 |
Correct |
1 ms |
320 KB |
Output is correct |
31 |
Correct |
3 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
578 ms |
41636 KB |
Output is correct |
2 |
Correct |
301 ms |
51304 KB |
Output is correct |
3 |
Correct |
302 ms |
42364 KB |
Output is correct |
4 |
Correct |
407 ms |
46208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
308 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
308 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
308 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
3 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
320 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
340 KB |
Output is correct |
33 |
Correct |
36 ms |
18284 KB |
Output is correct |
34 |
Correct |
34 ms |
18252 KB |
Output is correct |
35 |
Correct |
180 ms |
48612 KB |
Output is correct |
36 |
Correct |
179 ms |
48576 KB |
Output is correct |
37 |
Correct |
79 ms |
16444 KB |
Output is correct |
38 |
Correct |
92 ms |
29260 KB |
Output is correct |
39 |
Correct |
27 ms |
16264 KB |
Output is correct |
40 |
Correct |
159 ms |
43592 KB |
Output is correct |
41 |
Correct |
50 ms |
16324 KB |
Output is correct |
42 |
Correct |
64 ms |
16336 KB |
Output is correct |
43 |
Correct |
153 ms |
43944 KB |
Output is correct |
44 |
Correct |
155 ms |
43980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
308 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
312 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
3 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
316 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
372 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
316 KB |
Output is correct |
33 |
Correct |
568 ms |
42468 KB |
Output is correct |
34 |
Correct |
308 ms |
51352 KB |
Output is correct |
35 |
Correct |
304 ms |
42440 KB |
Output is correct |
36 |
Correct |
391 ms |
46204 KB |
Output is correct |
37 |
Correct |
36 ms |
18232 KB |
Output is correct |
38 |
Correct |
36 ms |
18288 KB |
Output is correct |
39 |
Correct |
183 ms |
48552 KB |
Output is correct |
40 |
Correct |
178 ms |
48564 KB |
Output is correct |
41 |
Correct |
79 ms |
16380 KB |
Output is correct |
42 |
Correct |
101 ms |
29256 KB |
Output is correct |
43 |
Correct |
27 ms |
16208 KB |
Output is correct |
44 |
Correct |
169 ms |
43680 KB |
Output is correct |
45 |
Correct |
51 ms |
16264 KB |
Output is correct |
46 |
Correct |
65 ms |
16300 KB |
Output is correct |
47 |
Correct |
155 ms |
44008 KB |
Output is correct |
48 |
Correct |
159 ms |
44040 KB |
Output is correct |
49 |
Correct |
120 ms |
21236 KB |
Output is correct |
50 |
Correct |
101 ms |
21284 KB |
Output is correct |
51 |
Correct |
307 ms |
50524 KB |
Output is correct |
52 |
Correct |
222 ms |
50104 KB |
Output is correct |
53 |
Correct |
551 ms |
19680 KB |
Output is correct |
54 |
Correct |
241 ms |
33172 KB |
Output is correct |
55 |
Correct |
98 ms |
17272 KB |
Output is correct |
56 |
Correct |
247 ms |
45516 KB |
Output is correct |
57 |
Correct |
367 ms |
17980 KB |
Output is correct |
58 |
Correct |
478 ms |
18456 KB |
Output is correct |
59 |
Correct |
155 ms |
44028 KB |
Output is correct |