#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX = 1e5 + 5, mod = 1e9 + 7, inv = 500000004;
int N;
ll h[MX], w[MX];
ll L[MX], R[MX], sum[MX];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N;
for(int i = 1; i <= N; i++) cin >> h[i];
for(int i = 1; i <= N; i++) cin >> w[i];
vector<pair<ll,ll>> v;
for(int i = 1; i <= N; i++) {
while(!v.empty() && v.back() > make_pair(h[i], (ll)i)) v.pop_back();
if(!v.empty()) L[i] = v.back().second;
v.push_back({h[i], i});
}
v.clear();
for(int i = N; i >= 1; i--) {
while(!v.empty() && v.back() > make_pair(h[i], (ll)i)) v.pop_back();
if(!v.empty()) R[i] = v.back().second;
else R[i] = N + 1;
v.push_back({h[i], i});
}
v.clear();
for(int i = 1; i <= N; i++) sum[i] = sum[i - 1] + w[i];
// for(int i = 1; i <= N; i++) {
// cout << L[i] << " " << R[i] << '\n';
// }
ll ans = 0;
for(int i = 1; i <= N; i++) {
ll x = sum[i - 1] - sum[L[i]];
ll y = sum[R[i] - 1] - sum[i];
x %= mod, y %= mod;
ans += 1ll * h[i] * (h[i] - 1) % mod * x % mod * y % mod * inv % mod;
ans += 1ll * h[i] * x % mod * y % mod;
ans %= mod;
x = sum[i - 1] - sum[L[i]];
y = sum[R[i] - 1] - sum[i];
x %= mod, y %= mod;
ans += 1ll * h[i] * (h[i] - 1) % mod * x % mod * w[i] % mod * inv % mod;
ans += 1ll * h[i] * x % mod * w[i] % mod;
ans += 1ll * h[i] * (h[i] - 1) % mod * y % mod * w[i] % mod * inv % mod;
ans += 1ll * h[i] * y % mod * w[i] % mod;
ans %= mod;
ans += 1ll * w[i] * (w[i] - 1) % mod * inv % mod * h[i] % mod * (h[i] - 1) % mod * inv % mod;
ans += 1ll * w[i] * h[i] % mod * (h[i] - 1) % mod * inv % mod;
ans += 1ll * h[i] * w[i] % mod * (w[i] - 1) % mod * inv % mod;
ans += 1ll * w[i] * h[i] % mod;
ans %= mod;
// cout << ans << '\n';
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
280 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
10 ms |
3300 KB |
Output is correct |
4 |
Correct |
21 ms |
6744 KB |
Output is correct |
5 |
Correct |
21 ms |
6092 KB |
Output is correct |
6 |
Correct |
25 ms |
5648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
984 KB |
Output is correct |
3 |
Correct |
12 ms |
3152 KB |
Output is correct |
4 |
Correct |
24 ms |
5888 KB |
Output is correct |
5 |
Correct |
23 ms |
5836 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
984 KB |
Output is correct |
4 |
Correct |
12 ms |
3152 KB |
Output is correct |
5 |
Correct |
25 ms |
5832 KB |
Output is correct |
6 |
Correct |
27 ms |
5896 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
984 KB |
Output is correct |
9 |
Correct |
12 ms |
3184 KB |
Output is correct |
10 |
Correct |
24 ms |
5916 KB |
Output is correct |
11 |
Correct |
24 ms |
5856 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
284 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
11 ms |
3280 KB |
Output is correct |
12 |
Correct |
20 ms |
6724 KB |
Output is correct |
13 |
Correct |
20 ms |
6116 KB |
Output is correct |
14 |
Correct |
19 ms |
5680 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
1240 KB |
Output is correct |
17 |
Correct |
12 ms |
4112 KB |
Output is correct |
18 |
Correct |
24 ms |
7820 KB |
Output is correct |
19 |
Correct |
24 ms |
7876 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
1244 KB |
Output is correct |
22 |
Correct |
12 ms |
4176 KB |
Output is correct |
23 |
Correct |
23 ms |
7756 KB |
Output is correct |
24 |
Correct |
23 ms |
7748 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
3 ms |
852 KB |
Output is correct |
31 |
Correct |
3 ms |
852 KB |
Output is correct |
32 |
Correct |
11 ms |
3216 KB |
Output is correct |
33 |
Correct |
12 ms |
3156 KB |
Output is correct |
34 |
Correct |
23 ms |
5912 KB |
Output is correct |
35 |
Correct |
23 ms |
5956 KB |
Output is correct |
36 |
Correct |
24 ms |
6092 KB |
Output is correct |
37 |
Correct |
23 ms |
6076 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
22 ms |
6096 KB |
Output is correct |
40 |
Correct |
22 ms |
6136 KB |
Output is correct |
41 |
Correct |
23 ms |
6360 KB |
Output is correct |
42 |
Correct |
24 ms |
6620 KB |
Output is correct |