# include "horses.h"
# include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define ll long long
#define pii pair <ll, ll>
const ll NN = 5e5 + 5, inf = 2e9, mod = 1e9 + 7;
long long n, x[NN],y[NN],tree_ml[4 * NN];
pii tree_mx[4 * NN];
long long merge_ml(ll a, ll b) {
return (a * b) % mod;
}
pii merge_mx(pii a, pii b) {
return max(a, b);
}
void build(ll node, ll le, ll ri) {
if (le == ri) {
tree_ml[node] = x[le];
tree_mx[node] = {y[le], le};
return ;
}
ll mid = (le + ri) / 2;
build(2 * node, le, mid);
build(2 * node + 1, mid + 1, ri);
tree_ml[node] = merge_ml(tree_ml[2 * node], tree_ml[2 * node + 1]);
tree_mx[node] = merge_mx(tree_mx[2 * node], tree_mx[2 * node + 1]);
}
void update(ll node, ll le, ll ri, ll idx, ll val, ll ty) {
if (le > idx || ri < idx) return ;
if (le == ri) {
if (ty == 0) tree_ml[node] = x[le];
else tree_mx[node] = {y[le], le};
return ;
}
ll mid = (le + ri) / 2;
update(2 * node,le, mid,idx,val,ty);
update(2 * node + 1, mid + 1, ri, idx,val,ty);
tree_ml[node] = merge_ml(tree_ml[2 * node], tree_ml[2 * node + 1]);
tree_mx[node] = merge_mx(tree_mx[2 * node], tree_mx[2 * node + 1]);
}
ll get_ml(ll node, ll le, ll ri, ll start, ll end) {
if (le > end || ri < start || start > end) return 1LL;
if (le >= start && ri <= end) return tree_ml[node];
ll mid = (le + ri) / 2;
return merge_ml(get_ml(2 * node, le, mid, start,end), get_ml(2 * node + 1, mid + 1, ri, start, end));
}
pii get_mx(ll node, ll le, ll ri, ll start, ll end) {
if (le > end || ri < start || start > end) return {-1e9, 0LL};
if (le >= start && ri <= end) return tree_mx[node];
ll mid = (le + ri) / 2;
return merge_mx(get_mx(2 * node, le, mid,start,end), get_mx(2 * node + 1, mid + 1, ri, start, end));
}
set <ll> s;
ll go() {
auto it = --s.end(); // n + 1
auto it1 = it; it--;
ll cur_mx = 1, cnt = 0, optid = n;
while (true) {
cnt++;
ll st_y = (*it); ll nd_y = (*it1) - 1;
if (st_y == 0) st_y++;
ll mx_y = get_mx(1,1,n,st_y, nd_y).f;
if (mx_y > cur_mx) {
optid = get_mx(1,1,n, st_y, nd_y).s;
cur_mx = mx_y;
}
cur_mx *= x[st_y];
if (it1 == s.begin() || cnt > 32 || cur_mx > inf) break;
it--; it1--;
}
return (get_ml(1,1,n,1,optid) * y[optid]) % mod;
}
int init(int N, int X[], int Y[]) {
n = N;
x[0] = 1; y[0] = 1;
for (int i = 1; i <= n; i++) {
x[i] = X[i - 1]; y[i] = Y[i - 1];
if (x[i] != 1) s.insert(i);
}
s.insert(0); s.insert(n + 1);
build(1, 1, n);
return go();
}
int updateX(int pos, int val) {
pos++;
if (x[pos] != 1) {
s.erase(pos);
}
x[pos] = val;
update(1,1,n, pos,val,0);
if (x[pos] != 1) s.insert(pos);
return go();
}
int updateY(int pos, int val) {
pos++;
y[pos] = val;
update(1,1,n, pos,val, 1);
return go();
}
/*
main() {
int N;
cin>>N;
int *X = (int*)malloc(sizeof(int)*(unsigned int)N);
int *Y = (int*)malloc(sizeof(int)*(unsigned int)N);
for (int i = 0; i < N; i++) {
cin>>X[i];
}
for (int i = 0; i < N; i++) {
cin>>Y[i];
}
cout<<init(N,X,Y)<<"\n";
int M;
cin>>M;
for (int i = 0; i < M; i++) {
int type; cin>>type;
int pos; cin>>pos;
int val; cin>>val;
if (type == 1) {
cout<<updateX(pos,val)<<"\n";
} else if (type == 2) {
cout<<updateY(pos,val)<<"\n";
}
}
return 0;
}*/
Compilation message
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:84:11: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
84 | return go();
| ~~^~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:95:14: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
95 | return go();
| ~~^~
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:102:14: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
102 | return go();
| ~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 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 |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
1 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 |
1 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 |
1 ms |
340 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 |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
328 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
472 KB |
Output is correct |
27 |
Correct |
4 ms |
436 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
3 ms |
340 KB |
Output is correct |
32 |
Correct |
4 ms |
432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
649 ms |
61264 KB |
Output is correct |
2 |
Correct |
327 ms |
73728 KB |
Output is correct |
3 |
Correct |
348 ms |
64904 KB |
Output is correct |
4 |
Correct |
387 ms |
68800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 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 |
340 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 |
1 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 |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
276 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
312 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
496 KB |
Output is correct |
27 |
Correct |
5 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
320 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
5 ms |
340 KB |
Output is correct |
32 |
Correct |
5 ms |
340 KB |
Output is correct |
33 |
Correct |
53 ms |
40792 KB |
Output is correct |
34 |
Correct |
57 ms |
40716 KB |
Output is correct |
35 |
Correct |
189 ms |
71172 KB |
Output is correct |
36 |
Correct |
183 ms |
71028 KB |
Output is correct |
37 |
Correct |
105 ms |
38968 KB |
Output is correct |
38 |
Correct |
103 ms |
51736 KB |
Output is correct |
39 |
Correct |
37 ms |
38732 KB |
Output is correct |
40 |
Correct |
164 ms |
66256 KB |
Output is correct |
41 |
Correct |
66 ms |
38732 KB |
Output is correct |
42 |
Correct |
87 ms |
38792 KB |
Output is correct |
43 |
Correct |
151 ms |
66516 KB |
Output is correct |
44 |
Correct |
158 ms |
66428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 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 |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
304 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
308 KB |
Output is correct |
22 |
Correct |
1 ms |
312 KB |
Output is correct |
23 |
Correct |
1 ms |
320 KB |
Output is correct |
24 |
Correct |
1 ms |
324 KB |
Output is correct |
25 |
Correct |
2 ms |
496 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
4 ms |
328 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
456 KB |
Output is correct |
31 |
Correct |
3 ms |
340 KB |
Output is correct |
32 |
Correct |
5 ms |
324 KB |
Output is correct |
33 |
Correct |
645 ms |
64960 KB |
Output is correct |
34 |
Correct |
321 ms |
73808 KB |
Output is correct |
35 |
Correct |
331 ms |
64908 KB |
Output is correct |
36 |
Correct |
364 ms |
68704 KB |
Output is correct |
37 |
Correct |
52 ms |
40780 KB |
Output is correct |
38 |
Correct |
47 ms |
40716 KB |
Output is correct |
39 |
Correct |
195 ms |
71124 KB |
Output is correct |
40 |
Correct |
173 ms |
71084 KB |
Output is correct |
41 |
Correct |
110 ms |
38984 KB |
Output is correct |
42 |
Correct |
110 ms |
51684 KB |
Output is correct |
43 |
Correct |
41 ms |
38668 KB |
Output is correct |
44 |
Correct |
153 ms |
66220 KB |
Output is correct |
45 |
Correct |
67 ms |
38788 KB |
Output is correct |
46 |
Correct |
91 ms |
38928 KB |
Output is correct |
47 |
Correct |
152 ms |
66536 KB |
Output is correct |
48 |
Correct |
165 ms |
66500 KB |
Output is correct |
49 |
Correct |
199 ms |
43764 KB |
Output is correct |
50 |
Correct |
145 ms |
43724 KB |
Output is correct |
51 |
Correct |
359 ms |
73012 KB |
Output is correct |
52 |
Correct |
235 ms |
72672 KB |
Output is correct |
53 |
Correct |
764 ms |
42200 KB |
Output is correct |
54 |
Correct |
282 ms |
55692 KB |
Output is correct |
55 |
Correct |
130 ms |
39812 KB |
Output is correct |
56 |
Correct |
244 ms |
67904 KB |
Output is correct |
57 |
Correct |
470 ms |
40508 KB |
Output is correct |
58 |
Correct |
644 ms |
40900 KB |
Output is correct |
59 |
Correct |
151 ms |
66492 KB |
Output is correct |