# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
967066 | blackslex | Fancy Fence (CEOI20_fancyfence) | C++17 | 34 ms | 5576 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |