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;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
const int maxN=1e5+5,MOD=1e9+7,REVMOD=250000002;
const ll inf=2e18;
int n,l[maxN+1],r[maxN+1];
ll h[maxN+1],w[maxN+1],s[maxN+1];
stack<int> st;
void clr()
{
while(!st.empty())
{
st.pop();
}
}
ll mul(ll x,ll y)
{
return (x*y)%MOD;
}
ll sc(ll x1,ll x2)
{
ll tmp=mul(x1+1,x2+1);
tmp--;
if(tmp<0)
{
tmp+=MOD;
}
return tmp;
}
ll calc(ll n,ll m)
{
return mul(mul(mul(1LL*n,1LL*(n+1)),mul(1LL*m,1LL*(m+1))),REVMOD);
}
ll get(ll x1,ll x2,int i)
{
return mul((mul(w[i]-1,x1+x2)+sc(x1,x2))%MOD,calc(1,h[i]));
}
int main()
{
//freopen("","r",stdin);
//freopen("","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>h[i];
}
s[0]=0;
for(int i=1;i<=n;i++)
{
cin>>w[i];
s[i]=(s[i-1]+w[i])%MOD;
}
clr();
for(int i=1;i<=n;i++)
{
while(!st.empty()&&h[st.top()]>=h[i])
{
st.pop();
}
if(st.empty())
{
l[i]=1;
}
else l[i]=st.top()+1;
st.push(i);
}
clr();
for(int i=n;i>=1;i--)
{
while(!st.empty()&&h[st.top()]>h[i])
{
st.pop();
}
if(st.empty())
{
r[i]=n;
}
else r[i]=st.top()-1;
st.push(i);
}
ll ans=0;
for(int i=1;i<=n;i++)
{
ll tmp1=s[i-1]-s[l[i]-1],tmp2=s[r[i]]-s[i];
ans=(ans+get(tmp1,tmp2,i))%MOD;
ans=(ans+calc(h[i],w[i]))%MOD;
//cout<<i<<" "<<l[i]<<" "<<r[i]<<" "<<tmp1<<" "<<tmp2<<" "<<get(tmp1,tmp2,i)<<'\n';
}
cout<<ans;
}
/*5
5 9 49 32 4
1 1 1 1 1
*/
# | 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... |