This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX = 1e5, 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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |