This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* author: NimaAryan
* created: 2023-12-01 09:55:42
**/
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#endif
using i64 = long long;
constexpr i64 M = 1E9 + 7;
void add(i64& x, i64 y) {
if ((x += y) >= M) {
x -= M;
}
}
i64 C(i64 h, i64 w) {
return ((h * (h + 1) / 2) % M) * ((w * (w + 1) / 2) % M) % M;
}
i64 F(i64 h) {
return (h * (h + 1) / 2) % M;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<i64> h(N);
for (int i = 0; i < N; ++i) {
cin >> h[i];
}
vector<i64> w(N);
for (int i = 0; i < N; ++i) {
cin >> w[i];
}
vector<int> L(N);
vector<int> stk;
for (int i = 0; i < N; ++i) {
while (!stk.empty() && h[stk.back()] >= h[i]) {
stk.pop_back();
}
L[i] = stk.empty() ? -1 : stk.back();
stk.push_back(i);
}
vector<i64> sum(N + 1);
for (int i = 0; i < N; ++i) {
sum[i + 1] = sum[i] + w[i];
}
auto range_sum = [&](int l, int r) {
return (sum[r] - sum[l]) % M;
};
vector<i64> mono(N), vain(N);
for (int i = 0; i < N; ++i) {
add(mono[i], C(h[i], w[i]));
add(mono[i], F(h[i]) * w[i] % M * range_sum(L[i] + 1, i) % M);
if (L[i] != -1) {
add(mono[i], w[i] * vain[L[i]] % M);
}
add(vain[i], F(h[i]) * w[i] % M);
add(vain[i], F(h[i]) * range_sum(L[i] + 1, i) % M);
if (L[i] != -1) {
add(vain[i], vain[L[i]]);
}
}
cout << accumulate(mono.begin(), mono.end(), 0LL) % M << "\n";
return 0;
}
# | 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... |