| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1282913 | red_souls | Fancy Fence (CEOI20_fancyfence) | C++20 | 1 ms | 580 KiB |
#include <bits/stdc++.h>
#define ll long long
#define task "Histogram"
using namespace std;
const int N = 1e5 + 16;
const ll INF = 1e18, mod = 1e9 + 7;
int n;
ll w[N], h[N];
namespace sub6 {
ll result, prefix[N];
stack <int> st;
int L[N], R[N];
ll sqr(ll k) {
return k * k % mod;
}
ll powMod(ll a, ll b) {
if (b == 0) {
return 1;
}
if (b & 1) {
return a * (sqr(powMod(a, b >> 1)) % mod) % mod;
}
return sqr(powMod(a, b >> 1)) % mod;
}
ll inv2, inv6;
void solve() {
inv2 = powMod(2, mod - 2);
inv6 = powMod(6, mod - 2);
while (!st.empty()) {
st.pop();
}
for (int i = 1; i <= n; i++) {
while (!st.empty() && h[st.top()] >= h[i]) {
st.pop();
}
if (st.empty()) {
L[i] = 1;
}
else {
L[i] = st.top() + 1;
}
st.push(i);
}
while (!st.empty()) {
st.pop();
}
for (int i = n; i >= 1; i--) {
while (!st.empty() && h[st.top()] > h[i]) {
st.pop();
}
if (st.empty()) {
R[i] = n;
}
else {
R[i] = st.top() - 1;
}
st.push(i);
}
for (int i = 1; i <= n; i++) {
prefix[i] = prefix[i - 1] + w[i];
}
for (int i = 1; i <= n; i++) {
ll A = prefix[L[i]];
ll B = prefix[n] - prefix[R[i]];
ll s = w[i] * A % mod * B % mod;
ll c1 = ((w[i] * (w[i] - 1) % mod) * inv2) % mod;
ll c2 = ((w[i] * (w[i] + 1) % mod) * inv2) % mod;
ll c3 = (w[i] * (w[i] + 1) % mod * (w[i] - 1) % mod) % mod * inv6 % mod;
s = (s + A * c1 % mod) % mod;
s = (s + B * c2 % mod) % mod;
s = (s + c3) % mod;
ll z = h[i] * (h[i] + 1) % mod * inv2 % mod;
result = (result + s * z % mod) % mod;
}
cout << result;
}
}
int main() {
ios_base :: sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> h[i];
}
for (int i = 1; i <= n; i++) {
cin >> w[i];
}
sub6 :: solve();
return 0;
}
Compilation message (stderr)
| # | 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... | ||||
