#include "horses.h"
#include <algorithm>
#include <cassert>
#include <iostream>
#include <set>
using namespace std;
typedef long long ll;
const int MN = 500005;
const ll MOD = 1e9 + 7;
ll binpow(ll x, ll e, const ll mod) {
x %= mod;
ll ret = 1;
for (; e>0; e>>=1) {
if (e&1) ret = (ret * x) % mod;
x = (x * x) % mod;
}
return ret;
}
struct Node {
int lo, hi;
ll mx, pd;
};
int N;
ll A[MN], B[MN];
Node st[MN << 2];
void build() {
int PN = N-1; PN |= PN >> 1; PN |= PN >> 2; PN |= PN >> 4; PN |= PN >> 8; PN |= PN >> 16; ++PN;
for (int i=0; i<PN; ++i) {
st[PN+i].lo = i;
st[PN+i].hi = i+1;
st[PN+i].mx = (i < N ? B[i] : 0);
st[PN+i].pd = (i < N ? A[i] : 0);
}
for (int i=PN-1; i>0; --i) {
st[i].lo = st[i << 1].lo;
st[i].hi = st[(i << 1) + 1].hi;
st[i].mx = max(st[i << 1].mx, st[(i << 1) + 1].mx);
st[i].pd = (st[i << 1].pd * st[(i << 1) + 1].pd) % MOD;
}
}
void upd_pd(const int node, const int pos, const ll val) {
if (pos >= st[node].hi || st[node].lo >= pos + 1) return;
if (pos <= st[node].lo && st[node].hi <= pos + 1) {
st[node].pd = val;
return;
}
upd_pd(node << 1, pos, val);
upd_pd((node << 1) + 1, pos, val);
st[node].pd = (st[node << 1].pd * st[(node << 1) + 1].pd) % MOD;
}
void upd_mx(const int node, const int pos, const ll val) {
if (pos >= st[node].hi || st[node].lo >= pos + 1) return;
if (pos <= st[node].lo && st[node].hi <= pos + 1) {
st[node].mx = val;
return;
}
upd_mx(node << 1, pos, val);
upd_mx((node << 1) + 1, pos, val);
st[node].mx = max(st[node << 1].mx, st[(node << 1) + 1].mx);
}
ll query_pd(const int node, const int lo, const int hi) {
if (lo >= st[node].hi || st[node].lo >= hi) return 1;
if (lo <= st[node].lo && st[node].hi <= hi) return st[node].pd;
return (query_pd(node << 1, lo, hi) * query_pd((node << 1) + 1, lo, hi)) % MOD;
}
ll query_mx(const int node, const int lo, const int hi) {
if (lo >= st[node].hi || st[node].lo >= hi) return 0;
if (lo <= st[node].lo && st[node].hi <= hi) return st[node].mx;
return max(query_mx(node << 1, lo, hi), query_mx((node << 1) + 1, lo, hi));
}
set<int, greater<int>> idxs;
int get_ans() {
__int128 suff = 1;
int idx = 0;
for (const int i: idxs) {
suff *= A[i];
idx = i;
if (suff >= MOD) break;
}
if (suff < MOD) idx = 0;
ll pref = query_pd(1, 0, idx);
__int128 best = query_mx(1, 0, N);
for (const int i: idxs) {
best = max(best, suff * query_mx(1, i, N));
suff /= A[i];
if (idx == i) break;
}
assert(suff == 1);
best %= MOD;
return (pref * best) % MOD;
}
int init(int n, int X[], int Y[]) {
N = n;
copy(X, X+N, A);
copy(Y, Y+N, B);
build();
for (int i=0; i<N; ++i) {
if (A[i] > 1) idxs.insert(i);
}
return get_ans();
}
int updateX(int pos, int val) {
upd_pd(1, pos, val);
if (A[pos] == 1 && val > 1) {
idxs.insert(pos);
} else if (A[pos] > 1 && val == 1) {
idxs.erase(pos);
}
A[pos] = val;
return get_ans();
}
int updateY(int pos, int val) {
upd_mx(1, pos, val);
B[pos] = val;
return get_ans();
}
/*
pref/suff of X (point update pduct)
max pref[i] * Y[i] (lazy multiply max segtree)
inverse mod p^2 = pow(x, (p*(p-1)) - 1, p^2)
*/
Compilation message
horses.cpp: In function 'int get_ans()':
horses.cpp:109:26: warning: conversion from '__int128' to 'int' may change value [-Wconversion]
109 | return (pref * best) % MOD;
| ~~~~~~~~~~~~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
2 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
2 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
2 ms |
492 KB |
Output is correct |
24 |
Correct |
2 ms |
492 KB |
Output is correct |
25 |
Correct |
3 ms |
492 KB |
Output is correct |
26 |
Correct |
2 ms |
492 KB |
Output is correct |
27 |
Correct |
6 ms |
492 KB |
Output is correct |
28 |
Correct |
3 ms |
492 KB |
Output is correct |
29 |
Correct |
2 ms |
364 KB |
Output is correct |
30 |
Correct |
2 ms |
492 KB |
Output is correct |
31 |
Correct |
4 ms |
492 KB |
Output is correct |
32 |
Correct |
6 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
943 ms |
61964 KB |
Output is correct |
2 |
Correct |
373 ms |
61420 KB |
Output is correct |
3 |
Correct |
400 ms |
61548 KB |
Output is correct |
4 |
Correct |
436 ms |
61400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
2 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
2 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
2 ms |
492 KB |
Output is correct |
24 |
Correct |
2 ms |
492 KB |
Output is correct |
25 |
Correct |
3 ms |
492 KB |
Output is correct |
26 |
Correct |
2 ms |
492 KB |
Output is correct |
27 |
Correct |
7 ms |
492 KB |
Output is correct |
28 |
Correct |
3 ms |
512 KB |
Output is correct |
29 |
Correct |
2 ms |
364 KB |
Output is correct |
30 |
Correct |
2 ms |
492 KB |
Output is correct |
31 |
Correct |
4 ms |
492 KB |
Output is correct |
32 |
Correct |
7 ms |
492 KB |
Output is correct |
33 |
Correct |
72 ms |
41068 KB |
Output is correct |
34 |
Correct |
66 ms |
40940 KB |
Output is correct |
35 |
Correct |
207 ms |
71148 KB |
Output is correct |
36 |
Correct |
202 ms |
71148 KB |
Output is correct |
37 |
Correct |
156 ms |
39020 KB |
Output is correct |
38 |
Correct |
129 ms |
51820 KB |
Output is correct |
39 |
Correct |
47 ms |
38764 KB |
Output is correct |
40 |
Correct |
181 ms |
66284 KB |
Output is correct |
41 |
Correct |
86 ms |
38892 KB |
Output is correct |
42 |
Correct |
112 ms |
38892 KB |
Output is correct |
43 |
Correct |
171 ms |
66668 KB |
Output is correct |
44 |
Correct |
167 ms |
66796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
2 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
2 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
2 ms |
492 KB |
Output is correct |
24 |
Correct |
2 ms |
528 KB |
Output is correct |
25 |
Correct |
4 ms |
492 KB |
Output is correct |
26 |
Correct |
2 ms |
492 KB |
Output is correct |
27 |
Correct |
6 ms |
492 KB |
Output is correct |
28 |
Correct |
3 ms |
492 KB |
Output is correct |
29 |
Correct |
2 ms |
364 KB |
Output is correct |
30 |
Correct |
2 ms |
492 KB |
Output is correct |
31 |
Correct |
4 ms |
492 KB |
Output is correct |
32 |
Correct |
5 ms |
492 KB |
Output is correct |
33 |
Correct |
966 ms |
65392 KB |
Output is correct |
34 |
Correct |
415 ms |
73836 KB |
Output is correct |
35 |
Correct |
403 ms |
65260 KB |
Output is correct |
36 |
Correct |
471 ms |
68820 KB |
Output is correct |
37 |
Correct |
66 ms |
40812 KB |
Output is correct |
38 |
Correct |
63 ms |
41100 KB |
Output is correct |
39 |
Correct |
207 ms |
71148 KB |
Output is correct |
40 |
Correct |
200 ms |
71388 KB |
Output is correct |
41 |
Correct |
152 ms |
39148 KB |
Output is correct |
42 |
Correct |
129 ms |
51820 KB |
Output is correct |
43 |
Correct |
56 ms |
38764 KB |
Output is correct |
44 |
Correct |
184 ms |
66156 KB |
Output is correct |
45 |
Correct |
86 ms |
38892 KB |
Output is correct |
46 |
Correct |
109 ms |
38892 KB |
Output is correct |
47 |
Correct |
168 ms |
66668 KB |
Output is correct |
48 |
Correct |
169 ms |
66540 KB |
Output is correct |
49 |
Correct |
286 ms |
43884 KB |
Output is correct |
50 |
Correct |
246 ms |
43884 KB |
Output is correct |
51 |
Correct |
425 ms |
73068 KB |
Output is correct |
52 |
Correct |
322 ms |
72684 KB |
Output is correct |
53 |
Correct |
1122 ms |
42252 KB |
Output is correct |
54 |
Correct |
460 ms |
55788 KB |
Output is correct |
55 |
Correct |
162 ms |
39916 KB |
Output is correct |
56 |
Correct |
340 ms |
68076 KB |
Output is correct |
57 |
Correct |
555 ms |
40628 KB |
Output is correct |
58 |
Correct |
784 ms |
40940 KB |
Output is correct |
59 |
Correct |
169 ms |
66668 KB |
Output is correct |