#include <bits/stdc++.h>
using namespace std;
#define int ll
#define endl '\n' //comment for interactive
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define re resize
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define all1(x) (x).begin()+1, (x).end()
#define loop(i, n) for(int i = 0; i < n; i++)
#define loop1(i, n) for(int i = 1; i <= n; i++)
#define print(x) cout << #x << ": " << x << endl << flush
typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
typedef array<int, 3> ti;
typedef vector<ii> vii;
typedef vector<ti> vti;
typedef vector<vi> vvi;
typedef priority_queue<int> pq;
template<class T> bool ckmin(T&a, T b) { bool B = a > b; a = min(a, b); return B; }
template<class T> bool ckmax(T&a, T b) { bool B = a < b; a = max(a, b); return B; }
const int inf = 1e17;
const int mod = 1e9+7;
struct min_st {
vii tree;
int left, right;
min_st(vi x) : tree(x.size()*4) {
left = 0;
right = x.size()-1;
_build(1, left, right, x);
}
void _build(int node, int l, int r, vi& x) {
if(l == r) {
tree[node] = {x[l], l};
return;
}
int m = (l+r)/2;
_build(node*2, l, m, x);
_build(node*2+1, m+1, r, x);
tree[node] = min(tree[node*2], tree[node*2+1]);
}
ii _query(int node, int l, int r, int ql, int qr) {
if(ql <= l and r <= qr) return tree[node];
if(r < ql or qr < l) return {inf, inf};
int m = (l+r)/2;
return min(
_query(node*2, l, m, ql, qr),
_query(node*2+1, m+1, r, ql, qr));
}
ii query(int ql, int qr) {
return _query(1, left, right, ql, qr);
}
};
int power(ll x, int y) {
int res = 1;
x = x % mod;
while(y > 0) {
if(y & 1)
res = ((ll)res*x) % mod;
y = y >> 1;
x = (x*x) % mod;
}
return res;
}
int inv(int n) {
return power(n, mod-2);
}
int mm(int x, int y) {
x %= mod, y %= mod;
return (x * y) % mod;
}
int nc2(int n) {
return mm(mm(n, (n-1)), inv(2));
}
void solve() {
int n;
cin >> n;
vi h(n+1), w(n+1);
loop1(i, n)
cin >> h[i];
loop1(i, n)
cin >> w[i];
vi pw(n+1);
loop1(i, n) {
pw[i] = pw[i-1] + w[i];
pw[i] %= mod;
}
min_st st(h);
auto calc = [&] (int l, int r, auto calc) -> int {
if(r < l) return 0;
auto [mh, mi] = st.query(l, r);
return
(mm(nc2(mh+1), nc2(w[mi] + 1))
+ mm(nc2(mh+1), mm(pw[mi-1] - pw[l-1], w[mi]))
+ mm(nc2(mh+1), mm(pw[r] - pw[mi], w[mi]))
+ mm(nc2(mh+1), mm(pw[mi-1] - pw[l-1], pw[r] - pw[mi]))
+ calc(l, mi-1, calc)
+ calc(mi+1, r, calc)) % mod;
};
cout << ((calc(1, n, calc) + mod) % mod) << endl;
}
signed main() {
fast_io;
int t = 1; //cin >> t;
while(t--)
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
35 ms |
7452 KB |
Output is correct |
4 |
Correct |
72 ms |
21260 KB |
Output is correct |
5 |
Correct |
65 ms |
15644 KB |
Output is correct |
6 |
Correct |
62 ms |
11404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
7 ms |
2644 KB |
Output is correct |
3 |
Correct |
34 ms |
11588 KB |
Output is correct |
4 |
Correct |
68 ms |
22996 KB |
Output is correct |
5 |
Correct |
69 ms |
25008 KB |
Output is correct |
6 |
Correct |
0 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
7 ms |
2644 KB |
Output is correct |
4 |
Correct |
35 ms |
11660 KB |
Output is correct |
5 |
Correct |
69 ms |
22924 KB |
Output is correct |
6 |
Correct |
70 ms |
25020 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
8 ms |
2764 KB |
Output is correct |
9 |
Correct |
37 ms |
12600 KB |
Output is correct |
10 |
Correct |
67 ms |
24772 KB |
Output is correct |
11 |
Correct |
71 ms |
24904 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
33 ms |
7360 KB |
Output is correct |
12 |
Correct |
67 ms |
21320 KB |
Output is correct |
13 |
Correct |
67 ms |
15720 KB |
Output is correct |
14 |
Correct |
66 ms |
11464 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
7 ms |
2764 KB |
Output is correct |
17 |
Correct |
35 ms |
12560 KB |
Output is correct |
18 |
Correct |
72 ms |
24944 KB |
Output is correct |
19 |
Correct |
71 ms |
25028 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
8 ms |
2772 KB |
Output is correct |
22 |
Correct |
38 ms |
12640 KB |
Output is correct |
23 |
Correct |
69 ms |
24812 KB |
Output is correct |
24 |
Correct |
72 ms |
24872 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
7 ms |
1364 KB |
Output is correct |
31 |
Correct |
7 ms |
1364 KB |
Output is correct |
32 |
Correct |
36 ms |
5828 KB |
Output is correct |
33 |
Correct |
36 ms |
5964 KB |
Output is correct |
34 |
Correct |
67 ms |
11336 KB |
Output is correct |
35 |
Correct |
67 ms |
11348 KB |
Output is correct |
36 |
Correct |
67 ms |
11596 KB |
Output is correct |
37 |
Correct |
67 ms |
11616 KB |
Output is correct |
38 |
Correct |
0 ms |
320 KB |
Output is correct |
39 |
Correct |
70 ms |
11608 KB |
Output is correct |
40 |
Correct |
71 ms |
11592 KB |
Output is correct |
41 |
Correct |
69 ms |
13676 KB |
Output is correct |
42 |
Correct |
76 ms |
24900 KB |
Output is correct |