Submission #852746

#TimeUsernameProblemLanguageResultExecution timeMemory
852746DenkataFancy Fence (CEOI20_fancyfence)C++14
100 / 100
24 ms7868 KiB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+3;
const int mod = 1e9+7;
ll i,j,p,q,n,m,k,h[maxn],w[maxn],l[maxn],r[maxn],pref[maxn],ans,same[maxn];
stack <ll> s;
bool marked[maxn];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n;
    for(i=1;i<=n;i++)
    {
        l[i] = 1;
        r[i] = n;
        cin>>h[i];
    }
    for(i=1;i<=n;i++)
    {
        cin>>w[i];
        pref[i] = pref[i-1]+w[i];
    }
    for(i=1;i<=n;i++)
    {
        while(!s.empty() && h[i]<h[s.top()])
            s.pop();
        if(!s.empty())
            l[i] = s.top()+1;
        s.push(i);
    }

    while(!s.empty())
        s.pop();
    for(i=n;i>=1;i--)
    {
        while(!s.empty() && h[i]<=h[s.top()])
            s.pop();
        if(!s.empty())
            r[i] = s.top()-1;
        s.push(i);
    }


    for(i=1;i<=n;i++)
    {
        q = pref[r[i]] - pref[i-1];///wmain
        q%=mod;
        p = pref[i-1]-pref[l[i]-1];///wside - left
        p%=mod;
        k = (h[i]*1ll*(h[i]+1))/2;k%=mod;
        ans+=(((p*q)%mod)*k)%mod;ans%=mod;
        ///ne sa vsichki prebroeni!!!
        q = pref[r[i]] - pref[i];q%=mod;
        p = w[i];
        ans+=(((p*q)%mod)*k)%mod;ans%=mod;
    }
    for(i=1;i<=n;i++)
    {
        k = (h[i]*1ll*(h[i]+1))/2;k%=mod;
        p = (w[i]*1ll*(w[i]+1))/2;p%=mod;
        ans+=(p*k)%mod;ans%=mod;
    }
    j = pref[n];j%=mod;
    k = (h[1]*1ll*(h[1]+1))/2;k%=mod;
    p = (j*(j+1))/2;p%=mod;
    q = 0;
    q=(p*k)%mod;ans%=mod;
    //cout<<q<<endl;
    cout<<ans<<endl;
    return 0;
}
/**
5
3 2 4 1 5
1 1 1 1 1

2
2 2
1 2
*/
#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...