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;
#define ll long long
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define sz(a) (int)a.size()
#define int long long
const int N = 1e5 + 5, mod = 1e9 + 7, K = 17;
int ans = 0;
int jump[N][K], h[N], n, p[N], w[N];
int merge(int i, int j) { return h[i] < h[j] ? i : j; }
int query(int l, int r) {
int j = 31 - __builtin_clz(r - l + 1);
return merge(jump[l][j], jump[r - (1 << j) + 1][j]);
}
void rec(int l, int r) {
if(l > r) return;
int i = query(l, r);
int canh = h[i] * (h[i] + 1) / 2 % mod;
int val = (p[i] - (l ? p[l - 1] : 0)) % mod;
val *= (p[r] - p[i]) % mod;
val %= mod;
if(i > l) {
val += (p[i - 1] % mod - (l ? p[l - 1] : 0) % mod + mod) % mod * w[i] % mod;
}
val += w[i] * (w[i] + 1) / 2 % mod;
val %= mod;
ans += val * canh % mod;
ans %= mod;
rec(l, i - 1);
rec(i + 1, r);
}
void solve() {
cin >> n;
for(int i = 0; i < n; ++i) {
cin >> h[i];
jump[i][0] = i;
}
for(int i = 0; i < n; ++i) {
cin >> w[i];
p[i] = w[i] + (i ? p[i - 1] : 0);
}
for(int j = 1; j < K; ++j) {
for(int i = 0; i + (1 << j) <= n; ++i) {
jump[i][j] = merge(jump[i][j - 1], jump[i + (1 << (j - 1))][j - 1]);
}
}
rec(0, n - 1);
cout << ans << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t = 1;
//cin >> t;
while(t--) {
solve();
}
}
# | 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... |