#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9 + 7;
int s(int x) {
return (x * (x + 1) / 2) % mod;
}
int s(int l, int r) {
return (s(r) - s(l-1)) % mod;
}
void norm(int& x) {
x = (x % mod + mod) % mod;
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
stack<pair<int, int>> st;
int sum = 0, delta = 0;
vector<int> H(n+1), W(n+1);
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++) {
int h = H[i], w = W[i], aw = w;
while (!st.empty() && st.top().first >= h) {
auto [ff, ss] = st.top();
st.pop();
int nxth = h;
if (!st.empty() && st.top().first >= h) nxth = st.top().first, st.top().second = (st.top().second + ss) % mod;
else aw = (aw + ss) % mod;
delta -= ((s(nxth + 1, ff) * ss) % mod);
norm(delta);
}
// cout << delta << ' ' << h << ' ' << w << '\n';
st.push({h, aw});
sum += (delta * w % mod + s(h) * s(w) % mod) % mod;
norm(sum);
delta += s(h) * w % mod;
norm(delta);
// cout << sum << ' ' << delta << '\n';
}
cout << sum << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 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 |
7 ms |
1256 KB |
Output is correct |
4 |
Correct |
15 ms |
1884 KB |
Output is correct |
5 |
Correct |
15 ms |
1884 KB |
Output is correct |
6 |
Correct |
15 ms |
1884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
10 ms |
1116 KB |
Output is correct |
4 |
Correct |
18 ms |
1884 KB |
Output is correct |
5 |
Correct |
19 ms |
2044 KB |
Output is correct |
6 |
Correct |
1 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 |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
9 ms |
1248 KB |
Output is correct |
5 |
Correct |
24 ms |
1880 KB |
Output is correct |
6 |
Correct |
19 ms |
2036 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
10 ms |
1752 KB |
Output is correct |
10 |
Correct |
17 ms |
3676 KB |
Output is correct |
11 |
Correct |
22 ms |
3924 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
476 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
8 ms |
1624 KB |
Output is correct |
12 |
Correct |
15 ms |
3164 KB |
Output is correct |
13 |
Correct |
15 ms |
3164 KB |
Output is correct |
14 |
Correct |
15 ms |
3300 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
2 ms |
600 KB |
Output is correct |
17 |
Correct |
10 ms |
2140 KB |
Output is correct |
18 |
Correct |
19 ms |
3948 KB |
Output is correct |
19 |
Correct |
20 ms |
3944 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
812 KB |
Output is correct |
22 |
Correct |
10 ms |
2392 KB |
Output is correct |
23 |
Correct |
17 ms |
5356 KB |
Output is correct |
24 |
Correct |
19 ms |
5468 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
352 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
352 KB |
Output is correct |
30 |
Correct |
3 ms |
732 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
9 ms |
1884 KB |
Output is correct |
33 |
Correct |
9 ms |
2140 KB |
Output is correct |
34 |
Correct |
19 ms |
3676 KB |
Output is correct |
35 |
Correct |
18 ms |
3676 KB |
Output is correct |
36 |
Correct |
19 ms |
3816 KB |
Output is correct |
37 |
Correct |
19 ms |
3932 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
19 ms |
3940 KB |
Output is correct |
40 |
Correct |
19 ms |
3932 KB |
Output is correct |
41 |
Correct |
18 ms |
3932 KB |
Output is correct |
42 |
Correct |
25 ms |
4688 KB |
Output is correct |