#include<bits/stdc++.h>
#define vi vector<int>
#define vll vector<long long int>
#define vpii vector<pair<int,int>>
#define vpll vector<pair<long long int, long long int>>
#define pb push_back
#define f0r(i,n) for(int i = 0;i<n;i++)
using namespace std;
typedef long long int ll;
const int mod = 1e9 + 7;
const int mxn = 1e5 + 5;
const int lg = floor(log2(mxn));
ll w[mxn];
ll h[mxn];
pair<ll,ll> minh[lg][mxn];
ll c2(ll x){
return x * (x+1) / 2 % mod;
}
ll ans(ll w, ll h){
return c2(w) * c2(h) % mod;
}
pair<ll,ll> minq(int l, int r){
int sz = r - l + 1;
int curlg = floor(log2(sz));
ll se;
if(minh[curlg][l].first < minh[curlg][r - (int)pow(2, curlg) + 1].first){
se = minh[curlg][l].second;
}
else se = minh[curlg][r - (int)pow(2, curlg) + 1].second;
return {min(minh[curlg][l].first, minh[curlg][r - (int)pow(2, curlg) + 1].first), se};
}
ll answer = 0;
ll ps[mxn];
ll sumw(int l, int r){
if(r < l)return 0;
return (ps[r+1] - ps[l]) % mod;
}
int cnt = 0;
void solve(int l, int r){
cnt++;
if(l > r)return;
pair<ll,ll>cur = minq(l, r);
//cout<<cur.first<<' '<<cur.second<<'\n';
ll curh = cur.first;
ll dex = cur.second;
//cout<<l<<' '<<r<<' '<<dex<<'\n';
answer += c2(curh) * sumw(l, dex-1) % mod * sumw(dex+1, r) % mod + (sumw(l, dex-1) + sumw(dex+1, r)) % mod * c2(curh) % mod * w[dex] % mod + c2(w[dex]) * c2(curh) % mod;
answer %= mod;
//cout<<c2(curh) * sumw(l, dex) * sumw(dex, r)<<'\n';
//cout<<answer<<'\n';
if(l != r){
if(dex == l){
//cout<<'e'<<'\n';
solve(l+1, r);
}
else if(dex == r)solve(l, r-1);
else{
solve(l, dex - 1);
solve(dex + 1, r);
}
}
}
int main(){
int n;
cin>>n;
f0r(i,n)cin>>h[i];
f0r(i,n)cin>>w[i];
f0r(i,n)minh[0][i] = {h[i], i};
ps[0] = 0;
f0r(i, n){
ps[i+1] = ps[i] + w[i];
}
for(int l = 1;l<=floor(log2(n));l++){
f0r(i, n - pow(2, l) + 1){
ll fi;
ll se;
if(minh[l-1][i].first < minh[l-1][i + (int)pow(2, l-1)].first){
se = minh[l-1][i].second;
}
else se = minh[l-1][i + (int)pow(2, l-1)].second;
fi = min(minh[l-1][i].first, minh[l-1][i + (int)pow(2, l-1)].first);
minh[l][i] = {fi, se};
}
}
/*
ll dp[n+1];
dp[0] = ans(w[0], h[0]);
for(int i = 1;i<n;i++){
ll rm = h[i];
ll thing = 0;
for(int j = i-1;j>=0;j--){
rm = min(rm, h[j]);
thing += c2(rm) * w[j] % mod;
thing %= mod;
}
dp[i] = ((dp[i-1] + ans(w[i], h[i])) % mod + thing * w[i] % mod) % mod;
}
//for(auto u : dp)cout<<u<<' ';
//cout<<'\n';
cout<<dp[n-1];
*/
solve(0, n-1);
cout<<answer;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
4 ms |
18780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
3 ms |
18780 KB |
Output is correct |
3 |
Correct |
83 ms |
28760 KB |
Output is correct |
4 |
Correct |
156 ms |
29964 KB |
Output is correct |
5 |
Correct |
167 ms |
30248 KB |
Output is correct |
6 |
Correct |
158 ms |
29776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
18780 KB |
Output is correct |
2 |
Correct |
17 ms |
25296 KB |
Output is correct |
3 |
Correct |
84 ms |
28360 KB |
Output is correct |
4 |
Correct |
172 ms |
29716 KB |
Output is correct |
5 |
Correct |
176 ms |
29832 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
3 ms |
18780 KB |
Output is correct |
3 |
Correct |
17 ms |
25296 KB |
Output is correct |
4 |
Correct |
84 ms |
28328 KB |
Output is correct |
5 |
Correct |
173 ms |
29588 KB |
Output is correct |
6 |
Correct |
179 ms |
29852 KB |
Output is correct |
7 |
Correct |
3 ms |
18896 KB |
Output is correct |
8 |
Correct |
17 ms |
25180 KB |
Output is correct |
9 |
Correct |
84 ms |
28396 KB |
Output is correct |
10 |
Correct |
143 ms |
29776 KB |
Output is correct |
11 |
Correct |
146 ms |
29704 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
3 ms |
18780 KB |
Output is correct |
3 |
Correct |
2 ms |
8648 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
3 ms |
19032 KB |
Output is correct |
9 |
Correct |
3 ms |
18776 KB |
Output is correct |
10 |
Correct |
4 ms |
18884 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
3 ms |
16732 KB |
Output is correct |
13 |
Correct |
3 ms |
18924 KB |
Output is correct |
14 |
Correct |
3 ms |
18776 KB |
Output is correct |
15 |
Correct |
3 ms |
18780 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
4 ms |
18780 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
4 ms |
18780 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
10692 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
12744 KB |
Output is correct |
10 |
Correct |
3 ms |
18780 KB |
Output is correct |
11 |
Correct |
76 ms |
28756 KB |
Output is correct |
12 |
Correct |
168 ms |
29740 KB |
Output is correct |
13 |
Correct |
164 ms |
30400 KB |
Output is correct |
14 |
Correct |
160 ms |
29408 KB |
Output is correct |
15 |
Correct |
3 ms |
18900 KB |
Output is correct |
16 |
Correct |
20 ms |
25220 KB |
Output is correct |
17 |
Correct |
87 ms |
28252 KB |
Output is correct |
18 |
Correct |
177 ms |
29620 KB |
Output is correct |
19 |
Correct |
188 ms |
29856 KB |
Output is correct |
20 |
Correct |
4 ms |
18952 KB |
Output is correct |
21 |
Correct |
18 ms |
25236 KB |
Output is correct |
22 |
Correct |
89 ms |
28192 KB |
Output is correct |
23 |
Correct |
146 ms |
29472 KB |
Output is correct |
24 |
Correct |
156 ms |
29524 KB |
Output is correct |
25 |
Correct |
2 ms |
12636 KB |
Output is correct |
26 |
Correct |
2 ms |
16732 KB |
Output is correct |
27 |
Correct |
4 ms |
18780 KB |
Output is correct |
28 |
Correct |
4 ms |
18940 KB |
Output is correct |
29 |
Correct |
5 ms |
18776 KB |
Output is correct |
30 |
Correct |
18 ms |
25180 KB |
Output is correct |
31 |
Correct |
17 ms |
25176 KB |
Output is correct |
32 |
Correct |
82 ms |
28268 KB |
Output is correct |
33 |
Correct |
79 ms |
28184 KB |
Output is correct |
34 |
Correct |
158 ms |
29520 KB |
Output is correct |
35 |
Correct |
165 ms |
29528 KB |
Output is correct |
36 |
Correct |
165 ms |
29596 KB |
Output is correct |
37 |
Correct |
163 ms |
29572 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
171 ms |
29720 KB |
Output is correct |
40 |
Correct |
161 ms |
29524 KB |
Output is correct |
41 |
Correct |
160 ms |
29748 KB |
Output is correct |
42 |
Correct |
160 ms |
29716 KB |
Output is correct |