#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
const int maxN=1e5+5,MOD=1e9+7,REVMOD=250000002;
const ll inf=2e18;
int n,l[maxN+1],r[maxN+1];
ll h[maxN+1],w[maxN+1],s[maxN+1];
stack<int> st;
void clr()
{
while(!st.empty())
{
st.pop();
}
}
ll mul(ll x,ll y)
{
return (x*y)%MOD;
}
ll sc(ll x1,ll x2)
{
ll tmp=mul(x1+1,x2+1);
tmp--;
if(tmp<0)
{
tmp+=MOD;
}
return tmp;
}
ll calc(ll n,ll m)
{
return mul(mul(mul(1LL*n,1LL*(n+1)),mul(1LL*m,1LL*(m+1))),REVMOD);
}
ll get(ll x1,ll x2,int i)
{
return mul((mul(w[i]-1,x1+x2)+sc(x1,x2))%MOD,calc(1,h[i]));
}
int main()
{
//freopen("","r",stdin);
//freopen("","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>h[i];
}
s[0]=0;
for(int i=1;i<=n;i++)
{
cin>>w[i];
s[i]=(s[i-1]+w[i])%MOD;
}
clr();
for(int i=1;i<=n;i++)
{
while(!st.empty()&&h[st.top()]>=h[i])
{
st.pop();
}
if(st.empty())
{
l[i]=1;
}
else l[i]=st.top()+1;
st.push(i);
}
clr();
for(int i=n;i>=1;i--)
{
while(!st.empty()&&h[st.top()]>h[i])
{
st.pop();
}
if(st.empty())
{
r[i]=n;
}
else r[i]=st.top()-1;
st.push(i);
}
ll ans=0;
for(int i=1;i<=n;i++)
{
ll tmp1=s[i-1]-s[l[i]-1],tmp2=s[r[i]]-s[i];
ans=(ans+get(tmp1,tmp2,i))%MOD;
ans=(ans+calc(h[i],w[i]))%MOD;
//cout<<i<<" "<<l[i]<<" "<<r[i]<<" "<<tmp1<<" "<<tmp2<<" "<<get(tmp1,tmp2,i)<<'\n';
}
cout<<ans;
}
/*5
5 9 49 32 4
1 1 1 1 1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
7 ms |
3560 KB |
Output is correct |
4 |
Correct |
11 ms |
4956 KB |
Output is correct |
5 |
Correct |
12 ms |
4732 KB |
Output is correct |
6 |
Correct |
11 ms |
4572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
2 ms |
2908 KB |
Output is correct |
3 |
Correct |
7 ms |
4180 KB |
Output is correct |
4 |
Correct |
14 ms |
5876 KB |
Output is correct |
5 |
Correct |
16 ms |
5980 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2908 KB |
Output is correct |
4 |
Correct |
7 ms |
4188 KB |
Output is correct |
5 |
Correct |
25 ms |
5828 KB |
Output is correct |
6 |
Correct |
26 ms |
5980 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
2908 KB |
Output is correct |
9 |
Correct |
9 ms |
4172 KB |
Output is correct |
10 |
Correct |
14 ms |
5724 KB |
Output is correct |
11 |
Correct |
16 ms |
5724 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
6 ms |
3656 KB |
Output is correct |
12 |
Correct |
11 ms |
4956 KB |
Output is correct |
13 |
Correct |
12 ms |
4700 KB |
Output is correct |
14 |
Correct |
11 ms |
4700 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
2 ms |
2892 KB |
Output is correct |
17 |
Correct |
7 ms |
4188 KB |
Output is correct |
18 |
Correct |
16 ms |
5744 KB |
Output is correct |
19 |
Correct |
15 ms |
5872 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
7 ms |
4208 KB |
Output is correct |
23 |
Correct |
16 ms |
5724 KB |
Output is correct |
24 |
Correct |
14 ms |
5936 KB |
Output is correct |
25 |
Correct |
0 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2648 KB |
Output is correct |
27 |
Correct |
0 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
0 ms |
2396 KB |
Output is correct |
30 |
Correct |
2 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
8 ms |
3932 KB |
Output is correct |
33 |
Correct |
8 ms |
3932 KB |
Output is correct |
34 |
Correct |
17 ms |
5212 KB |
Output is correct |
35 |
Correct |
16 ms |
5212 KB |
Output is correct |
36 |
Correct |
15 ms |
5468 KB |
Output is correct |
37 |
Correct |
18 ms |
5468 KB |
Output is correct |
38 |
Correct |
0 ms |
2396 KB |
Output is correct |
39 |
Correct |
14 ms |
5516 KB |
Output is correct |
40 |
Correct |
14 ms |
5468 KB |
Output is correct |
41 |
Correct |
14 ms |
5352 KB |
Output is correct |
42 |
Correct |
14 ms |
5724 KB |
Output is correct |