#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include "horses.h"
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
const int mod = 1e9 + 7;
const int N = 5e5 + 10;
int n, st[N * 4];
int BIT[N];
int power(int a, int n) {
int ans = 1;
for (; n; n >>= 1, a = 1LL * a * a % mod)
if (n & 1) ans = 1LL * ans * a % mod;
return ans;
}
void add(int i, int mul) {
for (++i; i < N; i += i & - i) BIT[i] = 1LL * BIT[i] * mul % mod;
}
int get(int i) {
int ans = 1;
for (++i; i; i -= i & - i) ans = 1LL * ans * BIT[i] % mod;
return ans;
}
void add(int id, int L, int R, int i, int val) {
if (L == R) {
st[id] = val;
return;
}
int mid = L + R >> 1;
if (i <= mid) add(id * 2, L, mid, i, val);
else add(id * 2 + 1, mid + 1, R, i, val);
st[id] = max(st[id * 2], st[id * 2 + 1]);
}
int get(int id, int L, int R, int u, int v) {
if (u > R || L > v) return -1;
if (u <= L && R <= v) return st[id];
int mid = L + R >> 1;
return max(get(id * 2, L, mid, u, v), get(id * 2 + 1, mid + 1, R, u, v));
}
set <int> s;
int x[N], y[N];
int solve() {
int cur = n;
long long mul = 1, p = 1;
int best = -1;
int i = -1;
while (1) {
int last = *(--s.lower_bound(cur));
int val = get(1, 0, n - 1, last, cur - 1);
if (best == -1 || 1.0L * best * p < val) {
best = val;
i = last;
p = 1;
}
p *= x[last];
if (mul > 1e9) break;
mul *= x[last];
cur = last;
if (cur == 0) break;
}
int j = (*(--s.end()) == i ? n : *s.upper_bound(i));
return 1LL * get(i) * get(1, 0, n - 1, i, j - 1) % mod;
}
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];
s.insert(0);
for (int i = 0; i < n; ++i) if (x[i] != 1) s.insert(i);
for (int i = 0; i < n; ++i) BIT[i + 1] = 1;
for (int i = 0; i < n; ++i) add(i, x[i]);
for (int i = 0; i < n; ++i) add(1, 0, n - 1, i, y[i]);
return solve();
}
int updateX(int i, int val) {
if (x[i] != 1 && i) s.erase(i);
add(i, power(x[i], mod - 2));
x[i] = val;
if (x[i] != 1 && i) s.insert(i);
add(i, x[i]);
return solve();
}
int updateY(int i, int val) {
add(1, 0, n - 1, i, val);
y[i] = val;
return solve();
}
//
//int main() {
// freopen("test.inp", "r", stdin);
// freopen("test.out", "w", stdout);
// ios :: sync_with_stdio(0); cin.tie(0);
//
// int n, m;
// cin >> n;
// int x[n], y[n];
// for (int i = 0; i < n; ++i) cin >> x[i];
// for (int i = 0; i < n; ++i) cin >> y[i];
// cin >> m;
// cout << init(n, x, y) << '\n';
// while (m--) {
// int cmd, i, val;
// cin >> cmd >> i >> val;
// if (cmd == 1) cout << updateX(i, val) << '\n';
// else cout << updateY(i, val) << '\n';
// }
// cerr << solve() << '\n';
//}
Compilation message
horses.cpp: In function 'int power(int, int)':
horses.cpp:20:22: warning: declaration of 'n' shadows a global declaration [-Wshadow]
20 | int power(int a, int n) {
| ~~~~^
horses.cpp:17:5: note: shadowed declaration is here
17 | int n, st[N * 4];
| ^
horses.cpp:22:40: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
22 | for (; n; n >>= 1, a = 1LL * a * a % mod)
| ~~~~~~~~~~~~^~~~~
horses.cpp:23:40: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
23 | if (n & 1) ans = 1LL * ans * a % mod;
| ~~~~~~~~~~~~~~^~~~~
horses.cpp: In function 'void add(int, int)':
horses.cpp:28:64: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
28 | for (++i; i < N; i += i & - i) BIT[i] = 1LL * BIT[i] * mul % mod;
| ~~~~~~~~~~~~~~~~~~~^~~~~
horses.cpp: In function 'int get(int)':
horses.cpp:33:57: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
33 | for (++i; i; i -= i & - i) ans = 1LL * ans * BIT[i] % mod;
| ~~~~~~~~~~~~~~~~~~~^~~~~
horses.cpp: In function 'void add(int, int, int, int, int)':
horses.cpp:42:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
42 | int mid = L + R >> 1;
| ~~^~~
horses.cpp: In function 'int get(int, int, int, int, int)':
horses.cpp:51:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
51 | int mid = L + R >> 1;
| ~~^~~
horses.cpp: In function 'int solve()':
horses.cpp:72:13: warning: conversion from 'long long int' to 'double' may change value [-Wconversion]
72 | if (mul > 1e9) break;
| ^~~
horses.cpp:78:54: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
78 | return 1LL * get(i) * get(1, 0, n - 1, i, j - 1) % mod;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 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 |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 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 |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 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 |
460 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
924 ms |
38628 KB |
Output is correct |
2 |
Correct |
381 ms |
38672 KB |
Output is correct |
3 |
Correct |
394 ms |
38592 KB |
Output is correct |
4 |
Correct |
467 ms |
38564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 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 |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
340 KB |
Output is correct |
33 |
Correct |
85 ms |
18252 KB |
Output is correct |
34 |
Correct |
82 ms |
18256 KB |
Output is correct |
35 |
Correct |
229 ms |
48544 KB |
Output is correct |
36 |
Correct |
218 ms |
48532 KB |
Output is correct |
37 |
Correct |
109 ms |
16420 KB |
Output is correct |
38 |
Correct |
148 ms |
29396 KB |
Output is correct |
39 |
Correct |
71 ms |
16196 KB |
Output is correct |
40 |
Correct |
199 ms |
43688 KB |
Output is correct |
41 |
Correct |
88 ms |
16340 KB |
Output is correct |
42 |
Correct |
102 ms |
16292 KB |
Output is correct |
43 |
Correct |
190 ms |
43976 KB |
Output is correct |
44 |
Correct |
205 ms |
44104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 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 |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 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 |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
340 KB |
Output is correct |
33 |
Correct |
894 ms |
42444 KB |
Output is correct |
34 |
Correct |
396 ms |
51256 KB |
Output is correct |
35 |
Correct |
386 ms |
42436 KB |
Output is correct |
36 |
Correct |
478 ms |
46368 KB |
Output is correct |
37 |
Correct |
84 ms |
18252 KB |
Output is correct |
38 |
Correct |
86 ms |
18324 KB |
Output is correct |
39 |
Correct |
231 ms |
48572 KB |
Output is correct |
40 |
Correct |
218 ms |
48588 KB |
Output is correct |
41 |
Correct |
116 ms |
16404 KB |
Output is correct |
42 |
Correct |
149 ms |
29188 KB |
Output is correct |
43 |
Correct |
74 ms |
16252 KB |
Output is correct |
44 |
Correct |
212 ms |
43620 KB |
Output is correct |
45 |
Correct |
89 ms |
16292 KB |
Output is correct |
46 |
Correct |
102 ms |
16384 KB |
Output is correct |
47 |
Correct |
188 ms |
44012 KB |
Output is correct |
48 |
Correct |
188 ms |
43980 KB |
Output is correct |
49 |
Correct |
187 ms |
21264 KB |
Output is correct |
50 |
Correct |
170 ms |
21244 KB |
Output is correct |
51 |
Correct |
445 ms |
50500 KB |
Output is correct |
52 |
Correct |
327 ms |
49972 KB |
Output is correct |
53 |
Correct |
553 ms |
19652 KB |
Output is correct |
54 |
Correct |
398 ms |
33208 KB |
Output is correct |
55 |
Correct |
151 ms |
17256 KB |
Output is correct |
56 |
Correct |
302 ms |
45512 KB |
Output is correct |
57 |
Correct |
323 ms |
17928 KB |
Output is correct |
58 |
Correct |
461 ms |
18484 KB |
Output is correct |
59 |
Correct |
187 ms |
43996 KB |
Output is correct |