Submission #567614

#TimeUsernameProblemLanguageResultExecution timeMemory
567614stevancvFancy Fence (CEOI20_fancyfence)C++14
100 / 100
34 ms5100 KiB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
using namespace std;
const int N = 2e5 + 5;
const ll mod = 1e9 + 7;
ll Add(ll a, ll b) {
    a += b;
    if (a >= mod) a -= mod;
    return a;
}
ll Sub(ll a, ll b) {
    a -= b;
    if (a < 0) a += mod;
    return a;
}
ll Md(ll x) {
    while (x < 0) x += mod;
    while (x >= mod) x -= mod;
    return x;
}
ll Mul(ll a, ll b) {
    return (a * b) % mod;
}
ll Exp(ll x, ll k) {
    ll ans = 1;
    for (; k; k >>= 1) {
        x = Mul(x, x);
        if (k & 1) ans = Mul(ans, x);
    }
    return ans;
}
ll Div(ll a, ll b) {
    return Mul(a, Exp(b, mod - 2));
}
ll G(ll x) {
    ll y = x * (x + 1) / 2;
    y %= mod;
    return y;
}
ll F(ll H, ll W) {
    H = G(H);
    W = G(W);
    H %= mod;
    W %= mod;
    return Mul(H, W);
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    vector<ll> h(n), w(n), p(n);
    for (int i = 0; i < n; i++) {
        cin >> h[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> w[i];
        p[i] = w[i];
        if (i > 0) p[i] = Add(p[i], p[i - 1]);
    }
    ll ans = 0;
    stack<int> s;
    for (int i = 0; i < n; i++) {
        while (!s.empty() && h[s.top()] > h[i]) {
            int j = s.top();
            s.pop();
            int k = -1;
            if (!s.empty()) k = s.top();
            ll mx = h[i];
            if (k != -1) mx = max(mx, h[k]);
            ll H = h[j] - mx;
            ll W = p[i - 1];
            if (k != -1) W = Sub(W, p[k]);
            ans = Add(ans, F(H, W));
            ans = Add(ans, Mul(G(W), Mul(H, mx)));
        }
        s.push(i);
    }
    while (!s.empty()) {
        int j = s.top();
        s.pop();
        int k = -1;
        if (!s.empty()) k = s.top();
        ll mx = 0;
        if (k != -1) mx = max(mx, h[k]);
        ll H = h[j] - mx;
        ll W = p[n - 1];
        if (k != -1) W = Sub(W, p[k]);
        ans = Add(ans, F(H, W));
        ans = Add(ans, Mul(G(W), Mul(H, mx)));
    }
    cout << ans << en;
    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...