#include "horses.h"
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5+5;
const int inf = 1e9+5;
const int MOD = 1e9+7;
typedef long long ll;
int expo(ll a, int b) {
ll cur = 1;
while (b) {
if (b & 1) {
cur = (cur*a)%MOD;
}
a = (a*a)%MOD;
b /= 2;
}
return cur;
}
int inv(int a) {
return (a == 1) ? 1 : expo(a, MOD-2);
}
int mx[2*MAXN];
int x[MAXN];
int y[MAXN];
int xinv[MAXN];
int n;
class stree { // range max
public:
stree() {
for (int i = n; i < 2*n; i++) mx[i] = y[i-n];
for (int i = n-1; i > 0; i--) mx[i] = max(mx[2*i], mx[2*i+1]);
}
void upd(int p, int v) {
for (p += n, mx[p] = v; p > 1; p /= 2) {
mx[p/2] = max(mx[p], mx[p^1]);
}
}
int range_mx(int l, int r) {
int ans = 0;
for (l += n, r += n; r > l; r >>= 1, l >>= 1) {
if (l & 1) ans = max(ans, mx[l++]);
if (r & 1) ans = max(ans, mx[--r]);
}
return ans;
}
};
stree *tree;
set<int> big;
ll prod = 1;
int solve() {
ll req = 0; // how big of a value you need to see for it to be worth it
ll ans = 0;
ll cur_mult = prod;
for (auto it = big.rbegin(); it != big.rend() && req < inf; it++) {
int v = *it;
int cur_mx = tree->range_mx(v, n);
if (cur_mx > req) ans = (cur_mult*cur_mx)%MOD;
req = max(req, (ll)cur_mx);
req *= x[v];
cur_mult = (cur_mult*xinv[v])%MOD;
}
// assert(cur_mult == 1);
if (mx[1] > req) return mx[1];
return ans;
}
int init(int N, int X[], int Y[]) {
n = N;
for (int i = 0; i < n; i++) {
x[i] = X[i];
y[i] = Y[i];
xinv[i] = inv(x[i]);
if (x[i] > 1) {
big.insert(i);
}
prod = (prod*x[i])%MOD;
}
tree = new stree();
return solve();
}
int updateX(int pos, int val) {
prod = (prod*xinv[pos])%MOD;
big.erase(pos);
x[pos] = val;
xinv[pos] = inv(x[pos]);
prod = (prod*x[pos])%MOD;
if (x[pos] > 1) big.insert(pos);
return solve();
}
int updateY(int pos, int val) {
y[pos] = val;
tree->upd(pos, val);
return solve();
}
Compilation message
horses.cpp: In function 'int expo(ll, int)':
horses.cpp:22:9: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
22 | return cur;
| ^~~
horses.cpp: In function 'int solve()':
horses.cpp:74:9: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
74 | return ans;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
308 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
312 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
312 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
316 KB |
Output is correct |
20 |
Correct |
1 ms |
340 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 |
320 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
328 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
207 ms |
38692 KB |
Output is correct |
2 |
Correct |
280 ms |
38844 KB |
Output is correct |
3 |
Correct |
248 ms |
38680 KB |
Output is correct |
4 |
Correct |
289 ms |
38808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
312 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
0 ms |
308 KB |
Output is correct |
13 |
Correct |
1 ms |
308 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
312 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
312 KB |
Output is correct |
23 |
Correct |
1 ms |
328 KB |
Output is correct |
24 |
Correct |
1 ms |
320 KB |
Output is correct |
25 |
Correct |
1 ms |
452 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
324 KB |
Output is correct |
33 |
Correct |
27 ms |
18040 KB |
Output is correct |
34 |
Correct |
27 ms |
18056 KB |
Output is correct |
35 |
Correct |
200 ms |
48412 KB |
Output is correct |
36 |
Correct |
196 ms |
48348 KB |
Output is correct |
37 |
Correct |
26 ms |
16200 KB |
Output is correct |
38 |
Correct |
95 ms |
29004 KB |
Output is correct |
39 |
Correct |
20 ms |
16012 KB |
Output is correct |
40 |
Correct |
185 ms |
43460 KB |
Output is correct |
41 |
Correct |
20 ms |
16036 KB |
Output is correct |
42 |
Correct |
22 ms |
16144 KB |
Output is correct |
43 |
Correct |
177 ms |
43828 KB |
Output is correct |
44 |
Correct |
177 ms |
43768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 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 |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 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 |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
312 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 |
1 ms |
320 KB |
Output is correct |
28 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
204 ms |
42204 KB |
Output is correct |
34 |
Correct |
277 ms |
50916 KB |
Output is correct |
35 |
Correct |
268 ms |
42256 KB |
Output is correct |
36 |
Correct |
282 ms |
46104 KB |
Output is correct |
37 |
Correct |
26 ms |
18132 KB |
Output is correct |
38 |
Correct |
26 ms |
18112 KB |
Output is correct |
39 |
Correct |
198 ms |
48332 KB |
Output is correct |
40 |
Correct |
202 ms |
48320 KB |
Output is correct |
41 |
Correct |
26 ms |
16192 KB |
Output is correct |
42 |
Correct |
96 ms |
28976 KB |
Output is correct |
43 |
Correct |
19 ms |
16076 KB |
Output is correct |
44 |
Correct |
178 ms |
43368 KB |
Output is correct |
45 |
Correct |
19 ms |
16056 KB |
Output is correct |
46 |
Correct |
21 ms |
16136 KB |
Output is correct |
47 |
Correct |
179 ms |
43796 KB |
Output is correct |
48 |
Correct |
181 ms |
43828 KB |
Output is correct |
49 |
Correct |
63 ms |
21092 KB |
Output is correct |
50 |
Correct |
60 ms |
21056 KB |
Output is correct |
51 |
Correct |
234 ms |
50268 KB |
Output is correct |
52 |
Correct |
219 ms |
49736 KB |
Output is correct |
53 |
Correct |
107 ms |
19384 KB |
Output is correct |
54 |
Correct |
140 ms |
32948 KB |
Output is correct |
55 |
Correct |
48 ms |
17148 KB |
Output is correct |
56 |
Correct |
214 ms |
45232 KB |
Output is correct |
57 |
Correct |
53 ms |
17720 KB |
Output is correct |
58 |
Correct |
78 ms |
18188 KB |
Output is correct |
59 |
Correct |
186 ms |
43780 KB |
Output is correct |