#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define fs first
#define sc second
const ll mxn = 1e5+10;
const ll mod = 1e9+7;
ll pw(ll a,ll b){
ll re = 1;
while(b){
if(b&1)re = re*a%mod;
a = a*a%mod;
b>>=1;
}
return re;
}
ll inv(ll k){
return pw(k,mod-2);
}
ll mad(ll a,ll b){
a += b;
if(a>=mod)a -= mod;
return a;
}
ll mub(ll a,ll b){
a = a+mod-b;
if(a>=mod)a -= mod;
return a;
}
ll mul(ll a,ll b){
return (a%mod)*(b%mod)%mod;
}
ll dp[mxn];
deque<pll> dq;
ll h[mxn],w[mxn],preh[mxn],prew[mxn];
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
ll n;
cin>>n;
for(int i = 1;i<=n;i++){
cin>>h[i];
preh[i] = mad(preh[i-1],h[i]);
}
for(int i = 1;i<=n;i++){
cin>>w[i];
prew[i] = mad(prew[i-1],w[i]);
}
// cout<<mul(h[n]+1,h[n])*mul(prew[n],prew[n]+1)%mod*inv(4)%mod;return 0;
dq.push_back({0,0});
ll ah = 0;
ll area = 0;
for(ll i = 1;i<=n;i++){
dp[i] = mul(w[i]+1,h[i]+1)*mul(w[i],h[i])%mod*inv(4)%mod;
while(dq.back().fs>h[i]){
ah = mub(ah,mul(dq.back().fs,mul(dq.back().fs,mub(prew[dq.back().sc],prew[dq[dq.size()-2].sc]))));
// ah = mub(ah,mul(dq.back().fs,mul(dq.back().fs,mub(prew[i-1],prew[dq.back().sc-1]))));
area = mub(area,mul(dq.back().fs,mub(prew[dq.back().sc],prew[dq[dq.size()-2].sc])));
// area = mub(area,mul(dq.back().fs,mub(prew[i-1],prew[dq.back().sc-1])));
auto pre = dq.back();
dq.pop_back();
ah = mad(ah,mul(h[i],mul(h[i],mub(prew[pre.sc],prew[dq.back().sc]))));
area = mad(area,mul(h[i],mub(prew[pre.sc],prew[dq.back().sc])));
// ah = mad(ah,mul(h[i],mul(mub(prew[i-1],prew[pre.sc-1]),h[i])));
// area = mad(area,mul(h[i],mub(prew[i-1],prew[pre.sc-1])));
}
// dp[i] = mad(dp[i],mul(mul(w[i],ah),inv(2)));
dp[i] = mad(dp[i],mul(mub(ah,area)*inv(2)%mod,w[i]));
dp[i] = mad(dp[i],mul(area,w[i]));
// cout<<i<<":"<<ah<<' '<<area<<' '<<dp[i]<<endl;
// for(auto &j:dq)cout<<j.sc<<' ';cout<<endl;
ah = mad(ah,mul(h[i],mul(h[i],w[i])));
area = mad(area,mul(h[i],w[i]));
dq.push_back({h[i],i});
}
ll total = 0;
for(int i = 1;i<=n;i++)total = mad(total,dp[i]);
cout<<total;
}
/*
3
1 2 1
1 1 1
5
1 2 3 2 1
1 1 1 1 1
2
1 2
1 2
5
1 2 1 2 1
1 1 1 1 1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 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 |
1 ms |
468 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 |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
20 ms |
3148 KB |
Output is correct |
4 |
Correct |
39 ms |
6832 KB |
Output is correct |
5 |
Correct |
40 ms |
5972 KB |
Output is correct |
6 |
Correct |
40 ms |
5468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
5 ms |
1100 KB |
Output is correct |
3 |
Correct |
21 ms |
4188 KB |
Output is correct |
4 |
Correct |
43 ms |
7852 KB |
Output is correct |
5 |
Correct |
43 ms |
8016 KB |
Output is correct |
6 |
Correct |
0 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 |
5 ms |
1116 KB |
Output is correct |
4 |
Correct |
21 ms |
4188 KB |
Output is correct |
5 |
Correct |
42 ms |
7700 KB |
Output is correct |
6 |
Correct |
43 ms |
8020 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
4 ms |
996 KB |
Output is correct |
9 |
Correct |
22 ms |
4180 KB |
Output is correct |
10 |
Correct |
46 ms |
7720 KB |
Output is correct |
11 |
Correct |
42 ms |
7764 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
348 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 |
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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
21 ms |
3164 KB |
Output is correct |
12 |
Correct |
39 ms |
6760 KB |
Output is correct |
13 |
Correct |
40 ms |
6096 KB |
Output is correct |
14 |
Correct |
41 ms |
5484 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
6 ms |
1116 KB |
Output is correct |
17 |
Correct |
22 ms |
4100 KB |
Output is correct |
18 |
Correct |
43 ms |
8156 KB |
Output is correct |
19 |
Correct |
44 ms |
8024 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
5 ms |
1116 KB |
Output is correct |
22 |
Correct |
23 ms |
4212 KB |
Output is correct |
23 |
Correct |
45 ms |
7760 KB |
Output is correct |
24 |
Correct |
42 ms |
7760 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
5 ms |
860 KB |
Output is correct |
31 |
Correct |
5 ms |
1052 KB |
Output is correct |
32 |
Correct |
22 ms |
3112 KB |
Output is correct |
33 |
Correct |
23 ms |
3160 KB |
Output is correct |
34 |
Correct |
44 ms |
5968 KB |
Output is correct |
35 |
Correct |
44 ms |
5968 KB |
Output is correct |
36 |
Correct |
45 ms |
6228 KB |
Output is correct |
37 |
Correct |
52 ms |
6236 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
44 ms |
6140 KB |
Output is correct |
40 |
Correct |
45 ms |
6224 KB |
Output is correct |
41 |
Correct |
44 ms |
6228 KB |
Output is correct |
42 |
Correct |
44 ms |
6992 KB |
Output is correct |