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>
#define pb push_back
#define ll long long
#define pii pair<int, int>
#define xx first
#define yy second
#define MAXN 100010
#define MOD 1000000007
using namespace std;
ll two=500000004; //inv
ll n, dp[MAXN], pos[MAXN], h[MAXN], w[MAXN], pref[MAXN], ress;
ll add(ll x, ll y)
{
ll ret=x+y;
if (ret>=MOD) ret-=MOD;
return ret;
}
ll mul(ll x, ll y)
{
ll ret=x*y;
if (ret>=MOD) ret%=MOD;
return ret;
}
ll sum(ll x, ll y)
{
ll ret=mul(mul(y, y+1), two);
ret-=mul(mul(x-1, x), two);
if (ret<0) ret+=MOD;
return ret;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n;
for (int i=1; i<=n; i++) cin>>h[i];
for (int i=1; i<=n; i++) cin>>w[i];
stack<pii> st;
st.push({-1, 0});
for (int i=1; i<=n; i++)
{
while (st.top().xx>h[i]) st.pop();
pos[i]=st.top().yy;
st.push({h[i], i});
}
pref[0]=0;
for (int i=1; i<=n; i++) pref[i]=pref[i-1]+w[i];
ll x, y;
x=sum(1, w[1]);
y=sum(1, h[1]);
dp[1]=mul(x, y);
for (int i=2; i<=n; i++)
{
int ind=pos[i];
//cout<<"ee "<<sum(pref[i-1]+1, pref[i])<<endl;
x=sum(pref[i-1]+1, pref[i]);
y=mul(mul(h[ind], h[ind]+1), two);
dp[i]=add(dp[i], mul(x, y));
//cout<<dp[i]<<endl;
x=sum(pref[i-1]+1-pref[ind], pref[i]-pref[ind]);
y=sum(h[ind]+1, h[i]);
dp[i]=add(dp[i], mul(x, y));
//cout<<dp[i]<<endl;
/*int ind=pos[i];
dp[i]=muldp[ind];
cout<<ind<<endl;
ll cnt=h[i]-h[ind];
ll temp=mul(mul(mul(cnt, cnt+1), two), (pref[i-1]-pref[ind]));
dp[i]=add(dp[i], temp);
dp[i]=mul(dp[i], w[i]);
ll x, y;
x=mul(mul(h[i], h[i]+1), two);
y=mul(mul(w[i], w[i]+1), two);
dp[i]=add(dp[i], mul(x, y));*/
}
for (int i=1; i<=n; i++) { ress+=dp[i]; /*cout<<dp[i]<<" ";*/ }
//cout<<endl;
cout<<ress;
}
/*
3
1 2 1
1 2 3
*/
# | 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... |