제출 #1085669

#제출 시각아이디문제언어결과실행 시간메모리
1085669_callmelucianFancy Fence (CEOI20_fancyfence)C++14
100 / 100
21 ms4768 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;

#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())

const ll MOD = 1e9 + 7;
const int mn = 1e5 + 5;

int add (int a, int b) {
    return a + b - (a + b < MOD ? 0 : MOD);
}

int sub (int a, int b) {
    return a - b + (a - b >= 0 ? 0 : MOD);
}

int mul (int a, int b) {
    return 1LL * a * b % MOD;
}

int binpow (int a, int b = MOD - 2) {
    int ans = 1, lg = 31 - __builtin_clz(b);
    for (int i = 0; i <= lg; i++) {
        if (b & (1 << i)) ans = mul(ans, a);
        a = mul(a, a);
    }
    return ans;
}

const int half = binpow(2);

int calc (int n) {
    return mul(mul(n, add(n, 1)), half);
}

int calcAll (int h, int w) {
    return mul(calc(h), calc(w));
}

int calcRight (int h, int w) {
    return mul(calc(h), w);
}

int dp[mn], dpRight[mn], h[mn], w[mn], pre[mn];
stack<pii> st;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n; cin >> n;
    for (int i = 1; i <= n; i++) cin >> h[i];
    for (int i = 1; i <= n; i++) {
        cin >> w[i];
        pre[i] = add(pre[i - 1], w[i]);
    }

    st.emplace(0, 0);
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        while (st.top().first > h[i]) st.pop();
        int delWidth = sub(pre[i - 1], pre[st.top().second]);
        int curRight = add(dpRight[st.top().second], calcRight(h[i], delWidth));
        st.emplace(h[i], i);

        dpRight[i] = add(curRight, calcRight(h[i], w[i]));
        ans = add(ans, add(mul(curRight, w[i]), calcAll(h[i], w[i])));
    }
    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...