#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
int n;
vector<ll> C, P;
set<int> indices;
ll treeProd[4000000],treeMax[4000000];
void updateProd(int l, int r, int idx, int i, ll val) {
if(idx < l || r < idx || r < l) return;
if(l == r) {
treeProd[i] = val;
return;
}
updateProd(l, (l+r)/2, idx, 2*i+1, val);
updateProd((l+r)/2+1, r, idx, 2*i+2, val);
treeProd[i] = treeProd[2*i+1] * treeProd[2*i+2];
treeProd[i] %= mod;
}
ll getProd(int l, int r, int L, int R, int i) {
if(R < l || L > r || r < l) return 1;
if(L <= l && r <= R) return treeProd[i];
return (getProd(l, (l+r)/2, L, R, 2*i+1) * getProd((l+r)/2+1, r, L, R, 2*i+2)) % mod;
}
void updateMax(int l, int r, int idx, int i, ll val) {
if(idx < l || r < idx || r < l) return;
if(l == r) {
treeMax[i] = val;
return;
}
updateMax(l, (l+r)/2, idx, 2*i+1, val);
updateMax((l+r)/2+1, r, idx, 2*i+2, val);
treeMax[i] = max(treeMax[2*i+1], treeMax[2*i+2]);
}
ll getMax(int l, int r, int L, int R, int i) {
if(R < l || L > r || r < l) return 0;
if(L <= l && r <= R) return treeMax[i];
return max(getMax(l, (l+r)/2, L, R, 2*i+1), getMax((l+r)/2+1, r, L, R, 2*i+2));
}
int solve() {
ll prod = 1;
int best = n-1;
ll pbest = 1;
int der = n;
for(auto it = indices.rbegin(); it != indices.rend(); it++) {
int i = *it;
ll p = getMax(0, n-1, i, der-1, 0);
if(prod <= p) {
best = i;
prod = p;
pbest = p;
}
prod *= C[i];
if(prod > 1e9) break;
der = i;
}
prod = getProd(0, n-1, 0, best, 0);
prod *= pbest;
prod %= mod;
return int(prod);
}
int init(int N, int X[], int Y[]) {
n = N;
C.resize(n);
P.resize(n);
for(int i = 0; i < n; ++i) C[i] = X[i];
for(int i = 0; i < n; ++i) P[i] = Y[i];
memset(treeProd, 1, sizeof(P));
memset(treeMax, 0, sizeof(P));
for(int i = 0; i < n; ++i) {
updateProd(0, n - 1, i, 0, C[i]);
updateMax(0, n - 1, i, 0, P[i]);
}
for(int i = 0; i < n; ++i)
if(C[i] > 1 || i == 0)
indices.insert(i);
return solve();
}
int updateX(int pos, int val) {
updateProd(0, n-1, pos, 0, val);
if(indices.find(pos) != indices.end()) indices.erase(pos);
if(pos == 0 || val > 1) indices.insert(pos);
C[pos] = val;
return solve();
}
int updateY(int pos, int val) {
updateMax(0, n-1, pos, 0, val);
P[pos] = val;
return solve();
}
Compilation message
horses.cpp: In function 'int solve()':
horses.cpp:63:13: warning: conversion to 'double' from 'll {aka long long int}' may alter its value [-Wconversion]
if(prod > 1e9) break;
^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 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 |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
1 ms |
512 KB |
Output is correct |
27 |
Correct |
4 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
512 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
512 KB |
Output is correct |
31 |
Correct |
3 ms |
384 KB |
Output is correct |
32 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
875 ms |
52968 KB |
Output is correct |
2 |
Correct |
595 ms |
53368 KB |
Output is correct |
3 |
Correct |
583 ms |
56808 KB |
Output is correct |
4 |
Correct |
604 ms |
59640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 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 |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
0 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
1 ms |
512 KB |
Output is correct |
27 |
Correct |
4 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
512 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
512 KB |
Output is correct |
31 |
Correct |
3 ms |
384 KB |
Output is correct |
32 |
Correct |
4 ms |
384 KB |
Output is correct |
33 |
Correct |
276 ms |
32632 KB |
Output is correct |
34 |
Correct |
261 ms |
32632 KB |
Output is correct |
35 |
Correct |
418 ms |
59512 KB |
Output is correct |
36 |
Correct |
406 ms |
59384 KB |
Output is correct |
37 |
Correct |
336 ms |
30840 KB |
Output is correct |
38 |
Correct |
334 ms |
43536 KB |
Output is correct |
39 |
Correct |
266 ms |
30536 KB |
Output is correct |
40 |
Correct |
404 ms |
58024 KB |
Output is correct |
41 |
Correct |
281 ms |
30584 KB |
Output is correct |
42 |
Correct |
307 ms |
30840 KB |
Output is correct |
43 |
Correct |
386 ms |
58360 KB |
Output is correct |
44 |
Correct |
394 ms |
58360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 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 |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
0 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
1 ms |
512 KB |
Output is correct |
27 |
Correct |
4 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
512 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
512 KB |
Output is correct |
31 |
Correct |
3 ms |
384 KB |
Output is correct |
32 |
Correct |
4 ms |
384 KB |
Output is correct |
33 |
Correct |
878 ms |
56852 KB |
Output is correct |
34 |
Correct |
572 ms |
57016 KB |
Output is correct |
35 |
Correct |
581 ms |
56824 KB |
Output is correct |
36 |
Correct |
637 ms |
59864 KB |
Output is correct |
37 |
Correct |
291 ms |
32632 KB |
Output is correct |
38 |
Correct |
271 ms |
32632 KB |
Output is correct |
39 |
Correct |
409 ms |
56824 KB |
Output is correct |
40 |
Correct |
405 ms |
56824 KB |
Output is correct |
41 |
Correct |
321 ms |
30840 KB |
Output is correct |
42 |
Correct |
313 ms |
43512 KB |
Output is correct |
43 |
Correct |
282 ms |
30584 KB |
Output is correct |
44 |
Correct |
392 ms |
57976 KB |
Output is correct |
45 |
Correct |
280 ms |
30584 KB |
Output is correct |
46 |
Correct |
301 ms |
30672 KB |
Output is correct |
47 |
Correct |
368 ms |
58360 KB |
Output is correct |
48 |
Correct |
377 ms |
58360 KB |
Output is correct |
49 |
Correct |
381 ms |
35704 KB |
Output is correct |
50 |
Correct |
349 ms |
35704 KB |
Output is correct |
51 |
Correct |
588 ms |
57592 KB |
Output is correct |
52 |
Correct |
457 ms |
57464 KB |
Output is correct |
53 |
Correct |
955 ms |
33912 KB |
Output is correct |
54 |
Correct |
501 ms |
47480 KB |
Output is correct |
55 |
Correct |
387 ms |
31736 KB |
Output is correct |
56 |
Correct |
496 ms |
59868 KB |
Output is correct |
57 |
Correct |
625 ms |
32396 KB |
Output is correct |
58 |
Correct |
823 ms |
32820 KB |
Output is correct |
59 |
Correct |
379 ms |
58360 KB |
Output is correct |