# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1116436 | jamesbamber | Fancy Fence (CEOI20_fancyfence) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <vector>
#include <stack>
#include <cassert>
#include <iostream>
using namespace std;
constexpr int MOD = 1e9+7;
int main(){
int N; cin >> N;
vector<int> L(N), W(N);
for(int &x: L) cin >> x;
for(int &x: W) cin >> x;
L.resize(N+2);
W.resize(N+2);
for(int i=N; i>0; i--) L[i] = L[i-1]; L[0] = 0;
for(int i=N; i>0; i--) W[i] = W[i-1]; W[0] = 0;
N+=2;
vector<int> pf(N+1);
for(int i=0; i<N; i++) pf[i+1] = (pf[i] + W[i])%MOD;
auto count_rect = [&](long long l_high, long long l_low, long long w){
long long l = l_high - l_low;
return ((w*(w+1)/2)%MOD) * ((l*(l+1)/2)%MOD + l*l_low%MOD)%MOD;
};
long long ans = 0;
stack<int> st;
for(int i=0; i<N; i++){
while(st.size() and L[st.top()] >= L[i]){
int j = st.top(); st.pop();
if(j == 0) break;
int l2 = L[j];
int l1 = max(L[i], L[st.top()]);
int w = (pf[i] - pf[st.top()+1] + MOD)%MOD;
ans += count_rect(l2, l1, w);
ans %= MOD;
}
st.push(i);
}
assert(st.size() == 1);
cout ans;
}