#include <bits/stdc++.h>
#define ll long long
using namespace std;
int recinta(int N, vector<int> L, vector<int> W){
const ll md = 1e9+7;
ll n = (ll) N;
vector<ll> l, w;
l.resize(n+2);
w.resize(n+2);
l[0] = 0;
w[0] = 0;
l[n+1] = 0;
w[n+1] = 0;
for(ll i = 0; i<n; i++){
l[i+1] = (ll) L[i];
w[i+1] = (ll) W[i];
}
ll sol = 0;
stack<array<ll, 2>> st;
ll curr_w = 0;
for(int i = 0; i<n+2; i++){
ll x_piccola = curr_w;
while(!st.empty() && st.top()[0] > l[i]){
x_piccola = st.top()[1];
ll hc = st.top()[0];
ll wtmp = (((curr_w-x_piccola)%md)+md)%md;
st.pop();
hc = (hc*(hc+1) %md) * 500000004 %md;
ll h_basso = max(st.top()[0], l[i]);
hc -= (h_basso*(h_basso+1) %md) * 500000004 %md;
hc += md; hc%=md;
sol += (((wtmp +1)*(wtmp) % md) * hc %md) * 500000004 %md;
}
if(st.empty() || st.top()[0] != l[i]){
st.push({l[i], x_piccola});
}
curr_w += w[i];
curr_w %= md;
}
sol %=md;
return sol;
}
int main(){
int N;
cin >> N;
vector<int> L(N), W(N);
for(int &x: L) cin >> x;
for(int &x: W) cin >> x;
cout << recinta(N, L, W) << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |