#include "horses.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll m = 1000000007;
ll mexp(ll a, ll b){
if (b==0){
return 1;
}
ll ans = mexp(a,b/2);
if (b%2==0){
return (ans*ans)%m;
} else {
return (((ans*ans)%m)*a)%m;
}
}
ll minv(ll x){
return mexp(x,m-2);
}
ll mdiv(ll a, ll b){
//a div b mod m
return (a*minv(b))%m;
}
//range maximum query
//4.14.20
struct Node{
int s,e,m,v;
Node *l, *r;
Node(int _s, int _e, int arr[]){
s=_s;e=_e;
if (s==e){
v=arr[s];
} else {
m=(s+e)/2;
l=new Node(s,m,arr);
r=new Node(m+1,e,arr);
v=max(l->v,r->v);
}
}
void update(int ind, int val){
if (s==e){
v=val;
return;
}
if (ind<=m){
l->update(ind,val);
} else {
r->update(ind,val);
}
v=max(l->v,r->v);
}
int query(int a, int b){
if (s==a&&e==b){
return v;
}
if (b<=m){
return l->query(a,b);
} else if (m<a){
return r->query(a,b);
} else {
return max(l->query(a,m),r->query(m+1,b));
}
}
};
//4.11.56
Node *root;
int n;
int *xvals;
int *yvals;
set<pair<int,int>> xset;
ll xprod = 1;
int query(){
auto it = prev(xset.end());
ll valdiv = 1; //value 'lost' from xvals suffix
ll maxfracn = 1;
ll maxfracd = 1;
while (true){
if (it->first==0){break;}
int mincrd = (it==xset.begin())?0:(prev(it)->first);
int maxcrd = it->first-1;
//cout<<mincrd<<' '<<maxcrd<<'\n';
int maxy = root->query(mincrd,maxcrd);
//cout<<maxy<<'\n';
valdiv*=it->second;
if (valdiv>m){
break;
}
ll newfracn = maxy;
ll newfracd = valdiv;
if (newfracn*maxfracd>maxfracn*newfracd){
maxfracn=newfracn;
maxfracd=newfracd;
}
if (it==xset.begin()){break;}
it = prev(it);
}
//cout<<"f "<<maxfracn<<' '<<maxfracd<<'\n';
return (((xprod*maxfracn)%m)*minv(maxfracd))%m;
}
int init(int N, int X[], int Y[]) {
n=N;
root = new Node(0,n-1,Y);
xvals=X;
yvals=Y;
for (int i = 0; i<n; i++){
xprod*=xvals[i];
xprod%=m;
if (xvals[i]!=1){
xset.insert({i,xvals[i]});
}
}
xset.insert({n,1});
return query();
}
int updateX(int pos, int val) {
if (xvals[pos]!=1){
xset.erase({pos,xvals[pos]});
}
xprod=mdiv(xprod,xvals[pos]);
xvals[pos]=val;
if (val!=1){
xset.insert({pos,xvals[pos]});
}
xprod*=xvals[pos];
xprod%=m;
return query();
}
int updateY(int pos, int val) {
yvals[pos]=val;
root->update(pos,val);
return query();
}
Compilation message
horses.cpp: In function 'int query()':
horses.cpp:114:49: warning: conversion to 'int' from 'll {aka long long int}' may alter its value [-Wconversion]
return (((xprod*maxfracn)%m)*minv(maxfracd))%m;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
372 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
1 ms |
256 KB |
Output is correct |
21 |
Correct |
1 ms |
256 KB |
Output is correct |
22 |
Correct |
0 ms |
256 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
2 ms |
512 KB |
Output is correct |
27 |
Correct |
2 ms |
512 KB |
Output is correct |
28 |
Correct |
2 ms |
512 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
512 KB |
Output is correct |
31 |
Correct |
2 ms |
512 KB |
Output is correct |
32 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
481 ms |
79736 KB |
Output is correct |
2 |
Correct |
435 ms |
88312 KB |
Output is correct |
3 |
Correct |
430 ms |
79864 KB |
Output is correct |
4 |
Correct |
483 ms |
83320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
0 ms |
256 KB |
Output is correct |
21 |
Correct |
0 ms |
256 KB |
Output is correct |
22 |
Correct |
0 ms |
256 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
2 ms |
512 KB |
Output is correct |
27 |
Correct |
2 ms |
512 KB |
Output is correct |
28 |
Correct |
2 ms |
512 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
512 KB |
Output is correct |
31 |
Correct |
2 ms |
512 KB |
Output is correct |
32 |
Correct |
2 ms |
512 KB |
Output is correct |
33 |
Correct |
88 ms |
55416 KB |
Output is correct |
34 |
Correct |
84 ms |
55416 KB |
Output is correct |
35 |
Correct |
232 ms |
85672 KB |
Output is correct |
36 |
Correct |
226 ms |
85624 KB |
Output is correct |
37 |
Correct |
96 ms |
53496 KB |
Output is correct |
38 |
Correct |
136 ms |
66296 KB |
Output is correct |
39 |
Correct |
69 ms |
53240 KB |
Output is correct |
40 |
Correct |
208 ms |
80860 KB |
Output is correct |
41 |
Correct |
80 ms |
53372 KB |
Output is correct |
42 |
Correct |
89 ms |
53448 KB |
Output is correct |
43 |
Correct |
200 ms |
81144 KB |
Output is correct |
44 |
Correct |
203 ms |
81020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
256 KB |
Output is correct |
22 |
Correct |
0 ms |
256 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
2 ms |
512 KB |
Output is correct |
27 |
Correct |
2 ms |
512 KB |
Output is correct |
28 |
Correct |
2 ms |
512 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
512 KB |
Output is correct |
31 |
Correct |
2 ms |
512 KB |
Output is correct |
32 |
Correct |
2 ms |
512 KB |
Output is correct |
33 |
Correct |
474 ms |
79556 KB |
Output is correct |
34 |
Correct |
432 ms |
88312 KB |
Output is correct |
35 |
Correct |
416 ms |
79724 KB |
Output is correct |
36 |
Correct |
489 ms |
83448 KB |
Output is correct |
37 |
Correct |
80 ms |
55416 KB |
Output is correct |
38 |
Correct |
81 ms |
55416 KB |
Output is correct |
39 |
Correct |
234 ms |
85624 KB |
Output is correct |
40 |
Correct |
228 ms |
85752 KB |
Output is correct |
41 |
Correct |
102 ms |
53496 KB |
Output is correct |
42 |
Correct |
136 ms |
66296 KB |
Output is correct |
43 |
Correct |
73 ms |
53240 KB |
Output is correct |
44 |
Correct |
211 ms |
80760 KB |
Output is correct |
45 |
Correct |
85 ms |
53368 KB |
Output is correct |
46 |
Correct |
91 ms |
53368 KB |
Output is correct |
47 |
Correct |
201 ms |
81016 KB |
Output is correct |
48 |
Correct |
201 ms |
81144 KB |
Output is correct |
49 |
Correct |
233 ms |
58360 KB |
Output is correct |
50 |
Correct |
214 ms |
58360 KB |
Output is correct |
51 |
Correct |
364 ms |
87544 KB |
Output is correct |
52 |
Correct |
321 ms |
87076 KB |
Output is correct |
53 |
Correct |
397 ms |
56696 KB |
Output is correct |
54 |
Correct |
273 ms |
70264 KB |
Output is correct |
55 |
Correct |
193 ms |
54392 KB |
Output is correct |
56 |
Correct |
345 ms |
82552 KB |
Output is correct |
57 |
Correct |
296 ms |
55032 KB |
Output is correct |
58 |
Correct |
365 ms |
55544 KB |
Output is correct |
59 |
Correct |
201 ms |
81144 KB |
Output is correct |