#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;
template<class X, class Y>
inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int MAXN = 500005;
const int modl = 1e9+7;
ll fen[MAXN];
int dp[12][MAXN], X[MAXN], Y[MAXN], nArr;
#ifdef Nhoksocqt1
int init(int _N, int _X[], int _Y[]);
int updateX(int pos, int val);
int updateY(int pos, int val);
int _X[MAXN], _Y[MAXN];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
freopen("HORSES.inp", "r", stdin);
freopen("HORSES.out", "w", stdout);
int _N;
cin >> _N;
for (int i = 0; i < _N; ++i) {
cin >> _X[i];
//_X[i] = Random(1, 10); cout << _X[i] << " \n"[i + 1 == _N];
}
for (int i = 0; i < _N; ++i) {
cin >> _Y[i];
//_Y[i] = Random(1, 10); cout << _Y[i] << " \n"[i + 1 == _N];
}
cout << init(_N, _X, _Y) << '\n';
int Q;
cin >> Q;
while(Q--) {
int type, pos, val;
cin >> type >> pos >> val;
type = Random(0, 1), pos = Random(0, _N - 1), val = Random(1, (type == 1 ? 1 : 1e9)); cout << type << ' ' << pos << ' ' << val << '\n';
if(type == 0) {
cout << updateX(pos, val) << '\n';
} else {
cout << updateY(pos, val) << '\n';
}
}
return 0;
}
#endif // Nhoksocqt1
ll powermod(ll a, int exponent) {
ll res(1);
while(exponent > 0) {
if(exponent & 1)
res = res * a % modl;
a = a * a % modl;
exponent >>= 1;
}
return res;
}
void modify(int i, int v) {
for (; i > 0; i -= i & -i)
fen[i] = fen[i] * v % modl;
}
int get(int i) {
ll res(1);
for (; i <= nArr; i += i & -i)
res = res * fen[i] % modl;
return res;
}
int getx(int i) {
ll res(1);
for (; i <= nArr; i += i & -i)
res = min(ll(1e9)+1, res * fen[i]);
return res;
}
int query(int u, int v) {
return 1LL * get(u) * powermod(get(v + 1), modl - 2) % modl;
}
int segx[4 * MAXN], segy[4 * MAXN];
void build(int seg[], int id, int l, int r, int x[]) {
if(l == r) {
seg[id] = x[l];
return;
}
int mid = (l + r) >> 1;
build(seg, id << 1, l, mid, x);
build(seg, id << 1 | 1, mid + 1, r, x);
seg[id] = max(seg[id << 1], seg[id << 1 | 1]);
}
void update(int seg[], int pos, int val) {
int id(1), l(1), r(nArr);
while(l != r) {
int mid = (l + r) >> 1;
if(pos <= mid) {
id = id << 1;
r = mid;
} else {
id = id << 1 | 1;
l = mid + 1;
}
}
seg[id] = val;
while(id > 1) {
id >>= 1;
seg[id] = max(seg[id << 1], seg[id << 1 | 1]);
}
}
int query(int seg[], int id, int l, int r, int u, int v) {
if(u <= l && r <= v)
return seg[id];
int mid = (l + r) >> 1, res(0);
if(mid >= u)
res = max(res, query(seg, id << 1, l, mid, u, v));
if(mid < v)
res = max(res, query(seg, id << 1 | 1, mid + 1, r, u, v));
return res;
}
int findFirst(int seg[], int id, int l, int r, int pos, int k) {
if(seg[id] < k || pos > r)
return nArr + 1;
if(pos <= l) {
while(l != r) {
int mid = (l + r) >> 1;
if(seg[id << 1] < k) {
id = id << 1 | 1;
l = mid + 1;
} else {
id = id << 1;
r = mid;
}
}
return l;
}
int mid = (l + r) >> 1;
return min(findFirst(seg, id << 1, l, mid, pos, k), findFirst(seg, id << 1 | 1, mid + 1, r, pos, k));
}
int calcDP(void) {
int MAX_HORSE(1);
for (int i = 1; i <= nArr; ++i)
MAX_HORSE *= X[i];
for (int i = 0; i <= nArr; ++i) {
for (int k = 0; k <= MAX_HORSE; ++k)
dp[i][k] = -1;
}
dp[0][1] = 0;
for (int i = 0; i < nArr; ++i) {
for (int k = 0; k <= MAX_HORSE; ++k) {
if(dp[i][k] < 0)
continue;
maximize(dp[i + 1][k * X[i + 1]], dp[i][k]);
}
for (int k = MAX_HORSE; k > 0; --k) {
if(dp[i + 1][k] < 0)
continue;
maximize(dp[i + 1][k - 1], dp[i + 1][k] + Y[i + 1]);
}
}
return dp[nArr][0];
}
int calc(void) {
int l(1), r(nArr), opt(1);
while(l <= r) {
int mid = (l + r) >> 1;
if(getx(mid) > 1e9) {
opt = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
ll prodNow(1);
for (int j = opt + 1; j <= nArr; ++j) {
prodNow *= X[j];
assert(prodNow <= 1e9);
}
ll res = query(1, opt), ans(Y[opt]), prod(1);
while(opt <= nArr) {
int nxt = opt + 1; //findFirst(segx, 1, 1, nArr, opt + 1, 2);
ans = max(ans, prod * query(segy, 1, 1, nArr, opt, nxt - 1));
prod *= X[nxt];
opt = nxt;
}
ans %= modl;
/*if(res * ans % modl != calc2()) {
cout << "WRONG ANSWER ! JURY: " << calc2() << ' ' << " FIND: " << res * ans % modl << '\n';
cout << nArr << '\n';
for (int i = 1; i <= nArr; ++i)
cout << X[i] << " \n"[i == nArr];
for (int i = 1; i <= nArr; ++i)
cout << Y[i] << " \n"[i == nArr];
cout << "0\n";
exit(0);
}*/
return res * ans % modl;
}
int init(int _N, int _X[], int _Y[]) {
nArr = _N;
for (int i = 1; i <= nArr; ++i) {
X[i] = _X[i - 1];
Y[i] = _Y[i - 1];
fen[i] = 1;
}
build(segx, 1, 1, nArr, X);
build(segy, 1, 1, nArr, Y);
for (int i = 1; i <= nArr; ++i)
modify(i, X[i]);
return calc();
}
int updateX(int pos, int val) {
++pos;
modify(pos, powermod(X[pos], modl - 2) * val % modl);
update(segx, pos, val);
X[pos] = val;
return calc();
}
int updateY(int pos, int val) {
Y[++pos] = val;
update(segy, pos, val);
return calc();
}
Compilation message
horses.cpp: In function 'int get(int)':
horses.cpp:101:12: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
101 | return res;
| ^~~
horses.cpp: In function 'int getx(int)':
horses.cpp:109:12: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
109 | return res;
| ^~~
horses.cpp: In function 'int query(int, int)':
horses.cpp:113:58: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
113 | return 1LL * get(u) * powermod(get(v + 1), modl - 2) % modl;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from horses.cpp:2:
horses.cpp: In function 'int calc()':
horses.cpp:232:16: warning: conversion from 'll' {aka 'long long int'} to 'double' may change value [-Wconversion]
232 | assert(prodNow <= 1e9);
| ^~~~~~~
horses.cpp:257:22: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
257 | return res * ans % modl;
| ~~~~~~~~~~^~~~~~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:278:50: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
278 | modify(pos, powermod(X[pos], modl - 2) * val % modl);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
# |
Verdict |
Execution time |
Memory |
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 |
340 KB |
Output is correct |
5 |
Correct |
0 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 |
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 |
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 |
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 |
# |
Verdict |
Execution time |
Memory |
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 |
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 |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
388 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 |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 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 |
Runtime error |
2 ms |
468 KB |
Execution killed with signal 6 |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
355 ms |
21600 KB |
Output is correct |
2 |
Correct |
185 ms |
21728 KB |
Output is correct |
3 |
Correct |
191 ms |
21568 KB |
Output is correct |
4 |
Correct |
196 ms |
21584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 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 |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 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 |
Runtime error |
2 ms |
468 KB |
Execution killed with signal 6 |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
340 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 |
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 |
1 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 |
0 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 |
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 |
Runtime error |
2 ms |
468 KB |
Execution killed with signal 6 |
22 |
Halted |
0 ms |
0 KB |
- |