Submission #633485

#TimeUsernameProblemLanguageResultExecution timeMemory
633485Ronin13Fancy Fence (CEOI20_fancyfence)C++14
100 / 100
102 ms5816 KiB
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;

ll dp[100001];
const ll linf = 1e18 + 1;
const ll mod = 1e9 + 7;


int main(){
    int n; cin >> n;
    ll h[n + 1], w[n + 1];
    for(int i = 1; i <= n; i++) cin >> h[i];
    for(int i = 1; i <= n; i++){
        cin >> w[i];
    }
    ll pref[n + 1];
    pref[0] = 0;
    for(int i = 1; i <= n; i++) pref[i] = pref[i - 1] + w[i];
    stack <int> st;
    st.push(1);
    int l[n + 1], r[n + 1];
    l[1] = 0;
    for(int i = 2; i <= n; i++){
        while(!st.empty()){
            int v = st.top();
            if(h[v] <= h[i]) break;
            st.pop();
        }
        if(st.empty()) l[i] = 0;
        else l[i] = st.top();
        st.push(i);
    }
    while(!st.empty()) st.pop();
    r[n] = n + 1;
    st.push(n);
    for(int i= n - 1; i >= 1; i--){
        while(!st.empty()){
            int v = st.top();
            if(h[v] < h[i]) break;
            st.pop();
        }
        if(st.empty()) r[i] = n + 1;
        else r[i] = st.top();
        st.push(i);
    }
    ll ans = 0;
    for(int i = 1; i <= n; i++){
        ll x = h[i] * (h[i] - 1) / 2 + h[i]; x %= mod;
        int L = l[i], R = r[i];
        ll y = pref[R - 1] - pref[L];
        ll y1 = y;
        if(y % 2 == 0){
            ll x = y - 1; x %= mod;
            y /= 2;
            y %= mod;
            y *= x; y %= mod;
            y += y1; y %= mod;
        }
        else{
            ll v = y - 1; v /= 2;
            v %= mod;
            v *= y % mod;
            v %= mod;
            y = v;
            y += y1; y %= mod;
        }
        ans += x * y;
        ans %= mod;
        y = pref[i - 1] - pref[L];
        y1 = y;
        if(y % 2 == 0){
            ll x = y - 1; x %= mod;
            y /= 2;
            y %= mod;
            y *= x; y %= mod;
            y += y1; y %= mod;
        }
        else{
            ll v = y - 1; v /= 2;
            v %= mod;
            v *= y % mod;
            v %= mod;
            y = v;
            y += y1;
            y %= mod;
        }
        ans -= x * y; ans %= mod; ans += mod; ans %= mod;
        y = pref[R - 1] - pref[i];
        y1 = y;
        if(y % 2 == 0){
            ll x = y - 1; x %= mod;
            y /= 2;
            y %= mod;
            y *= x; y %= mod;
            y += y1; y %= mod;
        }
        else{
            ll v = y - 1; v /= 2;
            v %= mod;
            v *= y % mod;
            v %= mod;
            y = v;
            y += y1; y %= mod;
        }
        ans -= x * y; ans %= mod; ans += mod; ans %= mod;
    }
    cout << ans;
    return 0;
}
#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...