#include <bits/stdc++.h>
#include "horses.h"
#define fi first
#define se second
#define mp make_pair
#define TASK ""
#define bit(x) (1LL << (x))
#define getbit(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
using namespace std;
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) {
return l + rd() % (r - l + 1);
}
const int N = 5e5 + 5;
const int oo = 1e9;
const long long ooo = 1e18;
const int mod = 1e9 + 7; // 998244353;
const long double pi = acos(-1);
struct segtree {
int n;
vector <int> st;
segtree(int _n = 0){
n = _n;
st.assign((n << 2) + 5, 0);
}
void update(int id, int l, int r, int pos, int val) {
if (l > pos || r < pos)
return;
if (l == r) {
st[id] = val;
return;
}
int mid = (l + r) >> 1;
update(id << 1, l, mid, pos, val);
update(id << 1 | 1, mid + 1, r, pos, val);
st[id] = max(st[id << 1], st[id << 1 | 1]);
}
void update(int pos, int val) {
update(1, 1, n, pos, val);
}
int getmax(int id, int l, int r, int u, int v) {
if (l > v || r < u)
return 0;
if (l >= u && r <= v)
return st[id];
int mid = (l + r) >> 1;
int lnode = getmax(id << 1, l, mid, u, v);
int rnode = getmax(id << 1 | 1, mid + 1, r, u, v);
return max(lnode, rnode);
}
int getmax(int l, int r) {
return getmax(1, 1, n, l, r);
}
};
struct BIT {
int n;
vector <int> bit;
BIT(int _n = 0){
n = _n;
bit.assign(n + 5, 1);
}
void update(int pos, int val) {
for (; pos <= n; pos += pos & -pos)
bit[pos] = (long long) bit[pos] * val % mod;
}
int getmul(int pos) {
int res = 1;
for (; pos; pos -= pos & -pos)
res = (long long) res * bit[pos] % mod;
return res;
}
};
BIT bit;
segtree st;
set <int> pos;
int l[N];
int r[N];
int x[N];
int y[N];
int n;
int bpow(int x, int k) {
int res = 1;
for (; k; k >>= 1, x = (long long) x * x % mod)
if (k & 1)
res = (long long) res * x % mod;
return res;
}
void build() {
st = segtree(n);
bit = BIT(n);
pos.insert(0);
pos.insert(n + 1);
r[0] = n + 1;
l[n + 1] = 0;
for (int i = 1; i <= n; i++) if (x[i] > 1) {
auto it = pos.upper_bound(i);
int v = *it;
int u = *(--it);
l[i] = u;
r[i] = v;
r[u] = i;
l[v] = i;
pos.insert(i);
bit.update(i, x[i]);
}
for (int i = 1; i <= n; i++)
st.update(i, y[i]);
}
int solve() {
vector <int> cand;
long long tot = 1;
for (int i = n + 1; i > 0 && tot < oo; i = l[i]) {
cand.push_back(i);
if (i <= n)
tot *= x[i];
}
if (tot < oo && cand.back() != 1)
cand.push_back(1);
reverse(ALL(cand));
pair <long long, long long> ans = mp(1, 0);
long long cur = 1;
int pos = 0;
for (int it = 0; it + 1 < (int) cand.size(); it++) {
int i = cand[it];
int j = cand[it + 1];
cur *= x[i];
// cerr << cur << " " << i << " " << j << "\n";
int mx = st.getmax(i, j - 1);
if (cur / ans.fi > ans.se / mx) {
pos = it;
ans = mp(cur, mx);
}
}
int ret = bit.getmul(cand[pos]);
ret = (long long) ret * st.getmax(cand[pos], cand[pos + 1] - 1) % mod;
return ret;
}
int init(int N, int X[], int Y[]) {
n = N;
for (int i = 1; i <= n; i++)
x[i] = X[i - 1];
for (int i = 1; i <= n; i++)
y[i] = Y[i - 1];
build();
return solve();
}
int updateX(int i, int val) {
i++;
if (x[i] > 1) {
pos.erase(i);
auto it = pos.upper_bound(i);
int v = *it;
int u = *(--it);
r[u] = v;
l[v] = u;
}
bit.update(i, bpow(x[i], mod - 2));
x[i] = val;
bit.update(i, x[i]);
if (x[i] > 1) {
auto it = pos.upper_bound(i);
int v = *it;
int u = *(--it);
r[u] = i;
l[v] = i;
r[i] = v;
l[i] = u;
pos.insert(i);
}
return solve();
}
int updateY(int pos, int val) {
pos++;
st.update(pos, val);
return solve();
}
//#include "grader.cpp"
Compilation message
horses.cpp: In function 'int rand(int, int)':
horses.cpp:20:11: warning: conversion from 'std::mersenne_twister_engine<long unsigned int, 64, 312, 156, 31, 13043109905998158313, 29, 6148914691236517205, 17, 8202884508482404352, 37, 18444473444759240704, 43, 6364136223846793005>::result_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
20 | return l + rd() % (r - l + 1);
| ~~^~~~~~~~~~~~~~~~~~~~
horses.cpp: In member function 'void BIT::update(int, int)':
horses.cpp:81:42: warning: conversion from 'long long int' to '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} may change value [-Wconversion]
81 | bit[pos] = (long long) bit[pos] * val % mod;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
horses.cpp: In member function 'int BIT::getmul(int)':
horses.cpp:87:37: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
87 | res = (long long) res * bit[pos] % mod;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
horses.cpp: In function 'int bpow(int, int)':
horses.cpp:104:14: warning: declaration of 'x' shadows a global declaration [-Wshadow]
104 | int bpow(int x, int k) {
| ~~~~^
horses.cpp:100:5: note: shadowed declaration is here
100 | int x[N];
| ^
horses.cpp:106:43: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
106 | for (; k; k >>= 1, x = (long long) x * x % mod)
| ~~~~~~~~~~~~~~~~~~^~~~~
horses.cpp:108:30: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
108 | res = (long long) res * x % mod;
| ~~~~~~~~~~~~~~~~~~~~^~~~~
horses.cpp: In function 'int solve()':
horses.cpp:155:6: warning: declaration of 'pos' shadows a global declaration [-Wshadow]
155 | int pos = 0;
| ^~~
horses.cpp:96:11: note: shadowed declaration is here
96 | set <int> pos;
| ^~~
horses.cpp:171:66: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
171 | ret = (long long) ret * st.getmax(cand[pos], cand[pos + 1] - 1) % mod;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:175:14: warning: declaration of 'N' shadows a global declaration [-Wshadow]
175 | int init(int N, int X[], int Y[]) {
| ~~~~^
horses.cpp:22:11: note: shadowed declaration is here
22 | const int N = 5e5 + 5;
| ^
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:221:17: warning: declaration of 'pos' shadows a global declaration [-Wshadow]
221 | int updateY(int pos, int val) {
| ~~~~^~~
horses.cpp:96:11: note: shadowed declaration is here
96 | set <int> pos;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 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 |
308 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
312 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 |
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 |
316 KB |
Output is correct |
18 |
Correct |
1 ms |
312 KB |
Output is correct |
19 |
Correct |
1 ms |
312 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
212 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 |
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 |
308 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
312 KB |
Output is correct |
15 |
Correct |
1 ms |
308 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 |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
312 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
404 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
2 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 |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
3 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
680 ms |
48592 KB |
Output is correct |
2 |
Correct |
504 ms |
48596 KB |
Output is correct |
3 |
Correct |
503 ms |
48556 KB |
Output is correct |
4 |
Correct |
621 ms |
48576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 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 |
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 |
224 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
308 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 |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
308 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
332 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 |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
448 KB |
Output is correct |
26 |
Correct |
2 ms |
468 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 |
340 KB |
Output is correct |
30 |
Correct |
2 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 |
97 ms |
25904 KB |
Output is correct |
34 |
Correct |
106 ms |
25876 KB |
Output is correct |
35 |
Correct |
343 ms |
56204 KB |
Output is correct |
36 |
Correct |
346 ms |
56220 KB |
Output is correct |
37 |
Correct |
137 ms |
22092 KB |
Output is correct |
38 |
Correct |
203 ms |
36852 KB |
Output is correct |
39 |
Correct |
96 ms |
20044 KB |
Output is correct |
40 |
Correct |
340 ms |
51276 KB |
Output is correct |
41 |
Correct |
111 ms |
20184 KB |
Output is correct |
42 |
Correct |
128 ms |
20100 KB |
Output is correct |
43 |
Correct |
306 ms |
51704 KB |
Output is correct |
44 |
Correct |
350 ms |
51708 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 |
1 ms |
340 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 |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
312 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
280 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 |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 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 |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
312 KB |
Output is correct |
20 |
Correct |
1 ms |
308 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
3 ms |
340 KB |
Output is correct |
25 |
Correct |
3 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
4 ms |
320 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
5 ms |
340 KB |
Output is correct |
33 |
Correct |
714 ms |
50240 KB |
Output is correct |
34 |
Correct |
536 ms |
58852 KB |
Output is correct |
35 |
Correct |
518 ms |
50080 KB |
Output is correct |
36 |
Correct |
638 ms |
53852 KB |
Output is correct |
37 |
Correct |
98 ms |
25840 KB |
Output is correct |
38 |
Correct |
112 ms |
25904 KB |
Output is correct |
39 |
Correct |
357 ms |
56272 KB |
Output is correct |
40 |
Correct |
361 ms |
56240 KB |
Output is correct |
41 |
Correct |
132 ms |
22156 KB |
Output is correct |
42 |
Correct |
210 ms |
36844 KB |
Output is correct |
43 |
Correct |
97 ms |
19984 KB |
Output is correct |
44 |
Correct |
325 ms |
51316 KB |
Output is correct |
45 |
Correct |
126 ms |
20228 KB |
Output is correct |
46 |
Correct |
125 ms |
20024 KB |
Output is correct |
47 |
Correct |
298 ms |
51652 KB |
Output is correct |
48 |
Correct |
325 ms |
51616 KB |
Output is correct |
49 |
Correct |
203 ms |
28916 KB |
Output is correct |
50 |
Correct |
183 ms |
28892 KB |
Output is correct |
51 |
Correct |
471 ms |
58152 KB |
Output is correct |
52 |
Correct |
392 ms |
57608 KB |
Output is correct |
53 |
Correct |
588 ms |
25208 KB |
Output is correct |
54 |
Correct |
384 ms |
40728 KB |
Output is correct |
55 |
Correct |
187 ms |
21100 KB |
Output is correct |
56 |
Correct |
426 ms |
53196 KB |
Output is correct |
57 |
Correct |
371 ms |
21824 KB |
Output is correct |
58 |
Correct |
544 ms |
22152 KB |
Output is correct |
59 |
Correct |
292 ms |
51636 KB |
Output is correct |