#include "horses.h"
#include <bits/stdc++.h>
using namespace std;
#define rep(i , j , k) for (int i = j; i < (int)k; i++)
#define pb push_back
#define lid id<<1
#define rid lid|1
typedef long long ll;
typedef pair<ll ,ll> pll;
const int MOD = 1e9 + 7;
const int MAXN = 5e5 + 10;
int N;
ll A[MAXN], B[MAXN], segM[MAXN << 2], segX[MAXN << 2];
bool segH[MAXN << 2];
void build(int l = 0, int r = N, int id = 1) {
if (l == r - 1) {
segM[id] = A[l];
segX[id] = B[l];
segH[id] = segM[id] > 1;
return;
}
int mid = l +r >> 1;
build(l , mid , lid);
build(mid , r ,rid);
segM[id] = segM[lid] * segM[rid] % MOD;
segX[id] = max(segX[lid] , segX[rid]);
segH[id] = segH[lid] | segH[rid];
}
void segUpt(int pos , int l = 0 , int r = N, int id = 1) {
if (l == r - 1) {
segM[id] = A[l];
segX[id] = B[l];
segH[id] = segM[id] > 1;
return;
}
int mid = l +r >> 1;
if (pos < mid) segUpt(pos , l , mid , lid);
else segUpt(pos , mid , r ,rid);
segM[id] = segM[lid] * segM[rid] % MOD;
segX[id] = max(segX[lid] , segX[rid]);
segH[id] = segH[lid] | segH[rid];
}
int segRight(int lim , int l = 0, int r = N, int id = 1) {
if (l >= lim || !segH[id]) return -1;
if (l == r - 1) return l;
int mid = l + r >> 1;
int res = segRight(lim , mid , r, rid);
if (!~res) res = segRight(lim , l , mid, lid);
return res;
}
int segMax(int lim , int l = 0, int r = N, int id = 1) {
if (l >= lim) return segX[id];
int mid = l +r >> 1;
int res = segMax(lim , mid , r , rid);
if (lim < mid) res = max(res , segMax(lim , l , mid , lid));
return res;
}
int segMul(int lim , int l = 0, int r = N, int id = 1) {
if (l >= lim) return 1;
if (r <= lim) return segM[id];
int mid = l + r >> 1;
return 1ll * segMul(lim , l , mid , lid) * segMul(lim , mid , r, rid) % MOD;
}
int solve() {
vector<pll> pos;
ll mul = 1;
int last = N;
while (mul < 1e9 && last) {
int id = segRight(last);
if (!~id) {
pos.pb({0 , segMax(0)});
break;
}
mul *= A[id];
last = id;
pos.pb({id , segMax(id)});
}
ll best = 0;
bool biger = false;
for (auto e : pos) {
ll local = e.second;
if(biger)
local = best;
else if (best > local)
local = best;
local *= A[e.first];
if (local >= MOD) {
biger = true;
local %= MOD;
}
best = local;
}
//cout << best * segMul(pos.back().first) % MOD << endl;
return best * segMul(pos.back().first) % MOD;
}
int init(int N2, int X[], int Y[]) {
N = N2;
rep(i , 0 , N) {
A[i] = X[i];
B[i] = Y[i];
}
build();
return solve();
}
int updateX(int pos, int val) {
A[pos] = val;
segUpt(pos);
return solve();
}
int updateY(int pos, int val) {
B[pos] = val;
segUpt(pos);
return solve();
}
Compilation message
horses.cpp: In function 'void build(int, int, int)':
horses.cpp:29:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l +r >> 1;
~~^~
horses.cpp: In function 'void segUpt(int, int, int, int)':
horses.cpp:44:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l +r >> 1;
~~^~
horses.cpp: In function 'int segRight(int, int, int, int)':
horses.cpp:55:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
horses.cpp: In function 'int segMax(int, int, int, int)':
horses.cpp:62:30: warning: conversion to 'int' from 'll {aka long long int}' may alter its value [-Wconversion]
if (l >= lim) return segX[id];
~~~~~~~^
horses.cpp:63:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l +r >> 1;
~~^~
horses.cpp: In function 'int segMul(int, int, int, int)':
horses.cpp:71:30: warning: conversion to 'int' from 'll {aka long long int}' may alter its value [-Wconversion]
if (r <= lim) return segM[id];
~~~~~~~^
horses.cpp:72:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
horses.cpp:73:72: warning: conversion to 'int' from 'long long int' may alter its value [-Wconversion]
return 1ll * segMul(lim , l , mid , lid) * segMul(lim , mid , r, rid) % MOD;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
horses.cpp: In function 'int solve()':
horses.cpp:81:15: warning: conversion to 'double' from 'll {aka long long int}' may alter its value [-Wconversion]
while (mul < 1e9 && last) {
^~~
horses.cpp:107:34: warning: conversion to 'int' from 'long long int' may alter its value [-Wconversion]
return best * segMul(pos.back().first) % MOD;
~~~~~~~~~~~^~~~~
horses.cpp:107:41: warning: conversion to 'int' from 'll {aka long long int}' may alter its value [-Wconversion]
return best * segMul(pos.back().first) % MOD;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
380 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
424 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
3 ms |
380 KB |
Output is correct |
24 |
Correct |
3 ms |
504 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
3 ms |
504 KB |
Output is correct |
27 |
Correct |
6 ms |
376 KB |
Output is correct |
28 |
Correct |
3 ms |
504 KB |
Output is correct |
29 |
Correct |
3 ms |
376 KB |
Output is correct |
30 |
Correct |
3 ms |
508 KB |
Output is correct |
31 |
Correct |
5 ms |
504 KB |
Output is correct |
32 |
Correct |
6 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
677 ms |
30600 KB |
Output is correct |
2 |
Correct |
203 ms |
30532 KB |
Output is correct |
3 |
Correct |
246 ms |
30536 KB |
Output is correct |
4 |
Correct |
277 ms |
30584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
3 ms |
376 KB |
Output is correct |
24 |
Correct |
3 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
504 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
27 |
Correct |
6 ms |
376 KB |
Output is correct |
28 |
Correct |
3 ms |
376 KB |
Output is correct |
29 |
Correct |
3 ms |
504 KB |
Output is correct |
30 |
Correct |
3 ms |
504 KB |
Output is correct |
31 |
Correct |
5 ms |
376 KB |
Output is correct |
32 |
Correct |
6 ms |
376 KB |
Output is correct |
33 |
Correct |
64 ms |
29816 KB |
Output is correct |
34 |
Correct |
61 ms |
29816 KB |
Output is correct |
35 |
Correct |
93 ms |
29688 KB |
Output is correct |
36 |
Correct |
80 ms |
29700 KB |
Output is correct |
37 |
Correct |
118 ms |
29816 KB |
Output is correct |
38 |
Correct |
68 ms |
32644 KB |
Output is correct |
39 |
Correct |
49 ms |
31608 KB |
Output is correct |
40 |
Correct |
67 ms |
35448 KB |
Output is correct |
41 |
Correct |
82 ms |
31736 KB |
Output is correct |
42 |
Correct |
108 ms |
31736 KB |
Output is correct |
43 |
Correct |
58 ms |
35972 KB |
Output is correct |
44 |
Correct |
57 ms |
35832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
3 ms |
504 KB |
Output is correct |
24 |
Correct |
3 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
6 ms |
376 KB |
Output is correct |
28 |
Correct |
4 ms |
504 KB |
Output is correct |
29 |
Correct |
3 ms |
376 KB |
Output is correct |
30 |
Correct |
3 ms |
504 KB |
Output is correct |
31 |
Correct |
5 ms |
504 KB |
Output is correct |
32 |
Correct |
6 ms |
504 KB |
Output is correct |
33 |
Correct |
670 ms |
30796 KB |
Output is correct |
34 |
Correct |
205 ms |
30796 KB |
Output is correct |
35 |
Correct |
239 ms |
32320 KB |
Output is correct |
36 |
Correct |
291 ms |
38164 KB |
Output is correct |
37 |
Correct |
73 ms |
33528 KB |
Output is correct |
38 |
Correct |
63 ms |
33512 KB |
Output is correct |
39 |
Correct |
94 ms |
40440 KB |
Output is correct |
40 |
Correct |
82 ms |
40440 KB |
Output is correct |
41 |
Correct |
118 ms |
31736 KB |
Output is correct |
42 |
Correct |
67 ms |
32632 KB |
Output is correct |
43 |
Correct |
48 ms |
31608 KB |
Output is correct |
44 |
Correct |
72 ms |
35660 KB |
Output is correct |
45 |
Correct |
81 ms |
31744 KB |
Output is correct |
46 |
Correct |
107 ms |
31836 KB |
Output is correct |
47 |
Correct |
56 ms |
35832 KB |
Output is correct |
48 |
Correct |
56 ms |
35960 KB |
Output is correct |
49 |
Correct |
223 ms |
35576 KB |
Output is correct |
50 |
Correct |
197 ms |
35576 KB |
Output is correct |
51 |
Correct |
274 ms |
42488 KB |
Output is correct |
52 |
Correct |
157 ms |
41920 KB |
Output is correct |
53 |
Correct |
764 ms |
34200 KB |
Output is correct |
54 |
Correct |
300 ms |
34424 KB |
Output is correct |
55 |
Correct |
149 ms |
32632 KB |
Output is correct |
56 |
Correct |
175 ms |
37400 KB |
Output is correct |
57 |
Correct |
465 ms |
33272 KB |
Output is correct |
58 |
Correct |
722 ms |
34108 KB |
Output is correct |
59 |
Correct |
56 ms |
35832 KB |
Output is correct |