// 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) {
int 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 |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |