#include<bits/stdc++.h>
using namespace std;
#define left(id) (id << 1)
#define right(id) ((id << 1) | 1)
#define mid(l,r) ((l + r) >> 1)
const int N = 1e5 + 5;
const int MOD = 1e9 + 7;
int n, h[N], w[N];
int l[N], r[N];
long long t[N];
struct DATA{
int id, val;
};
bool cmp(DATA A, DATA B){
return A.val < B.val;
}
vector<DATA> a;
int calc(long long len){
if (len <= 0) return 0;
__int128 x = (__int128)len;
__int128 res = x * (x + 1) / 2;
res %= MOD;
return (int)res;
}
int st[N * 4];
int lz[N * 4];
long long sum(int l, int r){
if (l > r) return 0;
return t[r] - t[l - 1];
}
void down(int id, int l, int r){
if (lz[id] == 0) return;
if (l == r){
lz[id] = 0;
return;
}
int m = mid(l, r);
int lc = left(id), rc = right(id);
st[lc] = lz[id];
st[rc] = lz[id];
lz[lc] = lz[id];
lz[rc] = lz[id];
lz[id] = 0;
}
void upd(int id, int l, int r, int u, int v, int val){
if (v < l || r < u) return;
down(id, l, r);
if (u <= l && r <= v){
st[id] = val;
lz[id] = val;
return;
}
int m = mid(l, r);
upd(left(id), l, m, u, v, val);
upd(right(id), m + 1, r, u, v, val);
}
int Get(int id, int l, int r, int pos){
if (l == r) return st[id];
down(id, l, r);
int m = mid(l, r);
if (pos <= m) return Get(left(id), l, m, pos);
else return Get(right(id), m + 1, r, pos);
}
void SOLVE(){
cin >> n;
for (int i = 1; i <= n; i++)
cin >> h[i];
for (int i = 1; i <= n; i++){
cin >> w[i];
t[i] = t[i - 1] + w[i];
}
stack<int> st1;
st1.push(0);
for (int i = 1; i <= n; i++){
while (h[st1.top()] >= h[i])
st1.pop();
l[i] = st1.top() + 1;
st1.push(i);
}
stack<int> st2;
st2.push(n + 1);
for (int i = n; i >= 1; i--){
while (h[st2.top()] >= h[i])
st2.pop();
r[i] = st2.top() - 1;
st2.push(i);
}
for (int i = 1; i <= n; i ++)
a.push_back({i, h[i]});
sort(a.begin(), a.end(), cmp);
long long ans = 0;
for (auto i : a){
int id = i.id;
int val = i.val;
long long len = sum(l[id], r[id]);
int r1 = calc(len);
int h1 = Get(1, 1, n, id);
int r2 = calc(h1);
long long s = 1ll * r1 * calc(val) % MOD;
long long f = 1ll * r2 * r1 % MOD;
long long add = s - f;
add %= MOD;
if (add < 0) add += MOD;
ans += add;
if (ans >= MOD) ans -= MOD;
upd(1, 1, n, l[id], r[id], val);
}
cout << ans;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define TASK "hist"
if (fopen(TASK".inp","r")){
freopen(TASK".inp","r",stdin);
freopen(TASK".out","w",stdout);
}
int nTest = 1;
// cin >> nTest;
while (nTest--){
SOLVE();
}
return 0;
}
Compilation message (stderr)
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:137:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
137 | freopen(TASK".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
fancyfence.cpp:138:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
138 | freopen(TASK".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~| # | 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... |