// 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);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
8 ms |
1628 KB |
Output is correct |
4 |
Correct |
15 ms |
2904 KB |
Output is correct |
5 |
Correct |
18 ms |
2652 KB |
Output is correct |
6 |
Correct |
15 ms |
2648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
12 ms |
1632 KB |
Output is correct |
4 |
Correct |
23 ms |
2784 KB |
Output is correct |
5 |
Correct |
25 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
600 KB |
Output is correct |
4 |
Correct |
10 ms |
1632 KB |
Output is correct |
5 |
Correct |
25 ms |
2652 KB |
Output is correct |
6 |
Correct |
19 ms |
2792 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
832 KB |
Output is correct |
9 |
Correct |
10 ms |
1868 KB |
Output is correct |
10 |
Correct |
19 ms |
5060 KB |
Output is correct |
11 |
Correct |
19 ms |
4924 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
9 ms |
1624 KB |
Output is correct |
12 |
Correct |
15 ms |
2652 KB |
Output is correct |
13 |
Correct |
15 ms |
2652 KB |
Output is correct |
14 |
Correct |
15 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
856 KB |
Output is correct |
17 |
Correct |
10 ms |
1628 KB |
Output is correct |
18 |
Correct |
19 ms |
2652 KB |
Output is correct |
19 |
Correct |
19 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
832 KB |
Output is correct |
22 |
Correct |
10 ms |
1868 KB |
Output is correct |
23 |
Correct |
23 ms |
4920 KB |
Output is correct |
24 |
Correct |
24 ms |
4908 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
3 ms |
604 KB |
Output is correct |
31 |
Correct |
2 ms |
532 KB |
Output is correct |
32 |
Correct |
9 ms |
1628 KB |
Output is correct |
33 |
Correct |
10 ms |
1632 KB |
Output is correct |
34 |
Correct |
20 ms |
2652 KB |
Output is correct |
35 |
Correct |
18 ms |
2652 KB |
Output is correct |
36 |
Correct |
19 ms |
2660 KB |
Output is correct |
37 |
Correct |
21 ms |
2656 KB |
Output is correct |
38 |
Correct |
0 ms |
360 KB |
Output is correct |
39 |
Correct |
19 ms |
2820 KB |
Output is correct |
40 |
Correct |
19 ms |
2664 KB |
Output is correct |
41 |
Correct |
19 ms |
2824 KB |
Output is correct |
42 |
Correct |
25 ms |
3656 KB |
Output is correct |