#include "horses.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll md = 1000000007;
vector<ll> x;
vector<ll> y;
ll n;
//overall maximum: prefix * maxy
struct Data{
ll prefix=1; //prefix inclusive of max
bool prefixgmd=false;
ll suffix=1;
bool suffixgmd=false;
ll maxy=1;
};
Data combine(Data l, Data r){
//first determine which has maximum
Data ans;
//cout<<"combine"<<endl;
//cout<<l.prefix<<" "<<l.prefixgmd<<" "<<l.suffix<<" "<<l.suffixgmd<<" "<<l.maxy<<endl;
//cout<<r.prefix<<" "<<r.prefixgmd<<" "<<r.suffix<<" "<<r.suffixgmd<<" "<<r.maxy<<endl;
if (l.suffixgmd||r.prefixgmd||l.suffix*r.prefix>=md){
//r has maximum
ans.prefix=(((l.prefix*l.suffix)%md)*r.prefix)%md;
ans.prefixgmd=true;
ans.suffix=r.suffix;
ans.suffixgmd=r.suffixgmd;
ans.maxy=r.maxy;
} else {
ll rval = l.suffix*r.prefix*r.maxy;
ll lval = l.maxy;
if (rval>lval){
//r has maximum
ans.maxy=r.maxy;
if (l.prefixgmd){
ans.prefixgmd=true;
ans.prefix=(((l.prefix*l.suffix)%md)*r.prefix)%md;
} else {
ans.prefix=(l.prefix*l.suffix);
if (ans.prefix>=md){
ans.prefix%=md;
ans.prefix*=r.prefix;
ans.prefix%=md;
ans.prefixgmd=true;
} else {
ans.prefix*=r.prefix;
if (ans.prefix>=md){
ans.prefix%=md;
ans.prefixgmd=true;
} else {
ans.prefixgmd=false;
}
}
ans.prefix%=md;
}
ans.suffix=r.suffix;
ans.suffixgmd=r.suffixgmd;
} else {
//l has maximum
ans.maxy=l.maxy;
ans.prefixgmd=l.prefixgmd;
ans.prefix=l.prefix;
if (r.suffixgmd){
ans.suffixgmd=true;
ans.suffix=(((l.suffix*r.prefix)%md)*r.suffix)%md;
} else {
ans.suffix=(l.suffix*r.prefix);
if (ans.suffix>=md){
ans.suffix%=md;
ans.suffix*=r.suffix;
ans.suffix%=md;
ans.suffixgmd=true;
} else {
ans.suffix*=r.suffix;
if (ans.suffix>=md){
ans.suffix%=md;
ans.suffixgmd=true;
} else {
ans.suffixgmd=false;
}
}
ans.suffix%=md;
}
}
}
//cout<<ans.prefix<<" "<<ans.prefixgmd<<" "<<ans.suffix<<" "<<ans.suffixgmd<<" "<<ans.maxy<<endl;
return ans;
}
struct Node{
ll s,e,m;
Data d;
Node *l, *r;
Node(ll _s, ll _e){
s=_s;e=_e;
m=(s+e)/2;
if (s==e){
d.prefix=x[s];
d.maxy=y[s];
return;
}
l=new Node(s,m);
r=new Node(m+1,e);
d=combine(l->d,r->d);
}
void update(ll pos){
if (s==e){
d.prefix=x[pos];
d.maxy=y[pos];
d.suffix=1;
d.prefixgmd=false;
d.suffixgmd=false;
return;
}
if (pos<=m){
l->update(pos);
} else {
r->update(pos);
}
d=combine(l->d,r->d);
}
Data query(ll a, ll b){
if (a==s&&b==e){
return d;
}
if (b<=m){
return l->query(a,b);
} else if (m<a){
return r->query(a,b);
} else {
return combine(l->query(a,m),r->query(m+1,b));
}
}
};
Node *root;
int init(int N, int X[], int Y[]) {
n=N;
x=vector<ll>(X,X+n);
y=vector<ll>(Y,Y+n);
root = new Node(0,n-1);
Data d = root->query(0,n-1);
ll ans = (d.prefix*d.maxy)%md;
return ans;
}
int updateX(int pos, int val) {
//cout<<"x"<<endl;
x[pos]=val;
root->update(pos);
Data d = root->query(0,n-1);
ll ans = (d.prefix*d.maxy)%md;
return ans;
}
int updateY(int pos, int val) {
//cout<<"y"<<endl;
y[pos]=val;
root->update(pos);
Data d = root->query(0,n-1);
ll ans = (d.prefix*d.maxy)%md;
return ans;
}
Compilation message
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:147:9: warning: conversion to 'int' from 'll {aka long long int}' may alter its value [-Wconversion]
return ans;
^~~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:156:9: warning: conversion to 'int' from 'll {aka long long int}' may alter its value [-Wconversion]
return ans;
^~~
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:165:9: warning: conversion to 'int' from 'll {aka long long int}' may alter its value [-Wconversion]
return ans;
^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
256 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 |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
256 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 |
256 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
252 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
256 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 |
252 KB |
Output is correct |
12 |
Correct |
3 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
2 ms |
256 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
256 KB |
Output is correct |
20 |
Correct |
2 ms |
256 KB |
Output is correct |
21 |
Correct |
2 ms |
256 KB |
Output is correct |
22 |
Correct |
26 ms |
348 KB |
Output is correct |
23 |
Correct |
3 ms |
504 KB |
Output is correct |
24 |
Correct |
3 ms |
632 KB |
Output is correct |
25 |
Correct |
3 ms |
504 KB |
Output is correct |
26 |
Correct |
3 ms |
504 KB |
Output is correct |
27 |
Correct |
4 ms |
504 KB |
Output is correct |
28 |
Correct |
3 ms |
632 KB |
Output is correct |
29 |
Correct |
3 ms |
504 KB |
Output is correct |
30 |
Correct |
3 ms |
504 KB |
Output is correct |
31 |
Correct |
3 ms |
504 KB |
Output is correct |
32 |
Correct |
3 ms |
508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
235 ms |
107780 KB |
Output is correct |
2 |
Correct |
435 ms |
108664 KB |
Output is correct |
3 |
Correct |
402 ms |
107860 KB |
Output is correct |
4 |
Correct |
428 ms |
114672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 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 |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
256 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 |
252 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
300 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 |
256 KB |
Output is correct |
22 |
Correct |
2 ms |
256 KB |
Output is correct |
23 |
Correct |
3 ms |
504 KB |
Output is correct |
24 |
Correct |
3 ms |
504 KB |
Output is correct |
25 |
Correct |
3 ms |
504 KB |
Output is correct |
26 |
Correct |
3 ms |
504 KB |
Output is correct |
27 |
Correct |
3 ms |
504 KB |
Output is correct |
28 |
Correct |
3 ms |
632 KB |
Output is correct |
29 |
Correct |
3 ms |
504 KB |
Output is correct |
30 |
Correct |
3 ms |
636 KB |
Output is correct |
31 |
Correct |
3 ms |
632 KB |
Output is correct |
32 |
Correct |
3 ms |
632 KB |
Output is correct |
33 |
Correct |
186 ms |
110168 KB |
Output is correct |
34 |
Correct |
178 ms |
110072 KB |
Output is correct |
35 |
Correct |
183 ms |
117004 KB |
Output is correct |
36 |
Correct |
189 ms |
116984 KB |
Output is correct |
37 |
Correct |
163 ms |
108196 KB |
Output is correct |
38 |
Correct |
154 ms |
109176 KB |
Output is correct |
39 |
Correct |
145 ms |
108132 KB |
Output is correct |
40 |
Correct |
171 ms |
111992 KB |
Output is correct |
41 |
Correct |
146 ms |
108176 KB |
Output is correct |
42 |
Correct |
170 ms |
108152 KB |
Output is correct |
43 |
Correct |
158 ms |
112432 KB |
Output is correct |
44 |
Correct |
158 ms |
112376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
396 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 |
256 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 |
256 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 |
640 KB |
Output is correct |
24 |
Correct |
3 ms |
632 KB |
Output is correct |
25 |
Correct |
3 ms |
632 KB |
Output is correct |
26 |
Correct |
3 ms |
504 KB |
Output is correct |
27 |
Correct |
3 ms |
632 KB |
Output is correct |
28 |
Correct |
3 ms |
636 KB |
Output is correct |
29 |
Correct |
3 ms |
504 KB |
Output is correct |
30 |
Correct |
3 ms |
504 KB |
Output is correct |
31 |
Correct |
3 ms |
692 KB |
Output is correct |
32 |
Correct |
3 ms |
504 KB |
Output is correct |
33 |
Correct |
233 ms |
110840 KB |
Output is correct |
34 |
Correct |
443 ms |
119676 KB |
Output is correct |
35 |
Correct |
418 ms |
110840 KB |
Output is correct |
36 |
Correct |
422 ms |
114780 KB |
Output is correct |
37 |
Correct |
182 ms |
110192 KB |
Output is correct |
38 |
Correct |
177 ms |
110056 KB |
Output is correct |
39 |
Correct |
182 ms |
116984 KB |
Output is correct |
40 |
Correct |
185 ms |
116928 KB |
Output is correct |
41 |
Correct |
158 ms |
108176 KB |
Output is correct |
42 |
Correct |
151 ms |
109152 KB |
Output is correct |
43 |
Correct |
143 ms |
108152 KB |
Output is correct |
44 |
Correct |
166 ms |
112176 KB |
Output is correct |
45 |
Correct |
143 ms |
108104 KB |
Output is correct |
46 |
Correct |
142 ms |
108184 KB |
Output is correct |
47 |
Correct |
156 ms |
112328 KB |
Output is correct |
48 |
Correct |
156 ms |
112476 KB |
Output is correct |
49 |
Correct |
399 ms |
112164 KB |
Output is correct |
50 |
Correct |
406 ms |
112120 KB |
Output is correct |
51 |
Correct |
268 ms |
118932 KB |
Output is correct |
52 |
Correct |
295 ms |
118396 KB |
Output is correct |
53 |
Correct |
380 ms |
110680 KB |
Output is correct |
54 |
Correct |
263 ms |
110840 KB |
Output is correct |
55 |
Correct |
221 ms |
109404 KB |
Output is correct |
56 |
Correct |
267 ms |
113796 KB |
Output is correct |
57 |
Correct |
223 ms |
109704 KB |
Output is correct |
58 |
Correct |
225 ms |
110428 KB |
Output is correct |
59 |
Correct |
158 ms |
112376 KB |
Output is correct |