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;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
const int mod = 1e9 + 7;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n; cin>>n;
vector<int> h(n + 1);
for (int i = 1; i <= n; i++){
cin>>h[i];
}
vector<int> w(n + 1);
vector<ll> p(n + 1);
for (int i = 1; i <= n; i++){
cin>>w[i];
p[i] = p[i - 1] + w[i];
}
auto sum = [&](int l, int r){
return (p[r] - p[l - 1]) % mod;
};
ll out = 0;
for (int i = 1; i <= n; i++){
out += (((1LL * w[i] * (w[i] + 1) / 2) % mod) * ((1LL * h[i] * (h[i] + 1) / 2) % mod)) % mod;
}
vector<int> f(n + 1);
vector<ll> pr(n + 1);
function<void(int, int)> solve = [&](int l, int r){
if (l == r) return;
int m = (l + r) / 2;
f[m] = h[m]; f[m + 1] = h[m + 1];
for (int i = m - 1; i >= l; i--) f[i] = min(f[i + 1], h[i]);
for (int i = m + 2; i <= r; i++) f[i] = min(f[i - 1], h[i]);
pr[m] = 0;
for (int i = m + 1; i <= r; i++){
pr[i] = (pr[i - 1] + ((1LL * f[i] * (f[i] + 1) / 2) % mod * w[i]) % mod) % mod;
}
int j = m + 1;
for (int i = m; i >= l; i--){
while (j <= r && f[j] >= f[i]) j++;
if (j <= r) out += (1LL * w[i] * (pr[r] - pr[j - 1])) % mod;
if (m < j) out += (((1LL * f[i] * (f[i] + 1) / 2) % mod) * ((1LL * w[i] * sum(m + 1, j - 1)) % mod)) % mod;
}
out %= mod;
};
solve(1, n);
cout<<out<<"\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... |