#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;
int sob = 0;
szL = (idx ? pre[idx-1] : 0) - (l ? pre[l-1] : 0);
szR = pre[r] - pre[idx];
sob += szL * szR % mod * h % mod * (h-1) % mod * inv2 % mod;
sob %= mod;
sob += szL * szR % mod * h % mod;
sob %= mod;
ans = (ans - sob + mod) % mod;
ans += w[idx] * h % mod * (h-1) % mod * inv2 % mod;
ans %= mod;
ans += w[idx] * h % mod;
ans %= mod;
cerr << ans << endl;
st.insert(idx);
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
3 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
4 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 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 |
3 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 |
4 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
3 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |