#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX = 1e5, mod = 1e9 + 7, inv = 500000004;
int N;
int h[MX], w[MX];
int 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<int,int>> v;
for(int i = 1; i <= N; i++) {
while(!v.empty() && v.back() > make_pair(h[i], 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], 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 << left[i] << " " << right[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 |
1 ms |
328 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 |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 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 |
328 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |