#include "bits/stdc++.h"
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
const long long INF = 1ll << 60;
const int N = 1e5 + 5;
const int M = 1e9 + 7;
int pw(int a) {
a %= M;
return a * (a - 1) / 2 % M;
}
int H[N], W[N], n, ps[N], lt[N], rt[N];
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
FOR(i, 1, n + 1) cin >> H[i];
FOR(i, 1, n + 1) cin >> W[i];
vector<int> st{0};
H[0] = H[n + 1] = -INF;
FOR(i, 1, n + 1) {
while (H[i] < H[st.back()]) st.pop_back();
lt[i] = st.back();
st.push_back(i);
}
st = {n + 1};
for (int i = n; i >= 1; i--) {
while (H[i] <= H[st.back()]) st.pop_back();
rt[i] = st.back();
st.push_back(i);
}
FOR(i, 1, n + 1) ps[i] = ps[i - 1] + W[i];
int ans = 0;
FOR(i, 1, n + 1) {
int L = ps[i - 1] - ps[lt[i]] + 1;
int R = ps[rt[i] - 1] - ps[i] + 1;
int re = pw(L + R + W[i] - 1) - pw(L) - pw(R);
ans += re % M * pw(H[i] + 1) % M;
}
ans %= M;
if (ans < 0) ans += M;
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
9 ms |
3292 KB |
Output is correct |
4 |
Correct |
23 ms |
6104 KB |
Output is correct |
5 |
Correct |
23 ms |
5840 KB |
Output is correct |
6 |
Correct |
18 ms |
5624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
1112 KB |
Output is correct |
3 |
Correct |
10 ms |
3876 KB |
Output is correct |
4 |
Correct |
22 ms |
7180 KB |
Output is correct |
5 |
Correct |
21 ms |
7124 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
1116 KB |
Output is correct |
4 |
Correct |
10 ms |
3804 KB |
Output is correct |
5 |
Correct |
20 ms |
7116 KB |
Output is correct |
6 |
Correct |
24 ms |
7300 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
2 ms |
1112 KB |
Output is correct |
9 |
Correct |
10 ms |
3804 KB |
Output is correct |
10 |
Correct |
19 ms |
6872 KB |
Output is correct |
11 |
Correct |
20 ms |
7132 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 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 |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
472 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
496 KB |
Output is correct |
8 |
Correct |
1 ms |
360 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
8 ms |
3296 KB |
Output is correct |
12 |
Correct |
17 ms |
6096 KB |
Output is correct |
13 |
Correct |
17 ms |
5880 KB |
Output is correct |
14 |
Correct |
16 ms |
5724 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
1116 KB |
Output is correct |
17 |
Correct |
10 ms |
3832 KB |
Output is correct |
18 |
Correct |
20 ms |
7420 KB |
Output is correct |
19 |
Correct |
20 ms |
7124 KB |
Output is correct |
20 |
Correct |
1 ms |
500 KB |
Output is correct |
21 |
Correct |
4 ms |
1112 KB |
Output is correct |
22 |
Correct |
12 ms |
3816 KB |
Output is correct |
23 |
Correct |
19 ms |
6872 KB |
Output is correct |
24 |
Correct |
20 ms |
7128 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
856 KB |
Output is correct |
31 |
Correct |
2 ms |
860 KB |
Output is correct |
32 |
Correct |
11 ms |
3304 KB |
Output is correct |
33 |
Correct |
11 ms |
3164 KB |
Output is correct |
34 |
Correct |
20 ms |
6032 KB |
Output is correct |
35 |
Correct |
20 ms |
5980 KB |
Output is correct |
36 |
Correct |
21 ms |
6200 KB |
Output is correct |
37 |
Correct |
24 ms |
6288 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
19 ms |
6236 KB |
Output is correct |
40 |
Correct |
18 ms |
6256 KB |
Output is correct |
41 |
Correct |
27 ms |
6420 KB |
Output is correct |
42 |
Correct |
19 ms |
6644 KB |
Output is correct |