Submission #967066

#TimeUsernameProblemLanguageResultExecution timeMemory
967066blackslexFancy Fence (CEOI20_fancyfence)C++17
100 / 100
34 ms5576 KiB
#include<bits/stdc++.h>

using namespace std;
using ll = long long;

const int M = 1e9 + 7, INV = 5e8 + 4;
int n;

int main() {
    scanf("%d", &n);
    vector<int> h(n + 5), w(n + 5);
    vector<ll>  pref(n + 5);
    for (int i = 1; i <= n; i++) scanf("%d", &h[i]);
    for (int i = 1; i <= n; i++) scanf("%d", &w[i]), pref[i] = pref[i - 1] + w[i], pref[i] %= M;
    vector<int> l(n + 5), r(n + 5, n + 1), sec(n + 5);
    stack<int> st;
    for (int i = 1; i <= n; i++) {
        while (!st.empty() && h[i] <= h[st.top()]) {
            sec[st.top()] = max(sec[st.top()], h[i]);
            st.pop();
        }
        if (!st.empty()) l[i] = st.top();
        st.emplace(i);
    }
    while (!st.empty()) st.pop();
    for (int i = n; i >= 1; i--) {
        while (!st.empty() && h[i] < h[st.top()]) {
            sec[st.top()] = max(sec[st.top()], h[i]);
            st.pop();
        }
        if (!st.empty()) r[i] = st.top();
        st.emplace(i);
    }
    for (int i = 1; i <= n; i++) l[i]++, r[i]--;
    ll ans = 0;
    for (int i = 1; i <= n; i++) {
        ll cursz = pref[r[i]] - pref[l[i] - 1]; cursz %= M; cursz += M; cursz %= M;
        ll pos = cursz * (cursz + 1) % M * INV % M;
        ans += pos * sec[i] % M * (h[i] - sec[i]) % M; ans %= M;
        ans += pos * (h[i] - sec[i]) % M * (h[i] - sec[i] + 1) % M * INV % M; ans %= M;
    }
    printf("%lld", ans);
}

Compilation message (stderr)

fancyfence.cpp: In function 'int main()':
fancyfence.cpp:10:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   10 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
fancyfence.cpp:13:39: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |     for (int i = 1; i <= n; i++) scanf("%d", &h[i]);
      |                                  ~~~~~^~~~~~~~~~~~~
fancyfence.cpp:14:39: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 |     for (int i = 1; i <= n; i++) scanf("%d", &w[i]), pref[i] = pref[i - 1] + w[i], pref[i] %= M;
      |                                  ~~~~~^~~~~~~~~~~~~
#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...