#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] - 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 - 1];
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;
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';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
984 KB |
Output is correct |
3 |
Correct |
12 ms |
3120 KB |
Output is correct |
4 |
Correct |
31 ms |
6840 KB |
Output is correct |
5 |
Correct |
26 ms |
7964 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
23 ms |
6648 KB |
Output is correct |
6 |
Correct |
24 ms |
7932 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
1112 KB |
Output is correct |
9 |
Correct |
16 ms |
4180 KB |
Output is correct |
10 |
Correct |
26 ms |
7744 KB |
Output is correct |
11 |
Correct |
24 ms |
7784 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |