//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include "horses.h"
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const ll INF = 1e9 + 7;
const int MAXN = 1e6 + 5;
ll pr[MAXN], x[MAXN], y[MAXN], tree[MAXN * 2], BIT[MAXN];
int n = 0;
ll qry(int ind) {
ll ret = 1;
ind++;
while (ind > 0) {
ret = (BIT[ind] * ret) % MOD;
ind -= ind & -ind;
}
return ret;
}
void upd(int ind, ll val) {
ind++;
while (ind <= n) {
BIT[ind] = (BIT[ind] * val) % MOD;
ind += ind & -ind;
}
}
ll pw(ll a, ll b) {
ll ret = 1;
while (b > 0) {
if (b & 1)
ret = (ret * a) % MOD, b--;
else
a = (a * a) % MOD, b /= 2ll;
}
return ret;
}
void build(int l, int r, int k) {
if (l == r) {
tree[k] = y[l];
return;
}
int m = (l + r) / 2;
build(l, m, k * 2);
build(m + 1, r, k * 2 + 1);
tree[k] = max(tree[k * 2], tree[k * 2 + 1]);
}
void upd(int l, int r, int k, int a, ll val) {
if (a < l || r < a)
return;
if (a <= l && r <= a) {
tree[k] = val;
return;
}
int m = (l + r) / 2;
upd(l, m, k * 2, a, val);
upd(m + 1, r, k * 2 + 1, a, val);
tree[k] = max(tree[k * 2], tree[k * 2 + 1]);
}
ll qry(int l, int r, int k, int a, int b) {
if (b < l || r < a)
return 0;
if (a <= l && r <= b)
return tree[k];
int m = (l + r) / 2;
return max(qry(l, m, k * 2, a, b), qry(m + 1, r, k * 2 + 1, a, b));
}
set<pii> rng;
ll getAns() {
auto it = rng.begin();
ll cur = 1, y = qry(0, n - 1, 1, it->fi, it->se);
ll prod = qry(n - 1);
ll ans = (prod * y) % MOD;
int cnt = 0;
while (cur < INF && it != rng.end()) {
cnt++;
ll ny = qry(0, n - 1, 1, it->fi, it->se);
if (cur * y < ny) {
ans = (prod * pw(qry(it->fi - 1), MOD - 2) % MOD) * ny % MOD;
cur = 1;
y = ny;
}
cur *= 1ll * (ll) x[it->fi];
it = next(it);
}
return ans;
}
int updateX(int pos, int val) {
pos = n-pos-1;
if (val == 1 && x[pos] != 1) {
auto it = rng.lower_bound({pos, -1});
pii nx = {pos, pos};
auto a = it, b = it;
pii flA = {-1, -1}, flB = {-1, -1};
if (it != rng.begin()) {
a = prev(a);
if (x[a->fi] == 1) {
nx.fi = a->fi;
flA = {a->fi, a->se};
}
}
if (it != (--rng.end())) {
b = next(b);
if (x[b->se] == 1) {
nx.se = b->se;
flB = {b->fi, b->se};
}
}
rng.erase(it);
if (flA.fi != -1)
rng.erase(flA);
if (flB.fi != -1)
rng.erase(flB);
rng.insert(nx);
} else if (x[pos] == 1 && val != 1) {
auto it = rng.lower_bound({pos, -1});
pii nx = {pos, pos};
if (it == rng.end() || it->fi > pos)
it = prev(it);
pii l = {it->fi, pos - 1};
pii r = {pos + 1, it->se};
rng.erase(it);
if (l.fi <= l.se)
rng.insert(l);
if (r.fi <= r.se)
rng.insert(r);
rng.insert(nx);
}
ll up = val;
up = (up * pw(x[pos], MOD - 2)) % MOD;
upd(pos, up);
x[pos] = val;
return (int) getAns();
}
int updateY(int pos, int val) {
pos = n-pos-1;
ll up = val;
up = (up * pw(y[pos], MOD - 2)) % MOD;
upd(0, n - 1, 1, pos, val);
y[pos] = val;
return (int) getAns();
}
int init(int n, int x[], int y[]) {
::n = n;
fill(BIT, BIT + n + 10, 1);
pr[n] = 1;
for (int i = 0; i < n; i++) {
pr[n-i-1] = (pr[n-i] * 1ll * (ll) x[n-i-1]) % MOD;
::y[n-i-1] = y[i];
::x[n-i-1] = x[i];
upd(n-i-1, x[i]);
}
for (int i = 0; i < n; i++) {
int j = i;
for (; j < n && ::x[j] == 1; j++);
if (j != i)
j--;
rng.insert({i, j});
i = j;
}
build(0, n - 1, 1);
return (int) getAns();
}
Compilation message
horses.cpp: In function 'll getAns()':
horses.cpp:86:17: warning: declaration of 'y' shadows a global declaration [-Wshadow]
ll cur = 1, y = qry(0, n - 1, 1, it->fi, it->se);
^
horses.cpp:17:23: note: shadowed declaration is here
ll pr[MAXN], x[MAXN], y[MAXN], tree[MAXN * 2], BIT[MAXN];
^
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:161:33: warning: declaration of 'y' shadows a global declaration [-Wshadow]
int init(int n, int x[], int y[]) {
^
horses.cpp:17:23: note: shadowed declaration is here
ll pr[MAXN], x[MAXN], y[MAXN], tree[MAXN * 2], BIT[MAXN];
^
horses.cpp:161:33: warning: declaration of 'x' shadows a global declaration [-Wshadow]
int init(int n, int x[], int y[]) {
^
horses.cpp:17:14: note: shadowed declaration is here
ll pr[MAXN], x[MAXN], y[MAXN], tree[MAXN * 2], BIT[MAXN];
^
horses.cpp:161:33: warning: declaration of 'n' shadows a global declaration [-Wshadow]
int init(int n, int x[], int y[]) {
^
horses.cpp:18:5: note: shadowed declaration is here
int n = 0;
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
9 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
512 KB |
Output is correct |
24 |
Correct |
3 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
512 KB |
Output is correct |
26 |
Correct |
3 ms |
512 KB |
Output is correct |
27 |
Correct |
10 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
512 KB |
Output is correct |
29 |
Correct |
4 ms |
384 KB |
Output is correct |
30 |
Correct |
3 ms |
512 KB |
Output is correct |
31 |
Correct |
6 ms |
384 KB |
Output is correct |
32 |
Correct |
8 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1276 ms |
52888 KB |
Output is correct |
2 |
Correct |
434 ms |
52740 KB |
Output is correct |
3 |
Correct |
427 ms |
52648 KB |
Output is correct |
4 |
Correct |
489 ms |
52756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
380 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
380 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
256 KB |
Output is correct |
24 |
Correct |
3 ms |
512 KB |
Output is correct |
25 |
Correct |
4 ms |
512 KB |
Output is correct |
26 |
Correct |
4 ms |
512 KB |
Output is correct |
27 |
Correct |
7 ms |
512 KB |
Output is correct |
28 |
Correct |
5 ms |
512 KB |
Output is correct |
29 |
Correct |
3 ms |
384 KB |
Output is correct |
30 |
Correct |
4 ms |
512 KB |
Output is correct |
31 |
Correct |
5 ms |
512 KB |
Output is correct |
32 |
Correct |
10 ms |
512 KB |
Output is correct |
33 |
Correct |
85 ms |
28536 KB |
Output is correct |
34 |
Correct |
86 ms |
28536 KB |
Output is correct |
35 |
Correct |
259 ms |
51776 KB |
Output is correct |
36 |
Correct |
280 ms |
51720 KB |
Output is correct |
37 |
Correct |
181 ms |
28464 KB |
Output is correct |
38 |
Correct |
309 ms |
51704 KB |
Output is correct |
39 |
Correct |
94 ms |
28152 KB |
Output is correct |
40 |
Correct |
273 ms |
51812 KB |
Output is correct |
41 |
Correct |
139 ms |
28308 KB |
Output is correct |
42 |
Correct |
179 ms |
28280 KB |
Output is correct |
43 |
Correct |
248 ms |
51908 KB |
Output is correct |
44 |
Correct |
246 ms |
51708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
3 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
428 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
4 ms |
512 KB |
Output is correct |
24 |
Correct |
3 ms |
384 KB |
Output is correct |
25 |
Correct |
4 ms |
384 KB |
Output is correct |
26 |
Correct |
4 ms |
512 KB |
Output is correct |
27 |
Correct |
7 ms |
428 KB |
Output is correct |
28 |
Correct |
5 ms |
512 KB |
Output is correct |
29 |
Correct |
4 ms |
384 KB |
Output is correct |
30 |
Correct |
4 ms |
512 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
10 ms |
384 KB |
Output is correct |
33 |
Correct |
1267 ms |
52736 KB |
Output is correct |
34 |
Correct |
449 ms |
52600 KB |
Output is correct |
35 |
Correct |
463 ms |
52600 KB |
Output is correct |
36 |
Correct |
469 ms |
52620 KB |
Output is correct |
37 |
Correct |
95 ms |
28508 KB |
Output is correct |
38 |
Correct |
101 ms |
28408 KB |
Output is correct |
39 |
Correct |
305 ms |
51764 KB |
Output is correct |
40 |
Correct |
262 ms |
51680 KB |
Output is correct |
41 |
Correct |
178 ms |
28412 KB |
Output is correct |
42 |
Correct |
292 ms |
51832 KB |
Output is correct |
43 |
Correct |
91 ms |
28152 KB |
Output is correct |
44 |
Correct |
296 ms |
51700 KB |
Output is correct |
45 |
Correct |
115 ms |
28408 KB |
Output is correct |
46 |
Correct |
186 ms |
28440 KB |
Output is correct |
47 |
Correct |
296 ms |
51576 KB |
Output is correct |
48 |
Correct |
276 ms |
51704 KB |
Output is correct |
49 |
Correct |
385 ms |
31216 KB |
Output is correct |
50 |
Correct |
273 ms |
31352 KB |
Output is correct |
51 |
Correct |
529 ms |
52728 KB |
Output is correct |
52 |
Correct |
403 ms |
52716 KB |
Output is correct |
53 |
Correct |
1150 ms |
31172 KB |
Output is correct |
54 |
Correct |
546 ms |
52544 KB |
Output is correct |
55 |
Correct |
231 ms |
28400 KB |
Output is correct |
56 |
Correct |
395 ms |
52680 KB |
Output is correct |
57 |
Correct |
464 ms |
29172 KB |
Output is correct |
58 |
Correct |
1424 ms |
29412 KB |
Output is correct |
59 |
Correct |
249 ms |
51704 KB |
Output is correct |