제출 #445107

#제출 시각아이디문제언어결과실행 시간메모리
445107KULIKOLDFancy Fence (CEOI20_fancyfence)C++17
100 / 100
34 ms5408 KiB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define endl '\n'
const int DIM = 2E5+7;
const ll MOD = 1E9+7;
ll W[DIM],H[DIM];
int solve(){
    ll res = 0,sum = 0;
    stack<pair<ll,ll> > S;
    int n;
    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){
        ll h = H[i],w = W[i];
        ll cur = 0;
        while(!S.empty() && S.top().first>=h){
            cur+=S.top().second;
            sum= (sum-S.top().first*(S.top().first+1)/2%MOD*S.top().second)%MOD;
            sum = (sum+h*(h+1)/2%MOD*S.top().second)%MOD;
            S.pop();
        }
        res = (res+w*sum)%MOD;
        ll mu = h*(h+1)/2%MOD;
        res = (res+w*(w+1)/2%MOD*mu%MOD)%MOD;
        cur+=w;
        sum = (sum+h*(h+1)/2%MOD*w)%MOD;
        S.push({h,cur%MOD});
    }
    if (res<0)res+=MOD;
    cout<<res<<endl;
    return 1;
}
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    t = 1;
    while(t--)
        assert(solve());
    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...