#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define pii pair<int, int>
#define xx first
#define yy second
#define MAXN 100010
#define MOD 1000000007
using namespace std;
ll two=500000004; //inv
ll n, dp[MAXN], pos[MAXN], h[MAXN], w[MAXN], pref[MAXN], cnt[MAXN], ress;
ll add(ll x, ll y)
{
ll ret=x+y;
if (ret>=MOD) ret-=MOD;
return ret;
}
ll mul(ll x, ll y)
{
ll ret=x*y;
if (ret>=MOD) ret%=MOD;
return ret;
}
ll sub(ll x, ll y)
{
ll ret=x-y;
if (ret<=0) ret+=MOD;
return ret;
}
ll sum(ll x, ll y)
{
ll ret=mul(mul(y, add(y, 1)), two);
//ret-=mul(mul(x-1, x), two);
//if (ret<0) ret+=MOD;
return ret;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n;
for (int i=1; i<=n; i++) cin>>h[i];
for (int i=1; i<=n; i++) cin>>w[i];
stack<pii> st;
st.push({-1, 0});
for (int i=1; i<=n; i++)
{
while (st.top().xx>=h[i]) st.pop();
pos[i]=st.top().yy;
st.push({h[i], i});
}
pref[0]=0;
for (int i=1; i<=n; i++) pref[i]=add(pref[i-1], w[i]);
ll x, y;
//cnt[1]=sum(1, h[1]);
for (int i=1; i<=n; i++)
{
int ind=pos[i];
cnt[i]=cnt[ind];
x=sub(pref[i], pref[ind]);
y=sum(1, h[i]);
cnt[i]=add(cnt[i], mul(x, y));
}
//dp[1]=mul(sum(1, w[1]), sum(1, h[1]));
for (int i=1; i<=n; i++)
{
int ind=pos[i];
dp[i]=mul(cnt[ind], w[i]);
x=mul(sub(pref[i-1], pref[ind]), w[i]);
y=sum(1, h[i]);
dp[i]=add(dp[i], mul(x, y));
x=sum(1, w[i]);
y=sum(1, h[i]);
dp[i]=add(dp[i], mul(x, y));
}
for (int i=1; i<=n; i++) { ress=add(dp[i], ress); /*cout<<cnt[i]<<" "<<dp[i]<<endl;*/ }
//cout<<endl;
cout<<ress;
}
/*
3
1 2 1
1 2 3
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
10 ms |
3140 KB |
Output is correct |
4 |
Correct |
20 ms |
6096 KB |
Output is correct |
5 |
Correct |
28 ms |
6104 KB |
Output is correct |
6 |
Correct |
30 ms |
6108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
3 ms |
716 KB |
Output is correct |
3 |
Correct |
14 ms |
2568 KB |
Output is correct |
4 |
Correct |
24 ms |
4932 KB |
Output is correct |
5 |
Correct |
27 ms |
4908 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
3 ms |
716 KB |
Output is correct |
4 |
Correct |
11 ms |
2672 KB |
Output is correct |
5 |
Correct |
23 ms |
4888 KB |
Output is correct |
6 |
Correct |
33 ms |
4992 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
3 ms |
988 KB |
Output is correct |
9 |
Correct |
20 ms |
3744 KB |
Output is correct |
10 |
Correct |
26 ms |
7616 KB |
Output is correct |
11 |
Correct |
29 ms |
7712 KB |
Output is correct |
12 |
Correct |
1 ms |
296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
300 KB |
Output is correct |
4 |
Correct |
0 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
352 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
356 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
0 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
304 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
15 ms |
3140 KB |
Output is correct |
12 |
Correct |
24 ms |
6096 KB |
Output is correct |
13 |
Correct |
21 ms |
6104 KB |
Output is correct |
14 |
Correct |
26 ms |
6056 KB |
Output is correct |
15 |
Correct |
1 ms |
352 KB |
Output is correct |
16 |
Correct |
3 ms |
988 KB |
Output is correct |
17 |
Correct |
18 ms |
3528 KB |
Output is correct |
18 |
Correct |
28 ms |
6884 KB |
Output is correct |
19 |
Correct |
32 ms |
6988 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
3 ms |
972 KB |
Output is correct |
22 |
Correct |
20 ms |
3736 KB |
Output is correct |
23 |
Correct |
25 ms |
7552 KB |
Output is correct |
24 |
Correct |
29 ms |
7620 KB |
Output is correct |
25 |
Correct |
1 ms |
320 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
324 KB |
Output is correct |
29 |
Correct |
2 ms |
332 KB |
Output is correct |
30 |
Correct |
3 ms |
972 KB |
Output is correct |
31 |
Correct |
3 ms |
972 KB |
Output is correct |
32 |
Correct |
20 ms |
3532 KB |
Output is correct |
33 |
Correct |
15 ms |
3524 KB |
Output is correct |
34 |
Correct |
35 ms |
6668 KB |
Output is correct |
35 |
Correct |
25 ms |
6640 KB |
Output is correct |
36 |
Correct |
27 ms |
6872 KB |
Output is correct |
37 |
Correct |
26 ms |
6880 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
36 ms |
6848 KB |
Output is correct |
40 |
Correct |
40 ms |
6836 KB |
Output is correct |
41 |
Correct |
26 ms |
6980 KB |
Output is correct |
42 |
Correct |
23 ms |
7248 KB |
Output is correct |