#include "horses.h"
#include<bits/stdc++.h>
#define f first
#define s second
#define ll long long
#define pii pair<int,int>
using namespace std;
const int mxn=5e5+5;
const ll inf=1e9;
const ll mod = 1e9 + 7;
ll x[mxn],y[mxn];
int n;
set<int> st;
ll bigmod(ll a,ll b) {
if(b==0)return 1;
ll mid=b/2;
ll c=bigmod(a,mid);
if(b%2==0)return (c*c)%mod;
c=(c*c)%mod;
return (a*c)%mod;
}
struct segtree {
int tree[4*mxn] = {};
void update(int node,int b,int e,int p,int v) {
if(e<p|| b>p)return;
if(b == e) {
tree[node] = v;
return;
}
int mid=b+e>>1;
int left=node<<1;
int right=left|1;
update(left,b,mid,p,v);
update(right,mid+1,e,p,v);
tree[node]=max(tree[left],tree[right]);
}
int query(int node,int b,int e,int l,int r) {
if(e<l || b>r) return 0;
if(b>=l && e<=r) {
return tree[node];
}
int mid=b+e>>1;
int left=node<<1;
int right=left|1;
return max(query(left,b,mid,l,r),query(right,mid + 1, e, l,r));
}
} seg;
struct BIT
{
ll bit[mxn];
void init() {
for(int i = 1; i < mxn; i++) bit[i] = 1;
}
void update(int p,ll v) {
if(p==0) return;
for(;p<mxn;p+=p&-p) bit[p] = bit[p] * v % mod;
}
ll query(int p) {
ll r = 1;
for(;p>0;p-=p&-p) r = bit[p] * r % mod;
return r;
}
} bt;
int calc() {
if(st.empty()) {
return seg.query(1,1,n,1,n);
}
int k = 0;
ll ans = 0;
ll cm = 0;
int last = n;
for(auto it = st.rbegin(); it != st.rend(); it++) {
int i = *it;
if(last - i > 1) {
ll v1 = bt.query(i + 2);
ll v2 = seg.query(1,1,n, i + 2, last);
if(v2 > cm) {
ans = v1 * v2 % mod;
cm = v2;
}
}
if(i == -1) break;
last = i;
ll v1 = bt.query(i + 1);
ll v2 = y[i];
if(v2 > cm) {
ans = v1 * v2 % mod;
cm = x[i] * v2;
} else {
cm = cm * x[i];
}
if(cm >= inf) break;
}
return ans;
}
int init(int N, int X[], int Y[]) {
st.insert(-1);
for(int i = 0; i < N; i++) {
x[i] = X[i];
y[i] = Y[i];
if(X[i] != 1) st.insert(i);
else {
seg.update(1,1,N,i + 1,y[i]);
}
}
bt.init();
n = N;
for(int i = 1; i <= N; i++) {
bt.update(i,X[i - 1]);
}
return calc();
}
int updateX(int pos, int val) {
ll tv = bigmod(x[pos],mod - 2);
bt.update(pos + 1, tv);
bt.update(pos + 1,val);
if(x[pos] != 1) {
st.erase(pos);
seg.update(1,1,n,pos + 1,y[pos]);
}
if(val != 1) {
st.insert(pos);
seg.update(1,1,n,pos + 1,0);
}
x[pos] = val;
return calc();
}
int updateY(int pos, int val) {
y[pos] = val;
if(x[pos] == 1) {
seg.update(1,1,n,pos + 1,val);
}
return calc();
}
Compilation message
horses.cpp: In member function 'void segtree::update(int, int, int, int, int)':
horses.cpp:41:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
41 | int mid=b+e>>1;
| ~^~
horses.cpp: In member function 'int segtree::query(int, int, int, int, int)':
horses.cpp:56:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | int mid=b+e>>1;
| ~^~
horses.cpp: In function 'int calc()':
horses.cpp:124:9: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
124 | return ans;
| ^~~
horses.cpp:93:6: warning: unused variable 'k' [-Wunused-variable]
93 | int k = 0;
| ^
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:138:30: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
138 | seg.update(1,1,N,i + 1,y[i]);
| ~~~^
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:162:33: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
162 | seg.update(1,1,n,pos + 1,y[pos]);
| ~~~~~^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4300 KB |
Output is correct |
2 |
Correct |
3 ms |
4172 KB |
Output is correct |
3 |
Correct |
2 ms |
4172 KB |
Output is correct |
4 |
Correct |
2 ms |
4172 KB |
Output is correct |
5 |
Correct |
2 ms |
4172 KB |
Output is correct |
6 |
Correct |
2 ms |
4172 KB |
Output is correct |
7 |
Correct |
2 ms |
4172 KB |
Output is correct |
8 |
Correct |
2 ms |
4172 KB |
Output is correct |
9 |
Correct |
2 ms |
4176 KB |
Output is correct |
10 |
Correct |
2 ms |
4172 KB |
Output is correct |
11 |
Correct |
2 ms |
4172 KB |
Output is correct |
12 |
Correct |
2 ms |
4172 KB |
Output is correct |
13 |
Correct |
2 ms |
4172 KB |
Output is correct |
14 |
Correct |
2 ms |
4172 KB |
Output is correct |
15 |
Correct |
2 ms |
4172 KB |
Output is correct |
16 |
Correct |
2 ms |
4172 KB |
Output is correct |
17 |
Correct |
2 ms |
4172 KB |
Output is correct |
18 |
Correct |
3 ms |
4172 KB |
Output is correct |
19 |
Correct |
2 ms |
4172 KB |
Output is correct |
20 |
Correct |
2 ms |
4172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4172 KB |
Output is correct |
2 |
Correct |
3 ms |
4172 KB |
Output is correct |
3 |
Correct |
2 ms |
4172 KB |
Output is correct |
4 |
Correct |
2 ms |
4172 KB |
Output is correct |
5 |
Correct |
2 ms |
4172 KB |
Output is correct |
6 |
Correct |
2 ms |
4172 KB |
Output is correct |
7 |
Correct |
2 ms |
4172 KB |
Output is correct |
8 |
Correct |
3 ms |
4176 KB |
Output is correct |
9 |
Correct |
2 ms |
4172 KB |
Output is correct |
10 |
Correct |
2 ms |
4172 KB |
Output is correct |
11 |
Correct |
2 ms |
4172 KB |
Output is correct |
12 |
Correct |
2 ms |
4172 KB |
Output is correct |
13 |
Correct |
2 ms |
4172 KB |
Output is correct |
14 |
Correct |
2 ms |
4172 KB |
Output is correct |
15 |
Correct |
2 ms |
4172 KB |
Output is correct |
16 |
Correct |
2 ms |
4172 KB |
Output is correct |
17 |
Correct |
2 ms |
4172 KB |
Output is correct |
18 |
Correct |
3 ms |
4172 KB |
Output is correct |
19 |
Correct |
2 ms |
4172 KB |
Output is correct |
20 |
Correct |
2 ms |
4172 KB |
Output is correct |
21 |
Correct |
2 ms |
4172 KB |
Output is correct |
22 |
Correct |
2 ms |
4172 KB |
Output is correct |
23 |
Correct |
3 ms |
4172 KB |
Output is correct |
24 |
Correct |
3 ms |
4172 KB |
Output is correct |
25 |
Correct |
3 ms |
4300 KB |
Output is correct |
26 |
Correct |
3 ms |
4300 KB |
Output is correct |
27 |
Correct |
6 ms |
4172 KB |
Output is correct |
28 |
Correct |
5 ms |
4300 KB |
Output is correct |
29 |
Correct |
4 ms |
4172 KB |
Output is correct |
30 |
Correct |
4 ms |
4300 KB |
Output is correct |
31 |
Correct |
3 ms |
4172 KB |
Output is correct |
32 |
Correct |
6 ms |
4172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
287 ms |
40580 KB |
Output is correct |
2 |
Correct |
373 ms |
44420 KB |
Output is correct |
3 |
Correct |
363 ms |
44592 KB |
Output is correct |
4 |
Correct |
493 ms |
44424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4308 KB |
Output is correct |
3 |
Correct |
2 ms |
4172 KB |
Output is correct |
4 |
Correct |
2 ms |
4172 KB |
Output is correct |
5 |
Correct |
2 ms |
4172 KB |
Output is correct |
6 |
Correct |
2 ms |
4172 KB |
Output is correct |
7 |
Correct |
2 ms |
4172 KB |
Output is correct |
8 |
Correct |
2 ms |
4172 KB |
Output is correct |
9 |
Correct |
2 ms |
4172 KB |
Output is correct |
10 |
Correct |
2 ms |
4172 KB |
Output is correct |
11 |
Correct |
2 ms |
4172 KB |
Output is correct |
12 |
Correct |
2 ms |
4172 KB |
Output is correct |
13 |
Correct |
2 ms |
4172 KB |
Output is correct |
14 |
Correct |
2 ms |
4172 KB |
Output is correct |
15 |
Correct |
2 ms |
4172 KB |
Output is correct |
16 |
Correct |
2 ms |
4172 KB |
Output is correct |
17 |
Correct |
3 ms |
4172 KB |
Output is correct |
18 |
Correct |
2 ms |
4172 KB |
Output is correct |
19 |
Correct |
2 ms |
4128 KB |
Output is correct |
20 |
Correct |
2 ms |
4172 KB |
Output is correct |
21 |
Correct |
2 ms |
4172 KB |
Output is correct |
22 |
Correct |
2 ms |
4172 KB |
Output is correct |
23 |
Correct |
3 ms |
4172 KB |
Output is correct |
24 |
Correct |
3 ms |
4172 KB |
Output is correct |
25 |
Correct |
4 ms |
4300 KB |
Output is correct |
26 |
Correct |
3 ms |
4300 KB |
Output is correct |
27 |
Correct |
6 ms |
4172 KB |
Output is correct |
28 |
Correct |
4 ms |
4300 KB |
Output is correct |
29 |
Correct |
4 ms |
4172 KB |
Output is correct |
30 |
Correct |
4 ms |
4300 KB |
Output is correct |
31 |
Correct |
4 ms |
4172 KB |
Output is correct |
32 |
Correct |
6 ms |
4276 KB |
Output is correct |
33 |
Correct |
131 ms |
20280 KB |
Output is correct |
34 |
Correct |
132 ms |
20628 KB |
Output is correct |
35 |
Correct |
193 ms |
39528 KB |
Output is correct |
36 |
Correct |
185 ms |
39468 KB |
Output is correct |
37 |
Correct |
198 ms |
20420 KB |
Output is correct |
38 |
Correct |
160 ms |
32068 KB |
Output is correct |
39 |
Correct |
125 ms |
20068 KB |
Output is correct |
40 |
Correct |
172 ms |
39676 KB |
Output is correct |
41 |
Correct |
123 ms |
20064 KB |
Output is correct |
42 |
Correct |
175 ms |
20036 KB |
Output is correct |
43 |
Correct |
166 ms |
39424 KB |
Output is correct |
44 |
Correct |
182 ms |
39464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4172 KB |
Output is correct |
2 |
Correct |
2 ms |
4172 KB |
Output is correct |
3 |
Correct |
3 ms |
4172 KB |
Output is correct |
4 |
Correct |
2 ms |
4172 KB |
Output is correct |
5 |
Correct |
2 ms |
4172 KB |
Output is correct |
6 |
Correct |
3 ms |
4172 KB |
Output is correct |
7 |
Correct |
2 ms |
4172 KB |
Output is correct |
8 |
Correct |
2 ms |
4172 KB |
Output is correct |
9 |
Correct |
2 ms |
4172 KB |
Output is correct |
10 |
Correct |
2 ms |
4172 KB |
Output is correct |
11 |
Correct |
2 ms |
4172 KB |
Output is correct |
12 |
Correct |
2 ms |
4172 KB |
Output is correct |
13 |
Correct |
2 ms |
4172 KB |
Output is correct |
14 |
Correct |
2 ms |
4172 KB |
Output is correct |
15 |
Correct |
2 ms |
4172 KB |
Output is correct |
16 |
Correct |
2 ms |
4172 KB |
Output is correct |
17 |
Correct |
2 ms |
4172 KB |
Output is correct |
18 |
Correct |
2 ms |
4172 KB |
Output is correct |
19 |
Correct |
2 ms |
4172 KB |
Output is correct |
20 |
Correct |
2 ms |
4172 KB |
Output is correct |
21 |
Correct |
2 ms |
4172 KB |
Output is correct |
22 |
Correct |
3 ms |
4172 KB |
Output is correct |
23 |
Correct |
3 ms |
4172 KB |
Output is correct |
24 |
Correct |
3 ms |
4172 KB |
Output is correct |
25 |
Correct |
5 ms |
4300 KB |
Output is correct |
26 |
Correct |
3 ms |
4300 KB |
Output is correct |
27 |
Correct |
6 ms |
4172 KB |
Output is correct |
28 |
Correct |
4 ms |
4300 KB |
Output is correct |
29 |
Correct |
4 ms |
4172 KB |
Output is correct |
30 |
Correct |
4 ms |
4312 KB |
Output is correct |
31 |
Correct |
4 ms |
4172 KB |
Output is correct |
32 |
Correct |
6 ms |
4172 KB |
Output is correct |
33 |
Correct |
285 ms |
40328 KB |
Output is correct |
34 |
Correct |
366 ms |
44408 KB |
Output is correct |
35 |
Correct |
347 ms |
44512 KB |
Output is correct |
36 |
Correct |
501 ms |
44420 KB |
Output is correct |
37 |
Correct |
134 ms |
20340 KB |
Output is correct |
38 |
Correct |
127 ms |
20160 KB |
Output is correct |
39 |
Correct |
194 ms |
39492 KB |
Output is correct |
40 |
Correct |
183 ms |
39464 KB |
Output is correct |
41 |
Correct |
198 ms |
20284 KB |
Output is correct |
42 |
Correct |
153 ms |
31996 KB |
Output is correct |
43 |
Correct |
124 ms |
20036 KB |
Output is correct |
44 |
Correct |
173 ms |
39604 KB |
Output is correct |
45 |
Correct |
122 ms |
20188 KB |
Output is correct |
46 |
Correct |
187 ms |
20064 KB |
Output is correct |
47 |
Correct |
161 ms |
39440 KB |
Output is correct |
48 |
Correct |
159 ms |
39476 KB |
Output is correct |
49 |
Correct |
265 ms |
22208 KB |
Output is correct |
50 |
Correct |
232 ms |
22092 KB |
Output is correct |
51 |
Correct |
321 ms |
40556 KB |
Output is correct |
52 |
Correct |
249 ms |
40376 KB |
Output is correct |
53 |
Correct |
953 ms |
22060 KB |
Output is correct |
54 |
Correct |
330 ms |
35140 KB |
Output is correct |
55 |
Correct |
251 ms |
20176 KB |
Output is correct |
56 |
Correct |
289 ms |
41284 KB |
Output is correct |
57 |
Correct |
243 ms |
20928 KB |
Output is correct |
58 |
Correct |
790 ms |
21188 KB |
Output is correct |
59 |
Correct |
165 ms |
39492 KB |
Output is correct |