제출 #657543

#제출 시각아이디문제언어결과실행 시간메모리
657543dooompyFancy Fence (CEOI20_fancyfence)C++17
100 / 100
31 ms5888 KiB
#include "bits/stdc++.h"

using namespace std;

void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
    cout << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
    while (l != r) cout << *l << " \n"[++l == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
    return o >> a.first >> a.second;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
    return o << '(' << a.first << ", " << a.second << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
    bool is = false;
    for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
    return o << '}';
}

#ifdef local
#define test(args...) abc("[" + string(#args) + "]", args)
#else
#define test(args...) void(0)
#endif

using ll = long long;

ll w[100005], h[100005];

const ll mod = 1e9+7;
const ll inv = (mod + 1) / 2;

ll mul(ll a, ll b) {
    return ((a % mod) * (b % mod)) % mod;
}

ll half(ll a) {
    return mul(a, inv);
}

ll f(ll a) {
    return half(mul(a, a+1));
}

int lef[100005], rit[100005];
ll cumw[100005];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
//    freopen("", "r", stdin);
//    freopen("", "w", stdout);
    int n; cin >> n;

    for (int i = 1; i <= n; i++) cin >> h[i];
    for (int i = 1; i <= n; i++) {
        cin >> w[i];
        cumw[i] = (cumw[i-1] + w[i]) % mod;
    }

    stack<int> s;

    for (int i = 1; i <= n; i++) {
        while (!s.empty() && h[s.top()] > h[i]) s.pop();

        if (s.empty()) {
            lef[i] = 1;
        } else {
            lef[i] = s.top() + 1;
        }
        s.push(i);
    }

    s = stack<int>();

    for (int i = n; i >= 1; i--) {
        while (!s.empty() && h[s.top()] >= h[i]) s.pop();

        if (s.empty()) {
            rit[i] = n;
        } else {
            rit[i] = s.top() - 1;
        }
        s.push(i);
    }

    ll ans = 0;

    for (int i = 1; i <= n; i++) {
        int l = lef[i], r = rit[i];
        ll wleft = 0, wright = 0;
        wleft = cumw[i-1] - cumw[l-1];
        wright = cumw[r] - cumw[i];

        test(wleft, wright, i);

        ll waysw = f(wleft + w[i] + wright);
        waysw -= f(wleft);
        waysw -= f(wright);

        waysw = (waysw + 2 * mod) % mod;
        ll waysh = f(h[i]);

        ans = (ans + mul(waysw, waysh))  %mod  ;

    }

    cout << ans;
}
#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...