#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
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 |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
3 |
Correct |
9 ms |
2268 KB |
Output is correct |
4 |
Correct |
19 ms |
4356 KB |
Output is correct |
5 |
Correct |
18 ms |
4444 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
480 KB |
Output is correct |
3 |
Correct |
2 ms |
684 KB |
Output is correct |
4 |
Correct |
9 ms |
2396 KB |
Output is correct |
5 |
Correct |
18 ms |
4360 KB |
Output is correct |
6 |
Correct |
21 ms |
4232 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
844 KB |
Output is correct |
9 |
Correct |
11 ms |
2436 KB |
Output is correct |
10 |
Correct |
18 ms |
4332 KB |
Output is correct |
11 |
Correct |
18 ms |
4428 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |