This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize "Ofast"
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef tuple<ll,ll,ll> trio;
typedef pair<int,int> pii;
const int MAXN = 1e5+10;
const ll MOD = 1e9+7;
#define all(x) x.begin(), x.end()
#define mk make_pair
#define pb push_back
#define f first
#define s second
ll n, resp, h[MAXN], w[MAXN], ant[MAXN], pos[MAXN], pw[MAXN];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
cin >> h[i];
for(int i = 1; i <= n; i++)
cin >> w[i], pw[i] = pw[i-1] + w[i];
stack<int> val;
val.push(0);
for(int i = 1; i <= n; i++) {
while(h[val.top()] > h[i]) val.pop();
ant[i] = val.top();
val.push(i);
}
while(!val.empty()) val.pop();
val.push(n+1);
for(int i = n; i >= 1; i--) {
while(h[val.top()] >= h[i]) val.pop();
pos[i] = val.top();
val.push(i);
}
for(int i = 1; i <= n; i++) {
ll l = ant[i]+1, r = pos[i]-1;
ll tot = (((pw[r]-pw[l-1]+1)%MOD) * ((pw[r]-pw[l-1])%MOD))/2;
ll casoA = (((pw[r]-pw[i]+1)%MOD) * ((pw[r]-pw[i])%MOD))/2;
ll casoB = (((pw[i-1]-pw[l-1]+1)%MOD) * ((pw[i-1]-pw[l-1])%MOD))/2;
resp += ((tot%MOD - casoA%MOD - casoB%MOD)%MOD) * ((h[i] * (h[i]+1) / 2)%MOD);
resp %= MOD;
}
cout << (resp + MOD) % MOD;
}
// g++ -std=c++17 clara.cpp -o clara
# | 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... |