#include "horses.h"
#include <bits/stdc++.h>
#define ll long long
#define pii pair<ll,ll>
#define x first
#define y second
#define MAXN 500015
#define mod 1000000007
using namespace std;
int dx[MAXN],dy[MAXN],n;
ll fast_pow(ll a,ll b){
ll ans=1;
while (b){
if (b&1) ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
struct segtree{
ll pos[4*MAXN],val[4*MAXN],val_x[4*MAXN],fac[4*MAXN];
pii remain[4*MAXN];
ll get_val(ll a=1,ll b=1,ll c=1,ll d=1){
vector<ll>vt={a,b,c,d};
ll ans=1;
for (ll i:vt){
ans=ans*i;
if (ans>1e9) return 1e9+2;
}
return ans;
}
void pull(int i){
fac[i]=fac[2*i]*fac[2*i+1]%mod;
if (get_val(remain[2*i].y,remain[2*i+1].x,val_x[2*i+1],val[2*i+1])>val[2*i]){
pos[i]=pos[2*i+1]; val[i]=val[2*i+1]; remain[i].y=remain[2*i+1].y; val_x[i]=val_x[2*i+1];
remain[i].x=get_val(remain[2*i].x,remain[2*i].y,val_x[2*i],remain[2*i+1].x);
}
else {
pos[i]=pos[2*i]; val[i]=val[2*i]; remain[i].x=remain[2*i].x; val_x[i]=val_x[2*i];
remain[i].y=get_val(remain[2*i].y,remain[2*i+1].x,remain[2*i+1].y,val_x[2*i+1]);
}
}
void build(int l,int r,int i){
if (l==r){
remain[i]={1,1};
pos[i]=l; val[i]=dy[l]; fac[i]=dx[l]; val_x[i]=dx[l];
return;
}
int mid=(l+r)>>1;
build(l,mid,2*i);
build(mid+1,r,2*i+1);
pull(i);
}
void modify_Y(int l,int r,int i,int p,ll c){
if (l==r){
val[i]=val[i]*c%mod;
return;
}
int mid=(l+r)>>1;
if (p<=mid) modify_Y(l,mid,2*i,p,c);
else modify_Y(mid+1,r,2*i+1,p,c);
pull(i);
}
void modify_X(int l,int r,int i,int p,ll c){
if (l==r){
fac[i]=fac[i]*c%mod;
val_x[i]=val_x[i]*c%mod;
return;
}
int mid=(l+r)>>1;
if (p<=mid) modify_X(l,mid,2*i,p,c);
else modify_X(mid+1,r,2*i+1,p,c);
pull(i);
}
ll query(int l,int r,int i,int ql,int qr){
if (ql<=l&&qr>=r) return fac[i];
int mid=(l+r)>>1;
if (qr<=mid) return query(l,mid,2*i,ql,qr);
else if (ql>mid) return query(mid+1,r,2*i+1,ql,qr);
else return query(l,mid,2*i,ql,qr)*query(mid+1,r,2*i+1,ql,qr)%mod;
}
void debug(int l,int r,int i){
cout<<l<<" ~ "<<r<<" : "<<fac[i]<<" best "<<pos[i]<<","<<val[i]<<","<<val_x[i]<<" remain : "<<remain[i].x<<' '<<remain[i].y<<'\n';
if (l==r) return;
int mid=(l+r)>>1;
debug(l,mid,2*i);
debug(mid+1,r,2*i+1);
}
}seg;
int init(int N, int X[], int Y[]) {
n=N;
for (int i=1;i<=n;i++){
dx[i]=X[i-1]; dy[i]=Y[i-1];
}
seg.build(1,n,1);
ll best=seg.pos[1];
ll ans=seg.query(1,n,1,1,best)*dy[best]%mod;
return ans;
}
int updateX(int pos, int val) {
pos++;
ll inv=fast_pow(dx[pos],mod-2);
dx[pos]=val;
seg.modify_X(1,n,1,pos,inv);
seg.modify_X(1,n,1,pos,val);
ll best=seg.pos[1];
ll ans=seg.query(1,n,1,1,best)*dy[best]%mod;
//cout<<"modify_X "<<best<<" "<<ans<<'\n';
//seg.debug(1,n,1);
return ans;
}
int updateY(int pos, int val) {
pos++;
ll inv=fast_pow(dy[pos],mod-2);
dy[pos]=val;
seg.modify_Y(1,n,1,pos,inv);
seg.modify_Y(1,n,1,pos,val);
ll best=seg.pos[1];
ll ans=seg.query(1,n,1,1,best)*dy[best]%mod;
//cout<<"modify_Y "<<best<<" "<<ans<<'\n';
//seg.debug(1,n,1);
return ans;
}
Compilation message
horses.cpp: In member function 'long long int segtree::get_val(long long int, long long int, long long int, long long int)':
horses.cpp:29:8: warning: conversion from 'long long int' to 'double' may change value [-Wconversion]
29 | if (ans>1e9) return 1e9+2;
| ^~~
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:98:27: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
98 | ll ans=seg.query(1,n,1,1,best)*dy[best]%mod;
| ^~~~
horses.cpp:99:9: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
99 | return ans;
| ^~~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:109:27: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
109 | ll ans=seg.query(1,n,1,1,best)*dy[best]%mod;
| ^~~~
horses.cpp:112:9: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
112 | return ans;
| ^~~
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:122:27: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
122 | ll ans=seg.query(1,n,1,1,best)*dy[best]%mod;
| ^~~~
horses.cpp:125:9: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
125 | return ans;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
31644 KB |
Output is correct |
2 |
Correct |
12 ms |
31600 KB |
Output is correct |
3 |
Correct |
13 ms |
31648 KB |
Output is correct |
4 |
Correct |
13 ms |
31640 KB |
Output is correct |
5 |
Correct |
13 ms |
31572 KB |
Output is correct |
6 |
Correct |
13 ms |
31572 KB |
Output is correct |
7 |
Correct |
13 ms |
31656 KB |
Output is correct |
8 |
Correct |
13 ms |
31628 KB |
Output is correct |
9 |
Correct |
13 ms |
31544 KB |
Output is correct |
10 |
Correct |
13 ms |
31540 KB |
Output is correct |
11 |
Correct |
13 ms |
31652 KB |
Output is correct |
12 |
Correct |
13 ms |
31584 KB |
Output is correct |
13 |
Correct |
12 ms |
31572 KB |
Output is correct |
14 |
Correct |
13 ms |
31540 KB |
Output is correct |
15 |
Correct |
13 ms |
31652 KB |
Output is correct |
16 |
Correct |
13 ms |
31572 KB |
Output is correct |
17 |
Correct |
13 ms |
31652 KB |
Output is correct |
18 |
Correct |
13 ms |
31572 KB |
Output is correct |
19 |
Correct |
14 ms |
31692 KB |
Output is correct |
20 |
Correct |
14 ms |
31628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
31636 KB |
Output is correct |
2 |
Correct |
13 ms |
31576 KB |
Output is correct |
3 |
Correct |
13 ms |
31572 KB |
Output is correct |
4 |
Correct |
13 ms |
31572 KB |
Output is correct |
5 |
Correct |
13 ms |
31572 KB |
Output is correct |
6 |
Correct |
13 ms |
31572 KB |
Output is correct |
7 |
Correct |
13 ms |
31572 KB |
Output is correct |
8 |
Correct |
12 ms |
31572 KB |
Output is correct |
9 |
Correct |
13 ms |
31572 KB |
Output is correct |
10 |
Correct |
13 ms |
31572 KB |
Output is correct |
11 |
Correct |
13 ms |
31624 KB |
Output is correct |
12 |
Correct |
13 ms |
31572 KB |
Output is correct |
13 |
Correct |
15 ms |
31588 KB |
Output is correct |
14 |
Correct |
13 ms |
31620 KB |
Output is correct |
15 |
Correct |
13 ms |
31572 KB |
Output is correct |
16 |
Correct |
13 ms |
31572 KB |
Output is correct |
17 |
Correct |
13 ms |
31600 KB |
Output is correct |
18 |
Correct |
13 ms |
31592 KB |
Output is correct |
19 |
Correct |
13 ms |
31572 KB |
Output is correct |
20 |
Correct |
12 ms |
31572 KB |
Output is correct |
21 |
Correct |
13 ms |
31572 KB |
Output is correct |
22 |
Correct |
13 ms |
31624 KB |
Output is correct |
23 |
Correct |
14 ms |
31692 KB |
Output is correct |
24 |
Correct |
14 ms |
31716 KB |
Output is correct |
25 |
Correct |
14 ms |
31748 KB |
Output is correct |
26 |
Correct |
15 ms |
31728 KB |
Output is correct |
27 |
Correct |
14 ms |
31700 KB |
Output is correct |
28 |
Correct |
18 ms |
31640 KB |
Output is correct |
29 |
Correct |
15 ms |
31648 KB |
Output is correct |
30 |
Correct |
15 ms |
31768 KB |
Output is correct |
31 |
Correct |
15 ms |
31728 KB |
Output is correct |
32 |
Correct |
15 ms |
31764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
282 ms |
73280 KB |
Output is correct |
2 |
Correct |
462 ms |
85824 KB |
Output is correct |
3 |
Correct |
363 ms |
77020 KB |
Output is correct |
4 |
Correct |
378 ms |
80944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
31572 KB |
Output is correct |
2 |
Correct |
13 ms |
31584 KB |
Output is correct |
3 |
Correct |
13 ms |
31572 KB |
Output is correct |
4 |
Correct |
14 ms |
31572 KB |
Output is correct |
5 |
Correct |
13 ms |
31652 KB |
Output is correct |
6 |
Correct |
15 ms |
31572 KB |
Output is correct |
7 |
Correct |
14 ms |
31572 KB |
Output is correct |
8 |
Correct |
13 ms |
31572 KB |
Output is correct |
9 |
Correct |
13 ms |
31608 KB |
Output is correct |
10 |
Correct |
12 ms |
31572 KB |
Output is correct |
11 |
Correct |
13 ms |
31572 KB |
Output is correct |
12 |
Correct |
12 ms |
31572 KB |
Output is correct |
13 |
Correct |
14 ms |
31632 KB |
Output is correct |
14 |
Correct |
12 ms |
31572 KB |
Output is correct |
15 |
Correct |
13 ms |
31564 KB |
Output is correct |
16 |
Correct |
13 ms |
31572 KB |
Output is correct |
17 |
Correct |
13 ms |
31548 KB |
Output is correct |
18 |
Correct |
13 ms |
31572 KB |
Output is correct |
19 |
Correct |
12 ms |
31564 KB |
Output is correct |
20 |
Correct |
12 ms |
31572 KB |
Output is correct |
21 |
Correct |
12 ms |
31572 KB |
Output is correct |
22 |
Correct |
13 ms |
31656 KB |
Output is correct |
23 |
Correct |
15 ms |
31768 KB |
Output is correct |
24 |
Correct |
15 ms |
31700 KB |
Output is correct |
25 |
Correct |
15 ms |
31772 KB |
Output is correct |
26 |
Correct |
14 ms |
31700 KB |
Output is correct |
27 |
Correct |
17 ms |
31708 KB |
Output is correct |
28 |
Correct |
17 ms |
31700 KB |
Output is correct |
29 |
Correct |
14 ms |
31664 KB |
Output is correct |
30 |
Correct |
14 ms |
31644 KB |
Output is correct |
31 |
Correct |
14 ms |
31700 KB |
Output is correct |
32 |
Correct |
14 ms |
31736 KB |
Output is correct |
33 |
Correct |
114 ms |
76224 KB |
Output is correct |
34 |
Correct |
109 ms |
76288 KB |
Output is correct |
35 |
Correct |
118 ms |
83232 KB |
Output is correct |
36 |
Correct |
116 ms |
83120 KB |
Output is correct |
37 |
Correct |
97 ms |
74456 KB |
Output is correct |
38 |
Correct |
91 ms |
75368 KB |
Output is correct |
39 |
Correct |
89 ms |
74376 KB |
Output is correct |
40 |
Correct |
100 ms |
78260 KB |
Output is correct |
41 |
Correct |
88 ms |
74368 KB |
Output is correct |
42 |
Correct |
92 ms |
74584 KB |
Output is correct |
43 |
Correct |
87 ms |
78568 KB |
Output is correct |
44 |
Correct |
84 ms |
78668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
31572 KB |
Output is correct |
2 |
Correct |
13 ms |
31572 KB |
Output is correct |
3 |
Correct |
13 ms |
31664 KB |
Output is correct |
4 |
Correct |
13 ms |
31628 KB |
Output is correct |
5 |
Correct |
13 ms |
31604 KB |
Output is correct |
6 |
Correct |
13 ms |
31572 KB |
Output is correct |
7 |
Correct |
15 ms |
31656 KB |
Output is correct |
8 |
Correct |
13 ms |
31628 KB |
Output is correct |
9 |
Correct |
15 ms |
31572 KB |
Output is correct |
10 |
Correct |
13 ms |
31572 KB |
Output is correct |
11 |
Correct |
13 ms |
31636 KB |
Output is correct |
12 |
Correct |
13 ms |
31556 KB |
Output is correct |
13 |
Correct |
13 ms |
31572 KB |
Output is correct |
14 |
Correct |
12 ms |
31572 KB |
Output is correct |
15 |
Correct |
13 ms |
31572 KB |
Output is correct |
16 |
Correct |
13 ms |
31632 KB |
Output is correct |
17 |
Correct |
13 ms |
31572 KB |
Output is correct |
18 |
Correct |
12 ms |
31572 KB |
Output is correct |
19 |
Correct |
13 ms |
31652 KB |
Output is correct |
20 |
Correct |
13 ms |
31572 KB |
Output is correct |
21 |
Correct |
13 ms |
31588 KB |
Output is correct |
22 |
Correct |
13 ms |
31572 KB |
Output is correct |
23 |
Correct |
15 ms |
31696 KB |
Output is correct |
24 |
Correct |
15 ms |
31700 KB |
Output is correct |
25 |
Correct |
15 ms |
31700 KB |
Output is correct |
26 |
Correct |
14 ms |
31700 KB |
Output is correct |
27 |
Correct |
14 ms |
31700 KB |
Output is correct |
28 |
Correct |
15 ms |
31652 KB |
Output is correct |
29 |
Correct |
15 ms |
31636 KB |
Output is correct |
30 |
Correct |
14 ms |
31700 KB |
Output is correct |
31 |
Correct |
19 ms |
31644 KB |
Output is correct |
32 |
Correct |
14 ms |
31704 KB |
Output is correct |
33 |
Correct |
302 ms |
77144 KB |
Output is correct |
34 |
Correct |
389 ms |
85744 KB |
Output is correct |
35 |
Correct |
368 ms |
77032 KB |
Output is correct |
36 |
Correct |
422 ms |
80792 KB |
Output is correct |
37 |
Correct |
106 ms |
76236 KB |
Output is correct |
38 |
Correct |
107 ms |
76228 KB |
Output is correct |
39 |
Correct |
118 ms |
83216 KB |
Output is correct |
40 |
Correct |
130 ms |
83168 KB |
Output is correct |
41 |
Correct |
102 ms |
74468 KB |
Output is correct |
42 |
Correct |
92 ms |
75312 KB |
Output is correct |
43 |
Correct |
88 ms |
74352 KB |
Output is correct |
44 |
Correct |
107 ms |
78332 KB |
Output is correct |
45 |
Correct |
87 ms |
74316 KB |
Output is correct |
46 |
Correct |
88 ms |
74480 KB |
Output is correct |
47 |
Correct |
81 ms |
78636 KB |
Output is correct |
48 |
Correct |
88 ms |
78572 KB |
Output is correct |
49 |
Correct |
393 ms |
78276 KB |
Output is correct |
50 |
Correct |
402 ms |
78312 KB |
Output is correct |
51 |
Correct |
351 ms |
85108 KB |
Output is correct |
52 |
Correct |
403 ms |
84576 KB |
Output is correct |
53 |
Correct |
413 ms |
76680 KB |
Output is correct |
54 |
Correct |
312 ms |
77136 KB |
Output is correct |
55 |
Correct |
319 ms |
75452 KB |
Output is correct |
56 |
Correct |
324 ms |
80076 KB |
Output is correct |
57 |
Correct |
318 ms |
76000 KB |
Output is correct |
58 |
Correct |
318 ms |
76476 KB |
Output is correct |
59 |
Correct |
96 ms |
78580 KB |
Output is correct |