#include "horses.h"
#include<bits/stdc++.h>
using namespace std;
#define maxn 500050
#define mod 1000000007
#define ll long long
#define f first
#define s second
#define ii pair<ll,ll>
#define db(x) cerr << #x << ": " << (x) << '\n';
struct node{
int my, px;
} st[maxn*4];
int x[maxn], y[maxn], n;
set<int> s;
node join(node a,node b){
return { max( a.my , b.my ) , (ll)a.px * b.px % mod };
}
void build(int nod,int l,int r){
if( l == r ){ st[nod] = { y[l] , x[l] }; return; }
int mi = ( l + r ) >> 1;
build(2*nod,l,mi);
build(2*nod+1,mi+1,r);
st[nod] = join( st[2*nod] , st[2*nod+1] );
}
void update(int nod,int l,int r,int id){
if( l == r ){ st[nod] = { y[l] , x[l] }; return; }
int mi = ( l + r ) >> 1;
if( id <= mi ) update(2*nod,l,mi,id);
else update(2*nod+1,mi+1,r,id);
st[nod] = join( st[2*nod] , st[2*nod+1] );
}
//int queryx(int nod,int l,int r,int xx,int yy){
//
// if( l > yy || r < xx || l > r ) return 0;
//
// if( l == r ){
// if( st[nod].mx > 1 ) return l;
// return 0;
// }
//
// int mi = ( l + r ) >> 1;
//
// if( l >= xx && r <= yy ){
// if( st[2*nod+1].mx > 1 )
// return queryx(2*nod+1,mi+1,r,xx,yy);
// if( st[2*nod].mx > 1 )
// return queryx(2*nod,l,mi,xx,yy);
// return 0;
// }
//
// return max( queryx(2*nod,l,mi,xx,yy) , queryx(2*nod+1,mi+1,r,xx,yy) );
//}
node queryy(int nod,int l,int r,int xx,int yy){
if( l > yy || r < xx || l > r ) return { 0 , 1 };
if( l >= xx && r <= yy ) return st[nod];
int mi = ( l + r ) >> 1;
return join( queryy(2*nod,l,mi,xx,yy) , queryy(2*nod+1,mi+1,r,xx,yy) );
}
int solve(){
ll res = (ll)x[n] * y[n];//db(res)
int id = n;
bool moduled = 0;
if( res < mod ){
for(int i=1; i<=32; i++){
int nid;
if( x[id-1] > 1 ) nid = id - 1;
else{
auto pnid = s.lower_bound(id);
if( pnid == s.begin() ){
nid = 0;
}
else{
pnid --;
nid = *pnid;
}
}
//db(nid)db(id)
if( nid == 0 ) break;
node f;
if( nid + 1 != id ) f = queryy(1,1,n,nid,id-1);
else f = { y[nid] , x[nid] };
res = max( res , (ll)f.my );
res *= f.px;
id = nid;
if( res >= mod ){
moduled = 1;
res %= mod;
break;
}
}
}
else{
res %= mod;
moduled = 1;
}
node cc = queryy(1,1,n,1,id-1);
res *= cc.px;
if( res >= mod ){
res %= mod;
moduled = 1;
}
if( !moduled ){
res = max( res , (ll)cc.my );
}
return res;
}
int init(int N, int X[], int Y[]) {
n = N;
for(int i=0; i<N; i++){
x[i+1] = X[i];
y[i+1] = Y[i];
if( x[i+1] > 1 ) s.insert(i+1);
}
build(1,1,N);
return solve();
}
int updateX(int pos, int val) {
x[pos+1] = val;
if( x[pos+1] == 1 ) s.erase(pos+1);
if( x[pos+1] > 1 ) s.insert(pos+1);
update(1,1,n,pos+1);
return solve();
}
int updateY(int pos, int val) {
y[pos+1] = val;
if( x[pos+1] == 1 ) s.erase(pos+1);
if( x[pos+1] > 1 ) s.insert(pos+1);
update(1,1,n,pos+1);
return solve();
}
Compilation message
horses.cpp: In function 'node join(node, node)':
horses.cpp:19:51: warning: narrowing conversion of '((((long long int)a.node::px) * ((long long int)b.node::px)) % 1000000007)' from 'long long int' to 'int' [-Wnarrowing]
19 | return { max( a.my , b.my ) , (ll)a.px * b.px % mod };
| ^
horses.cpp:19:51: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
horses.cpp: In function 'int solve()':
horses.cpp:130:12: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
130 | return res;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
288 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
288 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
360 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
512 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
384 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
229 ms |
40956 KB |
Output is correct |
2 |
Correct |
391 ms |
41004 KB |
Output is correct |
3 |
Correct |
361 ms |
40952 KB |
Output is correct |
4 |
Correct |
372 ms |
41008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
416 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
2 ms |
444 KB |
Output is correct |
32 |
Correct |
6 ms |
416 KB |
Output is correct |
33 |
Correct |
47 ms |
16632 KB |
Output is correct |
34 |
Correct |
45 ms |
16724 KB |
Output is correct |
35 |
Correct |
197 ms |
40056 KB |
Output is correct |
36 |
Correct |
182 ms |
40040 KB |
Output is correct |
37 |
Correct |
155 ms |
16764 KB |
Output is correct |
38 |
Correct |
106 ms |
28632 KB |
Output is correct |
39 |
Correct |
45 ms |
16512 KB |
Output is correct |
40 |
Correct |
172 ms |
40104 KB |
Output is correct |
41 |
Correct |
31 ms |
16632 KB |
Output is correct |
42 |
Correct |
111 ms |
16548 KB |
Output is correct |
43 |
Correct |
159 ms |
39928 KB |
Output is correct |
44 |
Correct |
156 ms |
39984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
6 ms |
384 KB |
Output is correct |
28 |
Correct |
3 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
233 ms |
41336 KB |
Output is correct |
34 |
Correct |
394 ms |
41160 KB |
Output is correct |
35 |
Correct |
366 ms |
41244 KB |
Output is correct |
36 |
Correct |
379 ms |
41084 KB |
Output is correct |
37 |
Correct |
48 ms |
16888 KB |
Output is correct |
38 |
Correct |
45 ms |
16896 KB |
Output is correct |
39 |
Correct |
196 ms |
40184 KB |
Output is correct |
40 |
Correct |
190 ms |
40184 KB |
Output is correct |
41 |
Correct |
157 ms |
16888 KB |
Output is correct |
42 |
Correct |
112 ms |
28632 KB |
Output is correct |
43 |
Correct |
42 ms |
16640 KB |
Output is correct |
44 |
Correct |
169 ms |
40204 KB |
Output is correct |
45 |
Correct |
30 ms |
16760 KB |
Output is correct |
46 |
Correct |
108 ms |
16760 KB |
Output is correct |
47 |
Correct |
156 ms |
40056 KB |
Output is correct |
48 |
Correct |
157 ms |
40056 KB |
Output is correct |
49 |
Correct |
218 ms |
18808 KB |
Output is correct |
50 |
Correct |
151 ms |
18808 KB |
Output is correct |
51 |
Correct |
373 ms |
41316 KB |
Output is correct |
52 |
Correct |
252 ms |
41080 KB |
Output is correct |
53 |
Correct |
1170 ms |
19364 KB |
Output is correct |
54 |
Correct |
332 ms |
35400 KB |
Output is correct |
55 |
Correct |
198 ms |
19624 KB |
Output is correct |
56 |
Correct |
295 ms |
47736 KB |
Output is correct |
57 |
Correct |
96 ms |
20088 KB |
Output is correct |
58 |
Correct |
888 ms |
20840 KB |
Output is correct |
59 |
Correct |
165 ms |
46268 KB |
Output is correct |