#pragma GCC optimize ("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5+5, mod = 1e9+7;
int n, w[N], pre[N];
pair<int, int> v[N];
set<int> st;
int pw(int x, int y) {
return (!y ? 1 : pw(x*x % mod, y/2) * (y%2 ? x : 1) % mod);
}
int inv(int x) {
return pw(x, mod-2);
}
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n;
for (int i = 0; i < n; i++) {
int h;
cin >> h;
v[i].first = h;
v[i].second = i;
}
sort(v, v+n);
for (int i = 0; i < n; i++) {
cin >> w[i];
pre[i] = w[i] + (i ? pre[i-1] : 0);
}
int ans = 0;
int inv2 = inv(2);
for (int i = 0; i < n; i++) {
int h = v[i].first;
int idx = v[i].second;
auto it = st.upper_bound(idx);
int l = 0;
if (it != st.begin()) l = *prev(it) + 1;
int r = n-1;
if (it != st.end()) r = *it - 1;
int szL = pre[idx] - (l ? pre[l-1] : 0);
int szR = pre[r] - pre[idx];
ans += szL * szR % mod * h % mod * (h-1) % mod * inv2 % mod;
ans %= mod;
ans += szL * szR % mod * h % mod;
ans %= mod;
szL = (idx ? pre[idx-1] : 0) - (l ? pre[l-1] : 0);
szR = pre[r] - (idx ? pre[idx-1] : 0);
ans += szL * szR % mod * h % mod * (h-1) % mod * inv2 % mod;
ans %= mod;
ans += szL * szR % mod * h % mod;
ans %= mod;
ans += w[idx] * (w[idx]-1) % mod * inv2 % mod * h % mod * (h-1) % mod * inv2 % mod;
ans %= mod;
ans += w[idx] * (w[idx]-1) % mod * inv2 % mod * h % mod;
ans %= mod;
ans += w[idx] * h % mod * (h-1) % mod * inv2 % mod;
ans %= mod;
ans += w[idx] * h % mod;
ans %= mod;
st.insert(idx);
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |