#include <bits/stdc++.h>
using namespace std;
//#include "horses.h"
#define ll long long
#define MOD 1000000007
set<ll> multiIndices;
int mainX[500007], mainY[500007];
struct node{
ll mx;
ll product;
ll s, e, m;
node *l, *r;
node(int x, int y, bool readFromX){
s = x, e = y; m = (s+e)/2;
if (s != e){
l = new node(x, m, readFromX);
r = new node(m+1, y, readFromX);
}
else if (readFromX){
product = mainX[s];
mx = mainX[s];
return;
}
else {
product = mainY[s];
mx = mainY[s];
return;
}
mx = max(l->mx, r->mx);
product = (l->product * r->product)%MOD;
}
void update(int idx, ll val){
if (s == e){
product = val;
mx = val;
return;
}
else if (idx <= m) l->update(idx, val);
else r->update(idx, val);
mx = max(l->mx, r->mx);
product = (l->product * r->product)%MOD;
}
ll productQuery(int x, int y){
if (x <= s && y >= e){
return product; //entirely contained
}
if (y <= m){
return l->productQuery(x, y);
}
if (x >= m+1){
return r->productQuery(x, y);
}
return (l->productQuery(x, m) * r->productQuery(m+1, y))%MOD;
}
ll maxQuery(int x, int y){
if (x <= s && y >= e){
return mx; //entirely contained
}
if (y <= m){
return l->maxQuery(x, y);
}
if (x >= m+1){
return r->maxQuery(x, y);
}
return max(l->maxQuery(x, m), r->maxQuery(m+1, y));
}
};
int n;
node *repro;
node *sellPrice;
ll calc(){
vector<int> indices;
auto iter = multiIndices.rbegin();
for (int x = 0; x < 32; x++){
if (iter == multiIndices.rend()) break;
indices.push_back(*iter);
iter++;
}
if (indices.size() == 0 || indices.back() != 0) indices.push_back(0);
reverse(indices.begin(), indices.end());
indices.push_back(n);
//cout << "\n";
//for (int i : indices) cout << i << ' ';
//cout << "\n";
ll ansLoc = 0;
ll ans = (sellPrice->maxQuery(0, indices[1] - 1) * repro->productQuery(0, 0))%MOD;
ll multiplier = 1;
for (int x = 1; x < indices.size() - 1; x++){
//cout << repro->productQuery(0, indices[x]) << ' ' << sellPrice->maxQuery(indices[x], indices[x+1] - 1) << "\n\n\n";
multiplier *= repro->productQuery(indices[x], indices[x]);
ll sp = sellPrice->maxQuery(indices[x], indices[x+1] - 1);
if (multiplier >= 1000000000 || ans <= multiplier * sp){
ans = sp; multiplier = 1; ansLoc = x;
}
//ans = max(ans, (repro->productQuery(0, indices[x]) * sellPrice->maxQuery(indices[x], indices[x+1] - 1))%MOD);
//oh this is wrong lmaooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
}
return (repro->productQuery(0, indices[ansLoc]) * sellPrice->maxQuery(indices[ansLoc], indices[ansLoc+1] - 1))%MOD;
}
int updateX(int pos, int val) {
if (mainX[pos] > 1) multiIndices.erase(multiIndices.find(pos));
mainX[pos] = val;
repro->update(pos, val);
if (mainX[pos] > 1) multiIndices.insert(pos);
return calc();
}
int updateY(int pos, int val) {
mainY[pos] = val;
sellPrice->update(pos, val);
return calc();
}
int init(int N, int X[], int Y[]) {
n = N;
for (int x = 0; x < N; x++) { mainX[x] = X[x]; if (X[x] > 1) multiIndices.insert(x);}
for (int x = 0; x < N-1; x++) { mainY[x] = Y[x];}
repro = new node(0, N, 1);
sellPrice = new node(0, N, 0);
return updateY(N-1, Y[N-1]);
}
Compilation message
horses.cpp: In constructor 'node::node(int, int, bool)':
horses.cpp:19:20: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
19 | l = new node(x, m, readFromX);
| ^
horses.cpp:20:18: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
20 | r = new node(m+1, y, readFromX);
| ~^~
horses.cpp: In member function 'long long int node::productQuery(int, int)':
horses.cpp:63:30: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
63 | return (l->productQuery(x, m) * r->productQuery(m+1, y))%MOD;
| ^
horses.cpp:63:52: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
63 | return (l->productQuery(x, m) * r->productQuery(m+1, y))%MOD;
| ~^~
horses.cpp: In member function 'long long int node::maxQuery(int, int)':
horses.cpp:79:29: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
79 | return max(l->maxQuery(x, m), r->maxQuery(m+1, y));
| ^
horses.cpp:79:46: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
79 | return max(l->maxQuery(x, m), r->maxQuery(m+1, y));
| ~^~
horses.cpp: In function 'long long int calc()':
horses.cpp:93:21: warning: conversion from 'long long int' to 'std::vector<int>::value_type' {aka 'int'} may change value [-Wconversion]
93 | indices.push_back(*iter);
| ^~~~~
horses.cpp:107:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for (int x = 1; x < indices.size() - 1; x++){
| ~~^~~~~~~~~~~~~~~~~~~~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:127:13: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
127 | return calc();
| ~~~~^~
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:133:13: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
133 | return calc();
| ~~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
220 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
3 ms |
596 KB |
Output is correct |
24 |
Correct |
3 ms |
596 KB |
Output is correct |
25 |
Correct |
3 ms |
596 KB |
Output is correct |
26 |
Correct |
2 ms |
596 KB |
Output is correct |
27 |
Correct |
3 ms |
596 KB |
Output is correct |
28 |
Correct |
3 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
580 KB |
Output is correct |
30 |
Correct |
3 ms |
596 KB |
Output is correct |
31 |
Correct |
2 ms |
596 KB |
Output is correct |
32 |
Correct |
3 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
527 ms |
157764 KB |
Output is correct |
2 |
Correct |
697 ms |
157840 KB |
Output is correct |
3 |
Correct |
637 ms |
157832 KB |
Output is correct |
4 |
Correct |
688 ms |
157776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
3 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
3 ms |
596 KB |
Output is correct |
26 |
Correct |
3 ms |
596 KB |
Output is correct |
27 |
Correct |
3 ms |
596 KB |
Output is correct |
28 |
Correct |
3 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
3 ms |
596 KB |
Output is correct |
31 |
Correct |
2 ms |
596 KB |
Output is correct |
32 |
Correct |
2 ms |
596 KB |
Output is correct |
33 |
Correct |
147 ms |
137400 KB |
Output is correct |
34 |
Correct |
144 ms |
137420 KB |
Output is correct |
35 |
Correct |
237 ms |
167720 KB |
Output is correct |
36 |
Correct |
241 ms |
167664 KB |
Output is correct |
37 |
Correct |
148 ms |
135624 KB |
Output is correct |
38 |
Correct |
162 ms |
148428 KB |
Output is correct |
39 |
Correct |
102 ms |
135408 KB |
Output is correct |
40 |
Correct |
232 ms |
162764 KB |
Output is correct |
41 |
Correct |
102 ms |
135480 KB |
Output is correct |
42 |
Correct |
111 ms |
135656 KB |
Output is correct |
43 |
Correct |
205 ms |
163136 KB |
Output is correct |
44 |
Correct |
190 ms |
163124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
308 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
308 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
4 ms |
596 KB |
Output is correct |
24 |
Correct |
3 ms |
604 KB |
Output is correct |
25 |
Correct |
3 ms |
596 KB |
Output is correct |
26 |
Correct |
4 ms |
596 KB |
Output is correct |
27 |
Correct |
3 ms |
596 KB |
Output is correct |
28 |
Correct |
3 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
576 KB |
Output is correct |
30 |
Correct |
3 ms |
576 KB |
Output is correct |
31 |
Correct |
2 ms |
596 KB |
Output is correct |
32 |
Correct |
2 ms |
596 KB |
Output is correct |
33 |
Correct |
514 ms |
161656 KB |
Output is correct |
34 |
Correct |
651 ms |
170412 KB |
Output is correct |
35 |
Correct |
691 ms |
161672 KB |
Output is correct |
36 |
Correct |
698 ms |
165412 KB |
Output is correct |
37 |
Correct |
150 ms |
137480 KB |
Output is correct |
38 |
Correct |
130 ms |
137468 KB |
Output is correct |
39 |
Correct |
246 ms |
167812 KB |
Output is correct |
40 |
Correct |
242 ms |
167844 KB |
Output is correct |
41 |
Correct |
169 ms |
135628 KB |
Output is correct |
42 |
Correct |
160 ms |
148432 KB |
Output is correct |
43 |
Correct |
95 ms |
135324 KB |
Output is correct |
44 |
Correct |
226 ms |
162948 KB |
Output is correct |
45 |
Correct |
100 ms |
135500 KB |
Output is correct |
46 |
Correct |
117 ms |
135500 KB |
Output is correct |
47 |
Correct |
199 ms |
163152 KB |
Output is correct |
48 |
Correct |
190 ms |
163124 KB |
Output is correct |
49 |
Correct |
668 ms |
140516 KB |
Output is correct |
50 |
Correct |
549 ms |
140500 KB |
Output is correct |
51 |
Correct |
667 ms |
169756 KB |
Output is correct |
52 |
Correct |
646 ms |
169152 KB |
Output is correct |
53 |
Correct |
687 ms |
138788 KB |
Output is correct |
54 |
Correct |
530 ms |
152312 KB |
Output is correct |
55 |
Correct |
235 ms |
136472 KB |
Output is correct |
56 |
Correct |
621 ms |
164616 KB |
Output is correct |
57 |
Correct |
318 ms |
137100 KB |
Output is correct |
58 |
Correct |
417 ms |
137544 KB |
Output is correct |
59 |
Correct |
212 ms |
163148 KB |
Output is correct |