#ifndef LOCAL
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC diagnostic ignored "-Wpedantic"
#endif
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rnd(52);
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T, typename V>
using table = gp_hash_table<T, V>;
using i128 = __int128;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
const ll INF = 2e18;
const int inf = 1e9;
const int maxn = 1e5 + 7;
const int MOD = 1e9 + 7;
const ld pi = acos(-1);
const int P = 5167;
const int L = 26;
const ld EPS = 0.0005;
const ll K = 40;
const ll back2 = MOD / 2 + 1;
template<typename T, typename V>
void fill(T &container, V value) {
for (auto &c : container)
c = value;
}
#define int ll
int get1(int h, int w) {
ll a = h * (h + 1) / 2;
a %= MOD;
return (a * w) % MOD;
}
int get2(int h, int w) {
int a = h * w, b = h * w + h + w + 1;
a %= MOD;
b %= MOD;
a = (a * back2) % MOD;
b = (b * back2) % MOD;
return (a * b) % MOD;
}
void solve() {
int n;
cin >> n;
vector<int> h(n), w(n);
for (int i = 0; i < n; ++i) cin >> h[i];
for (int i = 0; i < n; ++i) cin >> w[i];
int ans = 0;
int cnt = 0;
vector<pair<int, int> > st;
for (int i = 0; i < n; ++i) {
pair<int, int> curr = {h[i], 0};
while (!st.empty() && st.back().first >= h[i]) {
curr.second += st.back().second;
cnt = (cnt - get1(curr.first, curr.second) + MOD) % MOD;
st.pop_back();
}
st.push_back(curr);
cnt += get1(curr.first, curr.second);
cnt %= MOD;
ans += cnt * w[i];
ans %= MOD;
cnt = (cnt - get1(curr.first, curr.second) + MOD) % MOD;
st.back().second += w[i];
cnt += get1(st.back().first, st.back().second);
cnt %= MOD;
ans %= MOD;
ans += get2(h[i], w[i]);
ans %= MOD;
}
cout << ans << "\n";
}
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(7);
int t = 1;
//cin >> t;
while (t--) solve();
//stress();
}
# | 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... |