#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
const int mod = 1e9 + 7;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n; cin>>n;
vector<int> h(n + 1);
for (int i = 1; i <= n; i++){
cin>>h[i];
}
vector<int> w(n + 1);
vector<ll> p(n + 1);
for (int i = 1; i <= n; i++){
cin>>w[i];
p[i] = p[i - 1] + w[i];
}
auto sum = [&](int l, int r){
return (p[r] - p[l - 1]) % mod;
};
ll out = 0;
for (int i = 1; i <= n; i++){
out += (((1LL * w[i] * (w[i] + 1) / 2) % mod) * ((1LL * h[i] * (h[i] + 1) / 2) % mod)) % mod;
}
vector<int> f(n + 1);
vector<ll> pr(n + 1);
function<void(int, int)> solve = [&](int l, int r){
if (l == r) return;
int m = (l + r) / 2;
f[m] = h[m]; f[m + 1] = h[m + 1];
for (int i = m - 1; i >= l; i--) f[i] = min(f[i + 1], h[i]);
for (int i = m + 2; i <= r; i++) f[i] = min(f[i - 1], h[i]);
pr[m] = 0;
for (int i = m + 1; i <= r; i++){
pr[i] = (pr[i - 1] + ((1LL * f[i] * (f[i] + 1) / 2) % mod * w[i]) % mod) % mod;
}
int j = m + 1;
for (int i = m; i >= l; i--){
while (j <= r && f[j] >= f[i]) j++;
if (j <= r) out += (1LL * w[i] * (pr[r] - pr[j - 1])) % mod;
if (m < j) out += (((1LL * f[i] * (f[i] + 1) / 2) % mod) * ((1LL * w[i] * sum(m + 1, j - 1)) % mod)) % mod;
}
out %= mod;
solve(l, m); solve(m + 1, r);
};
solve(1, n);
cout<<out<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
14 ms |
1832 KB |
Output is correct |
4 |
Correct |
30 ms |
4312 KB |
Output is correct |
5 |
Correct |
29 ms |
4176 KB |
Output is correct |
6 |
Correct |
30 ms |
4176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
4 ms |
592 KB |
Output is correct |
3 |
Correct |
15 ms |
1764 KB |
Output is correct |
4 |
Correct |
35 ms |
4948 KB |
Output is correct |
5 |
Correct |
33 ms |
5196 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
4 ms |
592 KB |
Output is correct |
4 |
Correct |
17 ms |
1616 KB |
Output is correct |
5 |
Correct |
38 ms |
5084 KB |
Output is correct |
6 |
Correct |
39 ms |
5204 KB |
Output is correct |
7 |
Correct |
1 ms |
508 KB |
Output is correct |
8 |
Correct |
4 ms |
852 KB |
Output is correct |
9 |
Correct |
16 ms |
2792 KB |
Output is correct |
10 |
Correct |
32 ms |
4880 KB |
Output is correct |
11 |
Correct |
30 ms |
5080 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
0 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
504 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
13 ms |
1836 KB |
Output is correct |
12 |
Correct |
27 ms |
4180 KB |
Output is correct |
13 |
Correct |
27 ms |
4180 KB |
Output is correct |
14 |
Correct |
27 ms |
4180 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
852 KB |
Output is correct |
17 |
Correct |
15 ms |
2560 KB |
Output is correct |
18 |
Correct |
30 ms |
4948 KB |
Output is correct |
19 |
Correct |
30 ms |
5248 KB |
Output is correct |
20 |
Correct |
1 ms |
496 KB |
Output is correct |
21 |
Correct |
3 ms |
852 KB |
Output is correct |
22 |
Correct |
15 ms |
2644 KB |
Output is correct |
23 |
Correct |
34 ms |
4940 KB |
Output is correct |
24 |
Correct |
30 ms |
4980 KB |
Output is correct |
25 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
3 ms |
852 KB |
Output is correct |
31 |
Correct |
4 ms |
1020 KB |
Output is correct |
32 |
Correct |
15 ms |
2568 KB |
Output is correct |
33 |
Correct |
16 ms |
2644 KB |
Output is correct |
34 |
Correct |
33 ms |
4948 KB |
Output is correct |
35 |
Correct |
34 ms |
4948 KB |
Output is correct |
36 |
Correct |
35 ms |
5116 KB |
Output is correct |
37 |
Correct |
37 ms |
4952 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
34 ms |
5064 KB |
Output is correct |
40 |
Correct |
35 ms |
4964 KB |
Output is correct |
41 |
Correct |
38 ms |
4948 KB |
Output is correct |
42 |
Correct |
31 ms |
4948 KB |
Output is correct |