#include "horses.h"
#include <bits/stdc++.h>
#define int long long
using namespace std;
int MOD = 1000000007;
int INF = 1LL<<60;
//maintain a set of all indices such that X[i]>1
//consider the last 30 such indices
//then using a max segtree calculate the maximum Y[i] in between these indices, and thus compute the max X[i]*Y[i] at that index
//we can compute the prefix products of the X[i] using another segtree, which allows us to make point updates
//and range product queries
int exp(int base, int exponent) {
int t = base;
int p = 1;
for (int i=0; i<32; i++) {
if (exponent&(1LL<<i)) {
p*=t; p%=MOD;
}
t*=t;
t%=MOD;
}
return p;
}
int inv(int base) {
return exp(base,MOD-2);
}
class productSegTree {
public:
const int sz = 1048576;
vector<int> tree;
productSegTree() {
tree=vector<int>(sz,1);
}
//each node carries the product of its children
void update(int v, int l, int r, int tl, int tr, int assignment) {
if (l>tr || r<tl) return;
if (l<=tl && tr<=r) {
tree[v]=assignment;
tree[v]%=MOD;
return;
}
int m = (tl+tr)/2;
update(2*v,l,r,tl,m,assignment);
update(2*v+1,l,r,m+1,tr,assignment);
tree[v] = tree[2*v]*tree[2*v+1];
tree[v]%=MOD;
}
int query(int v, int l, int r, int tl, int tr) {
if (l>tr || r<tl) return 1;
if (l<=tl && tr<=r) {
return tree[v];
}
int m = (tl+tr)/2;
int a = query(2*v,l,r,tl,m);
int b = query(2*v+1,l,r,m+1,tr);
return (a*b)%MOD;
}
void update_wrapper(int index, int mult) {
update(1,index,index,0,sz/2-1,mult);
}
int query_wrapper(int index) {
//0...index
return query(1,0,index,0,sz/2-1);
}
};
class maxSegTree {
public:
const int sz = 1048576;
vector<int> tree;
maxSegTree() {
tree=vector<int>(sz,0);
}
//each node carries the max of its children
void update(int v, int l, int r, int tl, int tr, int assignment) {
if (l>tr || r<tl) return;
if (l<=tl && tr<=r) {
tree[v]=assignment;
return;
}
int m = (tl+tr)/2;
update(2*v,l,r,tl,m,assignment);
update(2*v+1,l,r,m+1,tr,assignment);
tree[v] = max(tree[2*v],tree[2*v+1]);
}
int query(int v, int l, int r, int tl, int tr) {
if (l>tr || r<tl) return -INF;
if (l<=tl && tr<=r) {
return tree[v];
}
int m = (tl+tr)/2;
int a = query(2*v,l,r,tl,m);
int b = query(2*v+1,l,r,m+1,tr);
return max(a,b);
}
void update_wrapper(int index, int assignment) {
update(1,index,index,0,sz/2-1,assignment);
}
int query_wrapper(int l, int r) {
return query(1,l,r,0,sz/2-1);
}
};
set<int> usefulX;
vector<int> xval,yval;
maxSegTree yseg;
productSegTree xseg;
int n;
signed get_answer() {
vector<int> needcheck={0};
auto it = usefulX.end();
for (int i=0; i<32; i++) {
if (it==usefulX.begin()) break;
it--;
int r = *it;
needcheck.push_back(r);
}
needcheck.push_back(n);
sort(needcheck.begin(), needcheck.end());
int answer = -1;
int suffprod = 1;
int bestyval = 0;
int bestsuffprod = 1;
//want maximum value of maxyval / suffprod
int k = needcheck.size();
for (int i=k-2; i>=0; i--) {
if (suffprod>1073741824) break;
int maxyval = yseg.query_wrapper(needcheck[i],needcheck[i+1]-1);
if (maxyval*bestsuffprod>bestyval*suffprod) {
//cout << "changed " << i << " " << maxyval <<" " << bestsuffprod <<" " << bestyval <<" " << suffprod << endl;
bestsuffprod = suffprod;
bestyval = maxyval;
answer = maxyval*xseg.query_wrapper(needcheck[i]);
answer%=MOD;
}
//cout << "trying " << i <<" " << needcheck[i] <<" " << needcheck[i+1] <<" " << maxyval <<" " << suffprod << " " << maxyval*xseg.query_wrapper(needcheck[i]) << endl;
suffprod*=xval[needcheck[i]];
}
return (signed)answer;
}
signed init(signed N, signed X[], signed Y[]) {
n=N;
xval=yval=vector<int>(N,0);
for (int i=0; i<N; i++) {
xval[i] = X[i]; yval[i] = Y[i];
if (xval[i]!=1) usefulX.insert(i);
}
for (int i=0; i<N; i++) {
xseg.update_wrapper(i,X[i]);
yseg.update_wrapper(i,Y[i]);
}
return get_answer();
}
signed updateX(signed pos, signed val) {
if (val==1 && xval[pos]!=1) {
usefulX.erase(pos);
}
if (val!=1 && xval[pos]==1) {
usefulX.insert(pos);
}
xval[pos] = val;
xseg.update_wrapper(pos,val);
return get_answer();
}
signed updateY(signed pos, signed val) {
yval[pos] = val;
yseg.update_wrapper(pos,val);
return get_answer();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
16732 KB |
Output is correct |
2 |
Correct |
10 ms |
16824 KB |
Output is correct |
3 |
Correct |
8 ms |
16732 KB |
Output is correct |
4 |
Correct |
6 ms |
16732 KB |
Output is correct |
5 |
Correct |
7 ms |
16728 KB |
Output is correct |
6 |
Correct |
7 ms |
16708 KB |
Output is correct |
7 |
Correct |
9 ms |
16684 KB |
Output is correct |
8 |
Correct |
6 ms |
16872 KB |
Output is correct |
9 |
Correct |
6 ms |
16732 KB |
Output is correct |
10 |
Correct |
7 ms |
16732 KB |
Output is correct |
11 |
Correct |
6 ms |
16856 KB |
Output is correct |
12 |
Correct |
6 ms |
16732 KB |
Output is correct |
13 |
Correct |
6 ms |
16792 KB |
Output is correct |
14 |
Correct |
7 ms |
16732 KB |
Output is correct |
15 |
Correct |
6 ms |
16784 KB |
Output is correct |
16 |
Correct |
8 ms |
16728 KB |
Output is correct |
17 |
Correct |
7 ms |
16732 KB |
Output is correct |
18 |
Correct |
6 ms |
16732 KB |
Output is correct |
19 |
Correct |
9 ms |
16728 KB |
Output is correct |
20 |
Correct |
8 ms |
16732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
16728 KB |
Output is correct |
2 |
Correct |
7 ms |
16732 KB |
Output is correct |
3 |
Correct |
6 ms |
16732 KB |
Output is correct |
4 |
Correct |
6 ms |
16728 KB |
Output is correct |
5 |
Correct |
7 ms |
16824 KB |
Output is correct |
6 |
Correct |
7 ms |
16732 KB |
Output is correct |
7 |
Correct |
7 ms |
16776 KB |
Output is correct |
8 |
Correct |
6 ms |
16728 KB |
Output is correct |
9 |
Correct |
7 ms |
16732 KB |
Output is correct |
10 |
Correct |
6 ms |
16732 KB |
Output is correct |
11 |
Correct |
8 ms |
16732 KB |
Output is correct |
12 |
Correct |
7 ms |
16876 KB |
Output is correct |
13 |
Correct |
6 ms |
16732 KB |
Output is correct |
14 |
Correct |
8 ms |
16732 KB |
Output is correct |
15 |
Correct |
7 ms |
16732 KB |
Output is correct |
16 |
Correct |
7 ms |
16708 KB |
Output is correct |
17 |
Correct |
7 ms |
16728 KB |
Output is correct |
18 |
Correct |
7 ms |
16732 KB |
Output is correct |
19 |
Correct |
7 ms |
16732 KB |
Output is correct |
20 |
Correct |
7 ms |
16712 KB |
Output is correct |
21 |
Correct |
7 ms |
16732 KB |
Output is correct |
22 |
Correct |
7 ms |
16868 KB |
Output is correct |
23 |
Correct |
8 ms |
16732 KB |
Output is correct |
24 |
Correct |
10 ms |
16732 KB |
Output is correct |
25 |
Correct |
9 ms |
16732 KB |
Output is correct |
26 |
Correct |
9 ms |
16936 KB |
Output is correct |
27 |
Correct |
10 ms |
16888 KB |
Output is correct |
28 |
Correct |
10 ms |
16884 KB |
Output is correct |
29 |
Correct |
7 ms |
16732 KB |
Output is correct |
30 |
Correct |
8 ms |
16960 KB |
Output is correct |
31 |
Correct |
9 ms |
16732 KB |
Output is correct |
32 |
Correct |
10 ms |
16728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
537 ms |
56656 KB |
Output is correct |
2 |
Correct |
400 ms |
65620 KB |
Output is correct |
3 |
Correct |
427 ms |
56656 KB |
Output is correct |
4 |
Correct |
414 ms |
60496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
16732 KB |
Output is correct |
2 |
Correct |
6 ms |
16732 KB |
Output is correct |
3 |
Correct |
9 ms |
16632 KB |
Output is correct |
4 |
Correct |
6 ms |
16732 KB |
Output is correct |
5 |
Correct |
7 ms |
16732 KB |
Output is correct |
6 |
Correct |
6 ms |
16984 KB |
Output is correct |
7 |
Correct |
6 ms |
16732 KB |
Output is correct |
8 |
Correct |
7 ms |
16732 KB |
Output is correct |
9 |
Correct |
6 ms |
16668 KB |
Output is correct |
10 |
Correct |
6 ms |
16732 KB |
Output is correct |
11 |
Correct |
6 ms |
16732 KB |
Output is correct |
12 |
Correct |
6 ms |
16796 KB |
Output is correct |
13 |
Correct |
6 ms |
16776 KB |
Output is correct |
14 |
Correct |
9 ms |
16732 KB |
Output is correct |
15 |
Correct |
6 ms |
16728 KB |
Output is correct |
16 |
Correct |
6 ms |
16984 KB |
Output is correct |
17 |
Correct |
8 ms |
16732 KB |
Output is correct |
18 |
Correct |
6 ms |
16732 KB |
Output is correct |
19 |
Correct |
6 ms |
16732 KB |
Output is correct |
20 |
Correct |
6 ms |
16732 KB |
Output is correct |
21 |
Correct |
6 ms |
16732 KB |
Output is correct |
22 |
Correct |
6 ms |
16732 KB |
Output is correct |
23 |
Correct |
13 ms |
16788 KB |
Output is correct |
24 |
Correct |
8 ms |
16732 KB |
Output is correct |
25 |
Correct |
8 ms |
16732 KB |
Output is correct |
26 |
Correct |
8 ms |
16732 KB |
Output is correct |
27 |
Correct |
10 ms |
16732 KB |
Output is correct |
28 |
Correct |
9 ms |
16944 KB |
Output is correct |
29 |
Correct |
7 ms |
16732 KB |
Output is correct |
30 |
Correct |
8 ms |
16732 KB |
Output is correct |
31 |
Correct |
8 ms |
16828 KB |
Output is correct |
32 |
Correct |
10 ms |
16732 KB |
Output is correct |
33 |
Correct |
175 ms |
32540 KB |
Output is correct |
34 |
Correct |
164 ms |
32592 KB |
Output is correct |
35 |
Correct |
282 ms |
62956 KB |
Output is correct |
36 |
Correct |
292 ms |
62804 KB |
Output is correct |
37 |
Correct |
184 ms |
30704 KB |
Output is correct |
38 |
Correct |
219 ms |
43604 KB |
Output is correct |
39 |
Correct |
168 ms |
30660 KB |
Output is correct |
40 |
Correct |
292 ms |
57932 KB |
Output is correct |
41 |
Correct |
170 ms |
30552 KB |
Output is correct |
42 |
Correct |
209 ms |
30692 KB |
Output is correct |
43 |
Correct |
306 ms |
58708 KB |
Output is correct |
44 |
Correct |
284 ms |
58464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
16728 KB |
Output is correct |
2 |
Correct |
8 ms |
16732 KB |
Output is correct |
3 |
Correct |
8 ms |
16884 KB |
Output is correct |
4 |
Correct |
6 ms |
16732 KB |
Output is correct |
5 |
Correct |
7 ms |
16668 KB |
Output is correct |
6 |
Correct |
7 ms |
16732 KB |
Output is correct |
7 |
Correct |
7 ms |
16732 KB |
Output is correct |
8 |
Correct |
7 ms |
16728 KB |
Output is correct |
9 |
Correct |
7 ms |
16660 KB |
Output is correct |
10 |
Correct |
6 ms |
16872 KB |
Output is correct |
11 |
Correct |
6 ms |
16732 KB |
Output is correct |
12 |
Correct |
6 ms |
16732 KB |
Output is correct |
13 |
Correct |
6 ms |
16844 KB |
Output is correct |
14 |
Correct |
7 ms |
16732 KB |
Output is correct |
15 |
Correct |
6 ms |
16676 KB |
Output is correct |
16 |
Correct |
6 ms |
16768 KB |
Output is correct |
17 |
Correct |
6 ms |
16644 KB |
Output is correct |
18 |
Correct |
6 ms |
16656 KB |
Output is correct |
19 |
Correct |
8 ms |
16732 KB |
Output is correct |
20 |
Correct |
6 ms |
16844 KB |
Output is correct |
21 |
Correct |
6 ms |
16732 KB |
Output is correct |
22 |
Correct |
7 ms |
16732 KB |
Output is correct |
23 |
Correct |
8 ms |
16732 KB |
Output is correct |
24 |
Correct |
8 ms |
16732 KB |
Output is correct |
25 |
Correct |
8 ms |
16772 KB |
Output is correct |
26 |
Correct |
8 ms |
16732 KB |
Output is correct |
27 |
Correct |
10 ms |
16728 KB |
Output is correct |
28 |
Correct |
10 ms |
16732 KB |
Output is correct |
29 |
Correct |
8 ms |
16676 KB |
Output is correct |
30 |
Correct |
9 ms |
16732 KB |
Output is correct |
31 |
Correct |
9 ms |
16732 KB |
Output is correct |
32 |
Correct |
10 ms |
16732 KB |
Output is correct |
33 |
Correct |
540 ms |
56816 KB |
Output is correct |
34 |
Correct |
437 ms |
65656 KB |
Output is correct |
35 |
Correct |
386 ms |
56912 KB |
Output is correct |
36 |
Correct |
458 ms |
60500 KB |
Output is correct |
37 |
Correct |
166 ms |
32668 KB |
Output is correct |
38 |
Correct |
165 ms |
32540 KB |
Output is correct |
39 |
Correct |
284 ms |
63056 KB |
Output is correct |
40 |
Correct |
278 ms |
63000 KB |
Output is correct |
41 |
Correct |
180 ms |
30776 KB |
Output is correct |
42 |
Correct |
213 ms |
43556 KB |
Output is correct |
43 |
Correct |
151 ms |
30672 KB |
Output is correct |
44 |
Correct |
264 ms |
57896 KB |
Output is correct |
45 |
Correct |
164 ms |
30544 KB |
Output is correct |
46 |
Correct |
177 ms |
30584 KB |
Output is correct |
47 |
Correct |
289 ms |
58468 KB |
Output is correct |
48 |
Correct |
275 ms |
58444 KB |
Output is correct |
49 |
Correct |
301 ms |
35664 KB |
Output is correct |
50 |
Correct |
273 ms |
35664 KB |
Output is correct |
51 |
Correct |
455 ms |
64852 KB |
Output is correct |
52 |
Correct |
397 ms |
64352 KB |
Output is correct |
53 |
Correct |
513 ms |
33872 KB |
Output is correct |
54 |
Correct |
368 ms |
47404 KB |
Output is correct |
55 |
Correct |
245 ms |
31536 KB |
Output is correct |
56 |
Correct |
407 ms |
59732 KB |
Output is correct |
57 |
Correct |
366 ms |
32364 KB |
Output is correct |
58 |
Correct |
408 ms |
32852 KB |
Output is correct |
59 |
Correct |
260 ms |
58452 KB |
Output is correct |