Submission #1287057

#TimeUsernameProblemLanguageResultExecution timeMemory
1287057zyntherixFancy Fence (CEOI20_fancyfence)C++20
0 / 100
1 ms584 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define vi vector<int>
#define vs vector<string>
#define vb vector<bool>
#define vpi vector<pi>
#define pb push_back
#define all(a) (a).begin(), (a).end()
const int mod = 1e9 + 7;
int md_inv_2 = 500000004;

int sm(int x)
{
    return (((x * (x + 1)) % mod) * md_inv_2) % mod;
}

void solve()
{
    int n;
    cin >> n;
    int h[n], w[n];
    for (int i = 0; i < n; i++)
        cin >> h[i];
    int pref_w[n + 1];
    pref_w[0] = 0;
    for (int i = 0; i < n; i++)
    {
        cin >> w[i];
        pref_w[i + 1] = (pref_w[i] + w[i]) % mod;
    }
    vpi ns, nl;
    vi nsi(n, n), nli(n, n);
    ns.pb({0, n});
    nl.pb({mod, n});
    for (int i = n - 1; i >= 0; i--)
    {
        while (ns.back().first >= h[i])
        {
            ns.pop_back();
        }
        nsi[i] = ns.back().second;
        ns.pb({h[i], i});
        while (nl.back().first < h[i])
        {
            nl.pop_back();
        }
        nli[i] = nl.back().second;
        nl.pb({h[i], i});
    }
    int ans = 0;
    vi st;
    st.pb(0);
    for (int i = 0; i < n; i++)
    {
        while (st.back() > h[i])
        {
            st.pop_back();
        }
        if (st.back() == h[i])
            continue;
        int w2 = (((pref_w[nsi[i]] - pref_w[i]) % mod) + mod) % mod;
        int x = (sm(w2) * (((sm(h[i]) - sm(st.back())) % mod) + mod) % mod) % mod;
        if (nsi[i] >= nli[i])
            ans = (ans + x) % mod;
        st.pb(h[i]);
    }
    ns.clear();
    nl.clear();
    nsi.clear();
    nli.clear();
    st.clear();
    nsi.resize(n, -1);
    nli.resize(n, -1);
    ns.pb({0, -1});
    nl.pb({mod, -1});
    pref_w[n] = 0;
    for (int i = n - 1; i >= 0; i--)
    {
        pref_w[i] = (pref_w[i + 1] + w[i]) % mod;
    }
    for (int i = 0; i < n; i++)
    {
        while (ns.back().first >= h[i])
        {
            ns.pop_back();
        }
        nsi[i] = ns.back().second;
        ns.pb({h[i], i});
        while (nl.back().first < h[i])
        {
            nl.pop_back();
        }
        nli[i] = nl.back().second;
        nl.pb({h[i], i});
    }
    st.pb(0);
    for (int i = n - 1; i >= 0; i--)
    {
        while (st.back() > h[i])
        {
            st.pop_back();
        }
        if (st.back() == h[i])
            continue;
        int w2 = (((pref_w[nsi[i] + 1] - pref_w[i + 1]) % mod) + mod) % mod;
        int x = (sm(w2) * (((sm(h[i]) - sm(st.back())) % mod) + mod) % mod) % mod;
        if (nsi[i] <= nli[i])
            ans = (ans + x) % mod;
        st.pb(h[i]);
    }
    int curr = 0;
    for (int i = 0; i < n; i++)
    {
        if (h[i] != 1)
        {
            ans = ((ans - sm(curr)) % mod + mod) % mod;
            curr = 0;
        }
        else
        {
            curr += w[i];
        }
    }
    ans = ((ans - sm(curr)) % mod + mod) % mod;
    cout << ans << '\n';
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    int t = 1;
    // cin >> t;
    while (t--)
    {
        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...