#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define pb push_back
#define mp make_pair
typedef pair<ll,ll> pii;
typedef tree<ll ,null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
const ll MAXN = 450;
const ll MOD = 1e9+7;
const ll INF = 1e9+10;
ll calc(ll n, ll m){
m%=MOD; n%=MOD;
ll res = (n%MOD*(n+1)%MOD)%MOD;
if(res%2==1)res+=MOD;
res/=2;
res%=MOD;
ll k = (m%MOD*(m+1)%MOD)%MOD;
if(k%2==1)k+=MOD;
k/=2;
k%=MOD;
res*=k;
res%=MOD;
if(res<0)res+=MOD;
return res;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n,i,k,ans=0;
cin>>n;
ll h[n+2],w[n+2],dp[n+2]; dp[0]=0;
for(i=1;i<=n;i++)cin>>h[i];
for(i=1;i<=n;i++){
cin>>w[i];
dp[i] = dp[i-1]+w[i];
}
stack <ll>st;
ll left[n+1],right[n+1];
bool meh[n+1];
for(i=1;i<=n;i++){
meh[i]=true;
while(!st.empty()&&h[st.top()]>=h[i]){
if(h[st.top()]==h[i])meh[i]=false;
st.pop();
}
if(!st.empty()){
left[i]=st.top();
}else{
left[i]=0;
}
st.push(i);
//cout<<left[i]<<" ";
}//cout<<endl;
while(!st.empty())st.pop();
for(i=n;i>=1;i--){
while(!st.empty()&&h[st.top()]>=h[i]){
st.pop();
}
if(!st.empty()){
right[i]=st.top();
}else{
right[i]=n+1;
}
st.push(i);
}
h[0]=0;
h[n+1]=0;
for(i=1;i<=n;i++){
if(meh[i]==false)continue;
//k é a soma no intervalo
k = dp[right[i]-1] - dp[left[i]];
ans += calc(k,h[i]);
ans -= calc(k , max(h[left[i]], h[right[i]]) );
//cout<<i<<" "<<k<<" "<<h[i]<<" "<<left[i]<<" "<<right[i];
//cout<<" "<<h[left[i]]<<" "<<h[right[i]]<<endl;
ans%=MOD;
}
/*
6
1 2 3 2 2 1
1 1 1 1 1 1
*/
if(ans<0)ans+=MOD;
cout<<ans<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
13 ms |
2888 KB |
Output is correct |
4 |
Correct |
24 ms |
5092 KB |
Output is correct |
5 |
Correct |
25 ms |
5084 KB |
Output is correct |
6 |
Correct |
29 ms |
5188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
4 ms |
844 KB |
Output is correct |
3 |
Correct |
15 ms |
3020 KB |
Output is correct |
4 |
Correct |
28 ms |
5044 KB |
Output is correct |
5 |
Correct |
34 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
4 ms |
844 KB |
Output is correct |
4 |
Correct |
19 ms |
3204 KB |
Output is correct |
5 |
Correct |
28 ms |
5100 KB |
Output is correct |
6 |
Correct |
32 ms |
5172 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
4 ms |
972 KB |
Output is correct |
9 |
Correct |
16 ms |
3384 KB |
Output is correct |
10 |
Correct |
31 ms |
6864 KB |
Output is correct |
11 |
Correct |
32 ms |
6996 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 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 |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
14 ms |
2880 KB |
Output is correct |
12 |
Correct |
25 ms |
5460 KB |
Output is correct |
13 |
Correct |
26 ms |
5448 KB |
Output is correct |
14 |
Correct |
24 ms |
5460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
4 ms |
844 KB |
Output is correct |
17 |
Correct |
15 ms |
3272 KB |
Output is correct |
18 |
Correct |
28 ms |
6216 KB |
Output is correct |
19 |
Correct |
31 ms |
6352 KB |
Output is correct |
20 |
Correct |
1 ms |
324 KB |
Output is correct |
21 |
Correct |
4 ms |
964 KB |
Output is correct |
22 |
Correct |
16 ms |
3404 KB |
Output is correct |
23 |
Correct |
31 ms |
6848 KB |
Output is correct |
24 |
Correct |
32 ms |
7000 KB |
Output is correct |
25 |
Correct |
1 ms |
204 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 |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
4 ms |
904 KB |
Output is correct |
31 |
Correct |
4 ms |
844 KB |
Output is correct |
32 |
Correct |
16 ms |
3196 KB |
Output is correct |
33 |
Correct |
17 ms |
3256 KB |
Output is correct |
34 |
Correct |
32 ms |
5960 KB |
Output is correct |
35 |
Correct |
34 ms |
6056 KB |
Output is correct |
36 |
Correct |
34 ms |
6348 KB |
Output is correct |
37 |
Correct |
37 ms |
6212 KB |
Output is correct |
38 |
Correct |
1 ms |
320 KB |
Output is correct |
39 |
Correct |
32 ms |
6208 KB |
Output is correct |
40 |
Correct |
31 ms |
6236 KB |
Output is correct |
41 |
Correct |
32 ms |
6348 KB |
Output is correct |
42 |
Correct |
32 ms |
6588 KB |
Output is correct |