Submission #894468

#TimeUsernameProblemLanguageResultExecution timeMemory
894468Dec0DeddFancy Fence (CEOI20_fancyfence)C++14
100 / 100
23 ms5972 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 1e5+10;
const int MOD = 1e9+7;

ll h[N], w[N], pw[N], dp[N], n;

void solve() {
    cin>>n;
    for (int i=1; i<=n; ++i) cin>>h[i];
    for (int i=1; i<=n; ++i) cin>>w[i];

    for (int i=1; i<=n; ++i) pw[i]=pw[i-1]+w[i], pw[i]%=MOD;
    dp[0]=0, h[0]=0;

    ll ans=0; // dp[i] -> from {1, 2, ..., i} ends on rightmost point of i
    stack<int> st; st.push(0);

    for (int i=1; i<=n; ++i) {
        while (h[st.top()] > h[i]) st.pop();
        ll hl=(h[i]*(h[i]+1)/2)%MOD;

        // add all rects which start before (st.top())
        (ans+=dp[st.top()]*w[i])%=MOD;

        // add all rects which contains atleast one from w[i] and suffix of others
        (ans+=(w[i]*(pw[i-1]-pw[st.top()]+1)%MOD)*hl)%=MOD;
        (ans+=((w[i]*(w[i]-1)/2)%MOD)*hl)%=MOD;

        (dp[i]+=dp[st.top()]+(pw[i]-pw[st.top()])*hl)%=MOD;
        st.push(i);
    }

    /*
    cout<<"dp: ";
    for (int i=1; i<=n; ++i) cout<<dp[i]<<" ";
    cout<<"\n"; */
    ans%=MOD;
    (ans+=MOD)%=MOD;

    assert(ans >= 0 && ans < MOD);
    cout<<ans<<"\n";
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    
    int t=1; //cin>>t;
    while (t--) solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...