#include "horses.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
using ll = long long;
const int MAX = 5e5 + 69;
const ll inf = 1e9 + 5;
const int mod = 1e9 + 7;
template<int MOD = mod>
struct Mint
{
int val;
Mint() : val(0) {}
Mint(int64_t _val) : val((int)(_val % MOD)) { if (val < 0) val += MOD; }
Mint& operator+= (const Mint &rhs) { val += rhs.val; if (val >= MOD) val -= MOD; return *this; }
Mint& operator-= (const Mint &rhs) { val -= rhs.val; if (val < 0) val += MOD; return *this; }
Mint& operator*= (const Mint &rhs) { val = (int)(1ll * val * rhs.val % MOD); return *this; }
friend Mint fpow(Mint x, int64_t y)
{
Mint res = 1;
for (; y > 0; y >>= 1, x *= x)
{
if (y & 1)
res *= x;
}
return res;
}
friend Mint inverse(Mint x) { return fpow(x, MOD-2); }
Mint& operator/= (const Mint &rhs) { return *this *= inverse(rhs); }
friend Mint operator+ (Mint a, const Mint &b) { return a += b; }
friend Mint operator- (Mint a, const Mint &b) { return a -= b; }
friend Mint operator- (Mint a) { return 0 - a; }
friend Mint operator* (Mint a, const Mint &b) { return a *= b; }
friend Mint operator/ (Mint a, const Mint &b) { return a /= b; }
friend ostream& operator<< (ostream &os, const Mint &a) { return os << a.val; }
friend bool operator== (const Mint &a, const Mint &b) { return a.val == b.val; }
friend bool operator!= (const Mint &a, const Mint &b) { return a.val != b.val; }
};
struct Segtree1
{
int n;
Mint<> st[2 * MAX];
Segtree1() {}
Segtree1(int n_, int ar[]) : n(n_)
{
for (int i = 0; i < n; ++i)
st[i+n] = ar[i];
for (int i = n-1; i >= 0; --i)
st[i] = st[i << 1] * st[i << 1 | 1];
}
inline void modify(int p, int val)
{
for (st[p += n] = val; p > 1; p >>= 1)
st[p >> 1] = st[p] * st[p^1];
}
inline Mint<> query(int l, int r)
{
Mint<> res = 1;
for (l += n, r += n+1; l < r; l >>= 1, r >>= 1)
{
if (l & 1) res *= st[l++];
if (r & 1) res *= st[--r];
}
return res;
}
} st1;
struct Segtree2
{
int n;
int st[2 * MAX];
Segtree2() {}
Segtree2(int n_, int ar[]) : n(n_)
{
for (int i = 0; i < n; ++i)
st[i+n] = ar[i];
for (int i = n-1; i >= 0; --i)
st[i] = max(st[i << 1], st[i << 1 | 1]);
}
inline void modify(int p, int val)
{
for (st[p += n] = val; p > 1; p >>= 1)
st[p >> 1] = max(st[p], st[p^1]);
}
inline int query(int l, int r)
{
int res = 0;
for (l += n, r += n+1; l < r; l >>= 1, r >>= 1)
{
if (l & 1) res = max(res, st[l++]);
if (r & 1) res = max(res, st[--r]);
}
return res;
}
} st2;
int N, X[MAX], Y[MAX];
set<int, greater<int>> non_one; // position of non-one numbers
int calc()
{
vector<int> idx = { N };
{
ll val = 1;
for (int x : non_one)
{
idx.pb(x);
val *= X[x];
if (val > inf)
break;
}
if (val <= inf && idx.back() != 0)
idx.pb(0);
}
reverse(idx.begin(), idx.end());
ll mx = 0; int best = 0, ybest = 0;
for (int i = 0; i+1 < (int)idx.size(); ++i)
{
int ymax = st2.query(idx[i], idx[i+1] - 1);
ll xval = st1.query(idx[1], idx[i]).val;
// cerr << idx[i] << " " << idx[i+1] << " " << xval << " " << ymax << '\n';
if (ymax * xval > mx)
mx = ymax * xval, best = i, ybest = ymax;
}
// cerr << "best: " << best << '\n';
// cerr << '\n';
return (st1.query(0, idx[best]) * ybest).val;
}
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];
st1 = Segtree1(n, x);
st2 = Segtree2(n, y);
for (int i = 0; i < N; ++i) if (x[i] != 1)
non_one.insert(i);
return calc();
}
int updateX(int pos, int val)
{
X[pos] = val;
st1.modify(pos, val);
if (val == 1) non_one.erase(pos);
else non_one.insert(pos);
return calc();
}
int updateY(int pos, int val)
{
Y[pos] = val;
st2.modify(pos, val);
return calc();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
11988 KB |
Output is correct |
3 |
Correct |
8 ms |
11988 KB |
Output is correct |
4 |
Correct |
8 ms |
11968 KB |
Output is correct |
5 |
Correct |
7 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
11988 KB |
Output is correct |
10 |
Correct |
5 ms |
11988 KB |
Output is correct |
11 |
Correct |
6 ms |
12060 KB |
Output is correct |
12 |
Correct |
6 ms |
11988 KB |
Output is correct |
13 |
Correct |
6 ms |
12116 KB |
Output is correct |
14 |
Correct |
6 ms |
11988 KB |
Output is correct |
15 |
Correct |
6 ms |
11988 KB |
Output is correct |
16 |
Correct |
6 ms |
11988 KB |
Output is correct |
17 |
Correct |
9 ms |
12000 KB |
Output is correct |
18 |
Correct |
6 ms |
11988 KB |
Output is correct |
19 |
Correct |
6 ms |
11988 KB |
Output is correct |
20 |
Correct |
8 ms |
11952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
11988 KB |
Output is correct |
3 |
Correct |
8 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
11988 KB |
Output is correct |
10 |
Correct |
6 ms |
11996 KB |
Output is correct |
11 |
Correct |
6 ms |
12024 KB |
Output is correct |
12 |
Correct |
6 ms |
11988 KB |
Output is correct |
13 |
Correct |
6 ms |
11988 KB |
Output is correct |
14 |
Correct |
6 ms |
11988 KB |
Output is correct |
15 |
Correct |
6 ms |
11988 KB |
Output is correct |
16 |
Correct |
7 ms |
11988 KB |
Output is correct |
17 |
Correct |
7 ms |
11988 KB |
Output is correct |
18 |
Correct |
6 ms |
11988 KB |
Output is correct |
19 |
Correct |
6 ms |
11988 KB |
Output is correct |
20 |
Correct |
6 ms |
11988 KB |
Output is correct |
21 |
Correct |
6 ms |
11988 KB |
Output is correct |
22 |
Correct |
6 ms |
11988 KB |
Output is correct |
23 |
Correct |
7 ms |
11976 KB |
Output is correct |
24 |
Correct |
7 ms |
11988 KB |
Output is correct |
25 |
Correct |
7 ms |
12116 KB |
Output is correct |
26 |
Correct |
7 ms |
12116 KB |
Output is correct |
27 |
Correct |
8 ms |
12080 KB |
Output is correct |
28 |
Correct |
7 ms |
12116 KB |
Output is correct |
29 |
Correct |
8 ms |
11988 KB |
Output is correct |
30 |
Correct |
7 ms |
12072 KB |
Output is correct |
31 |
Correct |
8 ms |
11988 KB |
Output is correct |
32 |
Correct |
8 ms |
12052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
228 ms |
46008 KB |
Output is correct |
2 |
Correct |
289 ms |
56852 KB |
Output is correct |
3 |
Correct |
230 ms |
48060 KB |
Output is correct |
4 |
Correct |
314 ms |
51868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
6 ms |
12056 KB |
Output is correct |
4 |
Correct |
7 ms |
11988 KB |
Output is correct |
5 |
Correct |
7 ms |
11956 KB |
Output is correct |
6 |
Correct |
7 ms |
12012 KB |
Output is correct |
7 |
Correct |
8 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
11988 KB |
Output is correct |
9 |
Correct |
8 ms |
11988 KB |
Output is correct |
10 |
Correct |
6 ms |
11988 KB |
Output is correct |
11 |
Correct |
6 ms |
11988 KB |
Output is correct |
12 |
Correct |
6 ms |
11988 KB |
Output is correct |
13 |
Correct |
6 ms |
11940 KB |
Output is correct |
14 |
Correct |
7 ms |
11988 KB |
Output is correct |
15 |
Correct |
6 ms |
11944 KB |
Output is correct |
16 |
Correct |
6 ms |
11988 KB |
Output is correct |
17 |
Correct |
6 ms |
11988 KB |
Output is correct |
18 |
Correct |
6 ms |
11936 KB |
Output is correct |
19 |
Correct |
6 ms |
11988 KB |
Output is correct |
20 |
Correct |
7 ms |
11988 KB |
Output is correct |
21 |
Correct |
7 ms |
11988 KB |
Output is correct |
22 |
Correct |
8 ms |
11956 KB |
Output is correct |
23 |
Correct |
7 ms |
12088 KB |
Output is correct |
24 |
Correct |
8 ms |
12032 KB |
Output is correct |
25 |
Correct |
7 ms |
12016 KB |
Output is correct |
26 |
Correct |
7 ms |
12116 KB |
Output is correct |
27 |
Correct |
10 ms |
12020 KB |
Output is correct |
28 |
Correct |
7 ms |
12116 KB |
Output is correct |
29 |
Correct |
7 ms |
11988 KB |
Output is correct |
30 |
Correct |
7 ms |
12116 KB |
Output is correct |
31 |
Correct |
7 ms |
11988 KB |
Output is correct |
32 |
Correct |
7 ms |
12004 KB |
Output is correct |
33 |
Correct |
41 ms |
20084 KB |
Output is correct |
34 |
Correct |
41 ms |
24004 KB |
Output is correct |
35 |
Correct |
181 ms |
54284 KB |
Output is correct |
36 |
Correct |
176 ms |
54248 KB |
Output is correct |
37 |
Correct |
51 ms |
22108 KB |
Output is correct |
38 |
Correct |
94 ms |
34960 KB |
Output is correct |
39 |
Correct |
31 ms |
21892 KB |
Output is correct |
40 |
Correct |
156 ms |
49344 KB |
Output is correct |
41 |
Correct |
33 ms |
21972 KB |
Output is correct |
42 |
Correct |
42 ms |
21988 KB |
Output is correct |
43 |
Correct |
147 ms |
49848 KB |
Output is correct |
44 |
Correct |
154 ms |
49816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
6 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
7 ms |
12024 KB |
Output is correct |
6 |
Correct |
8 ms |
11992 KB |
Output is correct |
7 |
Correct |
7 ms |
11988 KB |
Output is correct |
8 |
Correct |
7 ms |
11988 KB |
Output is correct |
9 |
Correct |
8 ms |
11988 KB |
Output is correct |
10 |
Correct |
6 ms |
11988 KB |
Output is correct |
11 |
Correct |
6 ms |
12028 KB |
Output is correct |
12 |
Correct |
7 ms |
11984 KB |
Output is correct |
13 |
Correct |
7 ms |
11988 KB |
Output is correct |
14 |
Correct |
6 ms |
11988 KB |
Output is correct |
15 |
Correct |
6 ms |
11988 KB |
Output is correct |
16 |
Correct |
6 ms |
11988 KB |
Output is correct |
17 |
Correct |
6 ms |
11936 KB |
Output is correct |
18 |
Correct |
6 ms |
11988 KB |
Output is correct |
19 |
Correct |
6 ms |
11988 KB |
Output is correct |
20 |
Correct |
6 ms |
11988 KB |
Output is correct |
21 |
Correct |
6 ms |
11988 KB |
Output is correct |
22 |
Correct |
5 ms |
11988 KB |
Output is correct |
23 |
Correct |
7 ms |
11988 KB |
Output is correct |
24 |
Correct |
7 ms |
12096 KB |
Output is correct |
25 |
Correct |
7 ms |
12116 KB |
Output is correct |
26 |
Correct |
7 ms |
12116 KB |
Output is correct |
27 |
Correct |
8 ms |
11988 KB |
Output is correct |
28 |
Correct |
7 ms |
12116 KB |
Output is correct |
29 |
Correct |
7 ms |
12008 KB |
Output is correct |
30 |
Correct |
6 ms |
12116 KB |
Output is correct |
31 |
Correct |
7 ms |
11988 KB |
Output is correct |
32 |
Correct |
7 ms |
12072 KB |
Output is correct |
33 |
Correct |
235 ms |
45892 KB |
Output is correct |
34 |
Correct |
323 ms |
56912 KB |
Output is correct |
35 |
Correct |
239 ms |
48132 KB |
Output is correct |
36 |
Correct |
301 ms |
51964 KB |
Output is correct |
37 |
Correct |
37 ms |
23904 KB |
Output is correct |
38 |
Correct |
39 ms |
24020 KB |
Output is correct |
39 |
Correct |
188 ms |
54324 KB |
Output is correct |
40 |
Correct |
177 ms |
54196 KB |
Output is correct |
41 |
Correct |
55 ms |
22168 KB |
Output is correct |
42 |
Correct |
86 ms |
34948 KB |
Output is correct |
43 |
Correct |
29 ms |
21880 KB |
Output is correct |
44 |
Correct |
165 ms |
49380 KB |
Output is correct |
45 |
Correct |
33 ms |
21976 KB |
Output is correct |
46 |
Correct |
37 ms |
22024 KB |
Output is correct |
47 |
Correct |
153 ms |
49680 KB |
Output is correct |
48 |
Correct |
149 ms |
49796 KB |
Output is correct |
49 |
Correct |
105 ms |
27008 KB |
Output is correct |
50 |
Correct |
104 ms |
26932 KB |
Output is correct |
51 |
Correct |
234 ms |
56144 KB |
Output is correct |
52 |
Correct |
245 ms |
55708 KB |
Output is correct |
53 |
Correct |
219 ms |
25324 KB |
Output is correct |
54 |
Correct |
181 ms |
38884 KB |
Output is correct |
55 |
Correct |
92 ms |
23000 KB |
Output is correct |
56 |
Correct |
213 ms |
51244 KB |
Output is correct |
57 |
Correct |
100 ms |
23588 KB |
Output is correct |
58 |
Correct |
145 ms |
24096 KB |
Output is correct |
59 |
Correct |
190 ms |
49636 KB |
Output is correct |