This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#include "horses.h"
//#include "grader.cpp"
#define ll long long
ll MOD=1000000007;
ll n,x[500005],y[500005],mul[2000005],ans[2000005];
void build_mul(ll tree,ll st,ll ed)
{
ll md=(st+ed)/2;
if(st==ed)
{
mul[tree]=x[st];
ans[tree]=x[st];
return;
}
build_mul(2*tree,st,md);
build_mul(2*tree+1,md+1,ed);
ans[tree]=(ans[2*tree]*ans[2*tree+1])%MOD;
if(mul[2*tree]==-1||mul[2*tree+1]==-1)
{
mul[tree]=-1;
}else if(mul[2*tree]*mul[2*tree+1]>1e9)
{
mul[tree]=-1;
}else
{
mul[tree]=mul[2*tree]*mul[2*tree+1];
}
}
void update_mul(ll tree,ll st,ll ed,ll idx,ll val)
{
ll md=(st+ed)/2;
if(st==ed)
{
mul[tree]=val;
ans[tree]=val;
return;
}
if(idx<=md)
{
update_mul(2*tree,st,md,idx,val);
}else
{
update_mul(2*tree+1,md+1,ed,idx,val);
}
ans[tree]=(ans[2*tree]*ans[2*tree+1])%MOD;
if(mul[2*tree]==-1||mul[2*tree+1]==-1)
{
mul[tree]=-1;
}else if(mul[2*tree]*mul[2*tree+1]>1e9)
{
mul[tree]=-1;
}else
{
mul[tree]=mul[2*tree]*mul[2*tree+1];
}
}
ll query_mul(ll tree,ll st,ll ed,ll l,ll r)
{
ll md=(st+ed)/2;
if(st>r||ed<l)return 1;
if(st>=l&&ed<=r)return mul[tree];
ll lft=query_mul(2*tree,st,md,l,r),rht=query_mul(2*tree+1,md+1,ed,l,r);
if(lft==-1||rht==-1)return -1;
if(lft*rht>1e9)return -1;
return lft*rht;
}
ll query_ans(ll tree,ll st,ll ed,ll l,ll r)
{
ll md=(st+ed)/2;
if(st>r||ed<l)return 1;
if(st>=l&&ed<=r)return ans[tree];
return (query_ans(2*tree,st,md,l,r)*query_ans(2*tree+1,md+1,ed,l,r))%MOD;
}
ll seg[2000005];
void build(ll tree,ll st,ll ed)
{
ll md=(st+ed)/2;
if(st==ed)
{
seg[tree]=st;
return;
}
build(2*tree,st,md);
build(2*tree+1,md+1,ed);
ll l_idx=seg[2*tree],r_idx=seg[2*tree+1];
ll rng=query_mul(1,1,n,l_idx+1,r_idx);
if(rng!=-1)
{
if(rng*y[r_idx]>1e9)
{
rng=-1;
}else
{
rng*=y[r_idx];
}
}
if(rng==-1)
{
seg[tree]=r_idx;
}else
{
if(rng>y[l_idx])
{
seg[tree]=r_idx;
}else
{
seg[tree]=l_idx;
}
}
}
void update(ll tree,ll st,ll ed,ll idx)
{
ll md=(st+ed)/2;
if(st==ed)
{
seg[tree]=idx;
return;
}
if(idx<=md)
{
update(2*tree,st,md,idx);
}else
{
update(2*tree+1,md+1,ed,idx);
}
ll l_idx=seg[2*tree],r_idx=seg[2*tree+1];
ll rng=query_mul(1,1,n,l_idx+1,r_idx);
if(rng!=-1)
{
if(rng*y[r_idx]>1e9)
{
rng=-1;
}else
{
rng*=y[r_idx];
}
}
if(rng==-1)
{
seg[tree]=r_idx;
}else
{
if(rng>y[l_idx])
{
seg[tree]=r_idx;
}else
{
seg[tree]=l_idx;
}
}
}
void debug(ll tree,ll st,ll ed)
{
ll md=(st+ed)/2;
if(st==ed)
{
printf("%lld %lld %lld\n",st,ed,mul[tree]);
return;
}
debug(2*tree,st,md);
debug(2*tree+1,md+1,ed);
printf("%lld %lld %lld\n",st,ed,mul[tree]);
}
void debug_seg(ll tree,ll st,ll ed)
{
ll md=(st+ed)/2;
if(st==ed)
{
printf("%lld %lld %lld\n",st,ed,seg[tree]);
return;
}
debug_seg(2*tree,st,md);
debug_seg(2*tree+1,md+1,ed);
printf("%lld %lld %lld\n",st,ed,seg[tree]);
}
int init(int N, int X[], int Y[])
{
n=N;for(int i=1;i<=n;i++)x[i]=X[i-1],y[i]=Y[i-1];
/*for(int i=1;i<=n;i++)
{
printf("%lld ",ans[i]);
}
printf("\n");*/
build_mul(1,1,n);
build(1,1,n);
//debug_seg(1,1,n);
//debug(1,1,n);
//printf("%lld\n",mul[1]);
return (query_ans(1,1,n,1,seg[1])*y[seg[1]])%MOD;
}
int updateX(int pos, int val)
{
++pos;
//printf("krit3379 %lld %lld\n",pos,val);
x[pos]=val;
update_mul(1,1,n,pos,val);
update(1,1,n,pos);
//debug(1,1,n);
//debug_seg(1,1,n);
return (query_ans(1,1,n,1,seg[1])*y[seg[1]])%MOD;
}
int updateY(int pos, int val)
{
++pos;
//printf("Jomnoi %lld %lld\n",pos,val);
y[pos]=val;
update(1,1,n,pos);
//debug_seg(1,1,n);
return (query_ans(1,1,n,1,seg[1])*y[seg[1]])%MOD;
}
Compilation message (stderr)
horses.cpp: In function 'void build_mul(long long int, long long int, long long int)':
horses.cpp:23:25: warning: conversion from 'long long int' to 'double' may change value [-Wconversion]
23 | }else if(mul[2*tree]*mul[2*tree+1]>1e9)
| ~~~~~~~~~~~^~~~~~~~~~~~~~
horses.cpp: In function 'void update_mul(long long int, long long int, long long int, long long int, long long int)':
horses.cpp:51:25: warning: conversion from 'long long int' to 'double' may change value [-Wconversion]
51 | }else if(mul[2*tree]*mul[2*tree+1]>1e9)
| ~~~~~~~~~~~^~~~~~~~~~~~~~
horses.cpp: In function 'long long int query_mul(long long int, long long int, long long int, long long int, long long int)':
horses.cpp:66:11: warning: conversion from 'long long int' to 'double' may change value [-Wconversion]
66 | if(lft*rht>1e9)return -1;
| ~~~^~~~
horses.cpp: In function 'void build(long long int, long long int, long long int)':
horses.cpp:91:15: warning: conversion from 'long long int' to 'double' may change value [-Wconversion]
91 | if(rng*y[r_idx]>1e9)
| ~~~^~~~~~~~~
horses.cpp: In function 'void update(long long int, long long int, long long int, long long int)':
horses.cpp:132:15: warning: conversion from 'long long int' to 'double' may change value [-Wconversion]
132 | if(rng*y[r_idx]>1e9)
| ~~~^~~~~~~~~
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:191:49: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
191 | return (query_ans(1,1,n,1,seg[1])*y[seg[1]])%MOD;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:202:49: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
202 | return (query_ans(1,1,n,1,seg[1])*y[seg[1]])%MOD;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:212:49: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
212 | return (query_ans(1,1,n,1,seg[1])*y[seg[1]])%MOD;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |