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;
using ll = long long;
using pl = pair<ll,ll>;
using vi = vector<ll>;
using ml = map<ll,ll>;
const ll mod=1000000007;
#define pb push_back
#define forp(i,a,b) for(ll i=a;i<=b;i++)
#define forn(i,a,b) for(ll i=a;i>=b;i--)
#define newl '\n'
#define form(m,it) for(auto it=m.begin();it!=m.end(); it++)
#define ff first
#define ss second
void solve(){
ll n; cin>>n;
ll h[n+1],w[n+1];
forp(i,1,n){cin>>h[i];}
forp(i,1,n){cin>>w[i];}
ll dp[n+1],m[n+1],pre[n+1];
stack<pl> st;
st.push({0,0});
dp[0]=pre[0]=h[0]=w[0]=0;
forp(i,1,n){
pre[i]=pre[i-1]+w[i]; pre[i]%=mod;
while(st.top().ff>h[i]){
st.pop();
}
m[i]=st.top().second;
st.push({h[i],i});
}
ll ans=0,j,c,d;
forp(i,1,n){
j=m[i];
dp[i]=dp[j];
c=(h[i]*(h[i]+1))/2; c%=mod; c*=(pre[i]-pre[j]); c%=mod;
dp[i]+=c; dp[i]%=mod;
c=(h[i]*(h[i]+1))/2; c%=mod; c*=(w[i]*(w[i]-1))/2; c%=mod;
d=(h[i]*(h[i]+1))/2; d%=mod; d*=(w[i]-1); d%=mod; d*=(pre[i-1]-pre[j]); d%=mod;
c+=d; c%=mod;
ans+=c; ans%=mod;
ans+=(dp[j]*w[i]-1); ans%=mod;
ans+=dp[i]; ans%=mod;
}
cout<<(ans+1+mod)%mod<<newl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# | 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... |