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;
/*ifstream fin("date.in");
ofstream fout("date.out");*/
typedef long long ll;
const ll mod=1e9+7;
ll s[500005],st[500005],dr[500005],rmq[500005][22],n,loga[500005],ans;
struct date
{
ll h,w;
} v[500005];
ll getmin(ll l, ll r)
{
ll lg=r-l+1;
ll lgr=loga[lg];
return min(rmq[l][lgr],rmq[r-(1<<lgr)+1][lgr]);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n;
for(int i=1;i<=n;i++)
cin>>v[i].h;
for(int i=1;i<=n;i++)
{
loga[i]=loga[i-1];
while((1LL<<(loga[i]+1))<=i)
loga[i]++;
}
v[0].h=2e9;
v[n+1].h=2e9;
for(int i=1;i<=n;i++)
{
cin>>v[i].w;
}
for(int i=1;i<=n+1;i++)
s[i]=s[i-1]+v[i].w;
for(int i=n+1;i>=0;i--)
{
rmq[i][0]=v[i].h;
for(int j=1;j<=17;j++)
rmq[i][j]=min(rmq[i][j-1],rmq[i+(1<<(j-1))][j-1]);
}
for(int i=1;i<=n;i++)
{
int st=0;
int dr=i-1;
int pozmin=i,pozmax=i;
while(st<=dr)
{
int mij=(st+dr)/2;
ll val=getmin(mij,i-1);
if(val>v[i].h)
{
pozmin=min(pozmin,mij);
dr=mij-1;
}
else
st=mij+1;
}
st=i+1;
dr=n+1;
while(st<=dr)
{
int mij=(st+dr)/2;
ll val=getmin(i+1,mij);
if(val>=v[i].h)
{
pozmax=max(pozmax,mij);
st=mij+1;
}
else
dr=mij-1;
}
ll chooseH=v[i].h*(v[i].h+1)/2LL;
chooseH%=mod;
// both outside
ll nrL=s[i-1]-s[max(0,pozmin-1)];
nrL%=mod;
ll nrR=s[pozmax]-s[i];
nrR%=mod;
ll val=(nrL*nrR)%mod;
val=(val*chooseH)%mod;
ans=(ans+val)%mod;
// left outside
nrL=s[i-1]-s[max(0,pozmin-1)];
nrL%=mod;
nrR=v[i].w%mod;
val=(nrL*nrR)%mod;
val=(val*chooseH)%mod;
ans=(ans+val)%mod;
// right outside
nrL=v[i].w%mod;
nrR=s[pozmax]-s[i];
nrR%=mod;
val=(nrL*nrR)%mod;
val=(val*chooseH)%mod;
ans=(ans+val)%mod;
// none outside
ll chooseW=v[i].w*(v[i].w+1)/2LL;
chooseW%=mod;
val=(chooseH*chooseW)%mod;
ans=(ans+val)%mod;
}
cout<<ans;
return 0;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |