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;
const int N = 100'000 + 10,
M = 1'000'000'007;
int n;
int h[N], w[N];
int d[N];
int fl[N], fr[N];
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> h[i];
for (int i = 1; i <= n; ++i) cin >> w[i];
for (int i = 1; i <= n; ++i) d[i] = (d[i - 1] + w[i]) % M;
{
stack<int> st; st.push(0);
for (int i = 1; i <= n; ++i) {
while (st.size() && h[st.top()] >= h[i]) st.pop();
fl[i] = st.top() + 1;
st.push(i);
}
}
{
stack<int> st; st.push(n + 1);
for (int i = n; i >= 1; --i) {
while (st.size() && h[st.top()] >= h[i]) st.pop();
fr[i] = st.top() - 1;
st.push(i);
}
}
auto cal = [&](int l, int r) { return 1ll * (l + r) * (r - l + 1) / 2 % M; };
auto add = [&](auto& x, long long val) { x = (x + val) % M; };
auto sub = [&](auto& x, long long val) { x = ((x - val) % M + M) % M; };
int answer = 0;
for (int i = 1; i <= n; ++i) {
if (fl[i] == fl[i - 1] && fr[i] == fr[i - 1] && h[i] == h[i - 1]) continue;
int l = fl[i], r = fr[i];
int height = max(h[l - 1], h[r + 1]);
add(answer, cal(1, d[r] - d[l - 1]) * cal(height + 1, h[i]));
}
cout << answer << "\n";
}
Compilation message (stderr)
fancyfence.cpp: In function 'int32_t main()':
fancyfence.cpp:41:8: warning: variable 'sub' set but not used [-Wunused-but-set-variable]
41 | auto sub = [&](auto& x, long long val) { x = ((x - val) % M + M) % M; };
| ^~~
# | 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... |