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;
void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
cout << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
while (l != r) cout << *l << " \n"[++l == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
return o >> a.first >> a.second;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
return o << '(' << a.first << ", " << a.second << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
bool is = false;
for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
return o << '}';
}
#ifdef local
#define test(args...) abc("[" + string(#args) + "]", args)
#else
#define test(args...) void(0)
#endif
using ll = long long;
ll w[100005], h[100005];
const ll mod = 1e9+7;
const ll inv = (mod + 1) / 2;
ll mul(ll a, ll b) {
return ((a % mod) * (b % mod)) % mod;
}
ll half(ll a) {
return mul(a, inv);
}
ll f(ll a) {
return half(mul(a, a+1));
}
int lef[100005], rit[100005];
ll cumw[100005];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("", "r", stdin);
// freopen("", "w", stdout);
int n; cin >> n;
for (int i = 1; i <= n; i++) cin >> h[i];
for (int i = 1; i <= n; i++) {
cin >> w[i];
cumw[i] = (cumw[i-1] + w[i]) % mod;
}
stack<int> s;
for (int i = 1; i <= n; i++) {
while (!s.empty() && h[s.top()] > h[i]) s.pop();
if (s.empty()) {
lef[i] = 1;
} else {
lef[i] = s.top() + 1;
}
s.push(i);
}
s = stack<int>();
for (int i = n; i >= 1; i--) {
while (!s.empty() && h[s.top()] >= h[i]) s.pop();
if (s.empty()) {
rit[i] = n;
} else {
rit[i] = s.top() - 1;
}
s.push(i);
}
ll ans = 0;
for (int i = 1; i <= n; i++) {
int l = lef[i], r = rit[i];
ll wleft = 0, wright = 0;
wleft = cumw[i-1] - cumw[l-1];
wright = cumw[r] - cumw[i];
test(wleft, wright, i);
ll waysw = f(wleft + w[i] + wright);
waysw -= f(wleft);
waysw -= f(wright);
waysw = (waysw + 2 * mod) % mod;
ll waysh = f(h[i]);
ans = (ans + mul(waysw, waysh)) %mod ;
}
cout << ans;
}
# | 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... |