#include<bits/stdc++.h>
//#include "horses.h"
using namespace std;
#define ll long long
#define endl "\n"
const int MOD = 1e9 + 7;
const int maxN = 500000;
const int siz = 1048576 / 2;
int N;
int x[maxN];
int y[maxN];
int segTree[siz * 2];
int segTree2[siz * 2];
set<int, greater<int>> s;
void st(int i, int v, int p, int lx, int rx){
if(rx - lx == 1){
segTree[p] = v;
return;
}
int m = (lx + rx) / 2;
if(i < m) st(i, v, 2 * p + 1, lx, m);
else st(i, v, 2 * p + 2, m, rx);
segTree[p] = max(segTree[2 * p + 1], segTree[2 * p + 2]);
}
int mx(int l, int r, int p, int lx, int rx){
if(lx >= l && rx <= r) return segTree[p];
else if(lx >= r || rx <= l) return 0;
int m = (lx + rx) / 2;
int m1 = mx(l, r, 2 * p + 1, lx, m);
int m2 = mx(l, r, 2 * p + 2, m, rx);
return max(m1, m2);
}
void st_x(int i, int v, int p, int lx, int rx){
if(rx - lx == 1){
segTree2[p] = v;
return;
}
int m = (lx + rx) / 2;
if(i < m) st_x(i, v, 2 * p + 1, lx, m);
else st_x(i, v, 2 * p + 2, m, rx);
ll left = segTree2[2 * p + 1];
ll right = segTree2[2 * p + 2];
segTree2[p] = (left * right) % MOD;
}
ll mult(int l, int r, int p, int lx, int rx){
if(lx >= l && rx <= r) return segTree2[p];
else if(lx >= r || rx <= l) return 1;
int m = (lx + rx) / 2;
ll m1 = mult(l, r, 2 * p + 1, lx, m);
ll m2 = mult(l, r, 2 * p + 2, m, rx);
return (m1 * m2) % MOD;
}
int ans(){
vector<int> ind;
ll best = 0;
ll optimaly = -1;
int r = 0;
if(s.empty() == false){
auto it = s.begin();
ll curr = 1;
bool included = 0;
int limit = 1000000000;
while(curr <= limit){
curr *= x[*it];
ind.push_back(*it);
if(curr > limit) continue;
if(*it == 0) included = 1;
it++;
if(it == s.end()){
if(ind.back() != 0) ind.push_back(0);
break;
}
}
reverse(ind.begin(), ind.end());
curr = 1;
if(included) curr = x[0];
ind.push_back(N);
for(int i = 1; i < (int)ind.size(); i++){
ll bestY = mx(ind[i - 1], ind[i], 0, 0, siz);
if(best < curr * bestY){
optimaly = bestY;
best = curr * bestY;
r = i;
}
if(ind[i] != N) curr *= x[ind[i]];
}
}else{
ind.push_back(N);
r = 0;
optimaly = mx(0, N, 0, 0, siz);
}
return (optimaly * mult(0, ind[r], 0, 0, siz)) % MOD;
}
int init(int n, int X[], int Y[] /*vector<int> X, vector<int> Y*/) {
N = n;
for(int i = 0; i < siz; i++){
segTree[i] = 0; segTree2[i] = 1;
}
for(int i = 0; i < N; i++){
x[i] = X[i]; y[i] = Y[i];
st(i, y[i], 0, 0, siz);
st_x(i, x[i], 0, 0, siz);
if(x[i] != 1) s.insert(i);
}
return ans();
}
int updateX(int pos, int val) {
st_x(pos, val, 0, 0, siz);
if(x[pos] != 1 && val == 1) s.erase(pos);
else if(x[pos] == 1 && val != 1) s.insert(pos);
x[pos] = val;
return ans();
}
int updateY(int pos, int val) {
st(pos, val, 0, 0, siz);
return ans();
}
/*
int main() {
int n; cin >> n;
vector<int> v(n);
vector<int> v2(n);
for(auto &i : v) cin >> i;
for(auto &i : v2) cin >> i;
cout << init(n, v, v2) << endl;
int m; cin >> m;
while(m--){
int t, pos, val; cin >> t >> pos >> val;
if(t) cout << updateX(pos, val) << endl;
else cout << updateY(pos, val)<< endl;
}
}*/
Compilation message
horses.cpp: In function 'void st_x(int, int, int, int, int)':
horses.cpp:49:34: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
49 | segTree2[p] = (left * right) % MOD;
| ~~~~~~~~~~~~~~~^~~~~
horses.cpp: In function 'int ans()':
horses.cpp:100:49: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
100 | return (optimaly * mult(0, ind[r], 0, 0, siz)) % MOD;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4436 KB |
Output is correct |
2 |
Correct |
2 ms |
4436 KB |
Output is correct |
3 |
Correct |
3 ms |
4436 KB |
Output is correct |
4 |
Correct |
2 ms |
4436 KB |
Output is correct |
5 |
Correct |
3 ms |
4412 KB |
Output is correct |
6 |
Correct |
2 ms |
4436 KB |
Output is correct |
7 |
Correct |
3 ms |
4436 KB |
Output is correct |
8 |
Correct |
3 ms |
4436 KB |
Output is correct |
9 |
Correct |
2 ms |
4396 KB |
Output is correct |
10 |
Correct |
2 ms |
4436 KB |
Output is correct |
11 |
Correct |
3 ms |
4436 KB |
Output is correct |
12 |
Correct |
4 ms |
4436 KB |
Output is correct |
13 |
Correct |
3 ms |
4436 KB |
Output is correct |
14 |
Correct |
3 ms |
4436 KB |
Output is correct |
15 |
Correct |
3 ms |
4412 KB |
Output is correct |
16 |
Correct |
3 ms |
4436 KB |
Output is correct |
17 |
Correct |
2 ms |
4436 KB |
Output is correct |
18 |
Correct |
3 ms |
4436 KB |
Output is correct |
19 |
Correct |
2 ms |
4436 KB |
Output is correct |
20 |
Correct |
2 ms |
4436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4436 KB |
Output is correct |
2 |
Correct |
2 ms |
4316 KB |
Output is correct |
3 |
Correct |
2 ms |
4436 KB |
Output is correct |
4 |
Correct |
2 ms |
4436 KB |
Output is correct |
5 |
Correct |
3 ms |
4436 KB |
Output is correct |
6 |
Correct |
3 ms |
4408 KB |
Output is correct |
7 |
Correct |
4 ms |
4436 KB |
Output is correct |
8 |
Correct |
4 ms |
4436 KB |
Output is correct |
9 |
Correct |
3 ms |
4436 KB |
Output is correct |
10 |
Correct |
3 ms |
4436 KB |
Output is correct |
11 |
Correct |
2 ms |
4436 KB |
Output is correct |
12 |
Correct |
2 ms |
4436 KB |
Output is correct |
13 |
Correct |
3 ms |
4436 KB |
Output is correct |
14 |
Correct |
2 ms |
4436 KB |
Output is correct |
15 |
Correct |
3 ms |
4408 KB |
Output is correct |
16 |
Correct |
3 ms |
4436 KB |
Output is correct |
17 |
Correct |
3 ms |
4436 KB |
Output is correct |
18 |
Correct |
2 ms |
4436 KB |
Output is correct |
19 |
Correct |
2 ms |
4436 KB |
Output is correct |
20 |
Correct |
4 ms |
4436 KB |
Output is correct |
21 |
Correct |
3 ms |
4404 KB |
Output is correct |
22 |
Correct |
3 ms |
4472 KB |
Output is correct |
23 |
Correct |
4 ms |
4396 KB |
Output is correct |
24 |
Correct |
4 ms |
4436 KB |
Output is correct |
25 |
Correct |
5 ms |
4544 KB |
Output is correct |
26 |
Correct |
3 ms |
4520 KB |
Output is correct |
27 |
Correct |
8 ms |
4416 KB |
Output is correct |
28 |
Correct |
5 ms |
4424 KB |
Output is correct |
29 |
Correct |
4 ms |
4416 KB |
Output is correct |
30 |
Correct |
4 ms |
4424 KB |
Output is correct |
31 |
Correct |
7 ms |
4436 KB |
Output is correct |
32 |
Correct |
8 ms |
4436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
754 ms |
40740 KB |
Output is correct |
2 |
Correct |
421 ms |
53324 KB |
Output is correct |
3 |
Correct |
403 ms |
44420 KB |
Output is correct |
4 |
Correct |
446 ms |
48168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4436 KB |
Output is correct |
2 |
Correct |
2 ms |
4328 KB |
Output is correct |
3 |
Correct |
3 ms |
4436 KB |
Output is correct |
4 |
Correct |
2 ms |
4436 KB |
Output is correct |
5 |
Correct |
2 ms |
4436 KB |
Output is correct |
6 |
Correct |
3 ms |
4436 KB |
Output is correct |
7 |
Correct |
3 ms |
4436 KB |
Output is correct |
8 |
Correct |
3 ms |
4436 KB |
Output is correct |
9 |
Correct |
3 ms |
4436 KB |
Output is correct |
10 |
Correct |
2 ms |
4436 KB |
Output is correct |
11 |
Correct |
3 ms |
4404 KB |
Output is correct |
12 |
Correct |
2 ms |
4412 KB |
Output is correct |
13 |
Correct |
2 ms |
4436 KB |
Output is correct |
14 |
Correct |
2 ms |
4436 KB |
Output is correct |
15 |
Correct |
3 ms |
4404 KB |
Output is correct |
16 |
Correct |
2 ms |
4412 KB |
Output is correct |
17 |
Correct |
3 ms |
4456 KB |
Output is correct |
18 |
Correct |
2 ms |
4436 KB |
Output is correct |
19 |
Correct |
2 ms |
4436 KB |
Output is correct |
20 |
Correct |
2 ms |
4436 KB |
Output is correct |
21 |
Correct |
2 ms |
4404 KB |
Output is correct |
22 |
Correct |
2 ms |
4436 KB |
Output is correct |
23 |
Correct |
4 ms |
4436 KB |
Output is correct |
24 |
Correct |
4 ms |
4436 KB |
Output is correct |
25 |
Correct |
5 ms |
4436 KB |
Output is correct |
26 |
Correct |
4 ms |
4436 KB |
Output is correct |
27 |
Correct |
10 ms |
4436 KB |
Output is correct |
28 |
Correct |
5 ms |
4436 KB |
Output is correct |
29 |
Correct |
4 ms |
4436 KB |
Output is correct |
30 |
Correct |
4 ms |
4420 KB |
Output is correct |
31 |
Correct |
6 ms |
4436 KB |
Output is correct |
32 |
Correct |
7 ms |
4492 KB |
Output is correct |
33 |
Correct |
189 ms |
20364 KB |
Output is correct |
34 |
Correct |
202 ms |
20288 KB |
Output is correct |
35 |
Correct |
351 ms |
50668 KB |
Output is correct |
36 |
Correct |
354 ms |
50504 KB |
Output is correct |
37 |
Correct |
236 ms |
18444 KB |
Output is correct |
38 |
Correct |
256 ms |
31248 KB |
Output is correct |
39 |
Correct |
180 ms |
18180 KB |
Output is correct |
40 |
Correct |
335 ms |
45676 KB |
Output is correct |
41 |
Correct |
225 ms |
18196 KB |
Output is correct |
42 |
Correct |
217 ms |
18348 KB |
Output is correct |
43 |
Correct |
322 ms |
46008 KB |
Output is correct |
44 |
Correct |
328 ms |
46004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4436 KB |
Output is correct |
2 |
Correct |
2 ms |
4436 KB |
Output is correct |
3 |
Correct |
2 ms |
4436 KB |
Output is correct |
4 |
Correct |
2 ms |
4412 KB |
Output is correct |
5 |
Correct |
2 ms |
4404 KB |
Output is correct |
6 |
Correct |
4 ms |
4404 KB |
Output is correct |
7 |
Correct |
2 ms |
4436 KB |
Output is correct |
8 |
Correct |
3 ms |
4404 KB |
Output is correct |
9 |
Correct |
2 ms |
4436 KB |
Output is correct |
10 |
Correct |
3 ms |
4436 KB |
Output is correct |
11 |
Correct |
3 ms |
4408 KB |
Output is correct |
12 |
Correct |
3 ms |
4436 KB |
Output is correct |
13 |
Correct |
2 ms |
4436 KB |
Output is correct |
14 |
Correct |
2 ms |
4436 KB |
Output is correct |
15 |
Correct |
3 ms |
4436 KB |
Output is correct |
16 |
Correct |
3 ms |
4436 KB |
Output is correct |
17 |
Correct |
4 ms |
4436 KB |
Output is correct |
18 |
Correct |
2 ms |
4408 KB |
Output is correct |
19 |
Correct |
2 ms |
4436 KB |
Output is correct |
20 |
Correct |
3 ms |
4436 KB |
Output is correct |
21 |
Correct |
3 ms |
4436 KB |
Output is correct |
22 |
Correct |
2 ms |
4436 KB |
Output is correct |
23 |
Correct |
4 ms |
4420 KB |
Output is correct |
24 |
Correct |
4 ms |
4436 KB |
Output is correct |
25 |
Correct |
5 ms |
4440 KB |
Output is correct |
26 |
Correct |
4 ms |
4436 KB |
Output is correct |
27 |
Correct |
9 ms |
4424 KB |
Output is correct |
28 |
Correct |
5 ms |
4540 KB |
Output is correct |
29 |
Correct |
4 ms |
4436 KB |
Output is correct |
30 |
Correct |
4 ms |
4416 KB |
Output is correct |
31 |
Correct |
6 ms |
4436 KB |
Output is correct |
32 |
Correct |
8 ms |
4484 KB |
Output is correct |
33 |
Correct |
846 ms |
44452 KB |
Output is correct |
34 |
Correct |
448 ms |
53156 KB |
Output is correct |
35 |
Correct |
457 ms |
44368 KB |
Output is correct |
36 |
Correct |
479 ms |
48240 KB |
Output is correct |
37 |
Correct |
196 ms |
20300 KB |
Output is correct |
38 |
Correct |
189 ms |
20220 KB |
Output is correct |
39 |
Correct |
345 ms |
50548 KB |
Output is correct |
40 |
Correct |
338 ms |
50552 KB |
Output is correct |
41 |
Correct |
240 ms |
18356 KB |
Output is correct |
42 |
Correct |
245 ms |
31136 KB |
Output is correct |
43 |
Correct |
196 ms |
18120 KB |
Output is correct |
44 |
Correct |
335 ms |
45600 KB |
Output is correct |
45 |
Correct |
210 ms |
18196 KB |
Output is correct |
46 |
Correct |
212 ms |
18304 KB |
Output is correct |
47 |
Correct |
323 ms |
45996 KB |
Output is correct |
48 |
Correct |
326 ms |
45928 KB |
Output is correct |
49 |
Correct |
317 ms |
23304 KB |
Output is correct |
50 |
Correct |
280 ms |
23364 KB |
Output is correct |
51 |
Correct |
497 ms |
52400 KB |
Output is correct |
52 |
Correct |
407 ms |
51904 KB |
Output is correct |
53 |
Correct |
783 ms |
21708 KB |
Output is correct |
54 |
Correct |
430 ms |
35184 KB |
Output is correct |
55 |
Correct |
274 ms |
19368 KB |
Output is correct |
56 |
Correct |
423 ms |
47628 KB |
Output is correct |
57 |
Correct |
512 ms |
19920 KB |
Output is correct |
58 |
Correct |
630 ms |
20460 KB |
Output is correct |
59 |
Correct |
321 ms |
46020 KB |
Output is correct |