This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define pb push_back
using ll = long long;
const int MOD = 1e9 + 7;
const int INV2 = 500000004;
template<class T> using V = vector<T>;
template<class T, size_t SZ> using AR = array<T, SZ>;
int main() {
cin.tie(0)->sync_with_stdio(0);
int N; cin >> N;
V<ll> A(N); for(auto& x : A) cin >> x;
V<ll> B(N); for(auto& x : B) cin >> x;
A.pb(0); B.pb(0);
auto sum = [&](ll l, ll r) {
l %= MOD, r %= MOD;
// sum of l + (l + 1) + (l + 2) + ... + r;
ll L = (((l * 1LL * (l - 1)) % MOD) * 1LL * INV2) % MOD; // exclusive L
ll R = (((r * 1LL * (r + 1)) % MOD) * 1LL * INV2) % MOD; // inclusive R
return (R - L + 10LL * MOD) % MOD;
};
V<AR<ll, 2>> stk = {{0, -1}};
ll ans = 0;
for(int i = 0; i <= N; i++) {
ll x = A[i], amt = B[i];
ll w = 0;
while(stk.back()[1] != -1 && stk.back()[0] >= x) {
ll H = stk.back()[0], W = stk.back()[1]; stk.pop_back();
ll h = max(x, stk.back()[0]); amt += W, w += W;
// w blocks width, from h + 1 to H height
ll ways = (sum(h + 1, H) * 1LL * sum(1, w)) % MOD;
// cout << i << " => " << w << " - " << h + 1 << " -> " << H << " ==> " << ways << endl;
ans += ways;
if (ans >= MOD) ans -= MOD;
}
stk.pb({x, amt});
}
cout << ans << nl;
exit(0-0);
}
# | 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... |