QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#398922#8282. MirrorsHHY_zZhu#WA 1ms3620kbC++232.6kb2024-04-25 19:54:312024-04-25 19:54:38

Judging History

This is the latest submission verdict.

  • [2024-04-25 19:54:38]
  • Judged
  • Verdict: WA
  • Time: 1ms
  • Memory: 3620kb
  • [2024-04-25 19:54:31]
  • Submitted

answer

#include <bits/stdc++.h>

using ll = long long int;
constexpr int MAXN = 5e5 + 19;

int n;
ll h[MAXN];

int m;
struct Node {
    bool is_lower;
    int p;
    ll v;
} node[MAXN];

ll count(int l, int r) {
    int R = n;
    if (r + 1 <= m) {
        R = node[r + 1].p;
    }
    R = R - node[r].p;

    int L = 1;
    if (l - 1 >= 1) {
        L = node[l - 1].p;
    }
    L = node[r].p - L;

    return (ll)L * R;
}

ll presum(int n) {
    return (ll)n * (n + 1) / 2;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin >> n;
    for (int i = 1; i <= n; ++i) {
        std::cin >> h[i];
    }

    for (int l = 1, r; l <= n; l = r + 1) {
        r = l;
        while (r + 1 <= n && h[r] >= h[r + 1]) {
            ++r;
        }
        if (l < r) {
            if (l != 1) {
                ll lim = h[l] + h[l + 1];
                if (h[l] > h[l + 1])  {
                    lim = std::min(lim, h[l - 1] + h[l]);
                }
                ++m;
                node[m].is_lower = true;
                node[m].p = l;
                node[m].v = lim;
            }
            if (r != n) {
                ll lim = h[r] + h[r - 1];
                if (h[r] < h[r - 1]) {
                    lim = std::max(lim, h[r + 1] + h[r]);
                }
                ++m;
                node[m].is_lower = false;
                node[m].p = r;
                node[m].v = lim;
            }
        }
    }

    // for (int i = 1; i <= m; ++i) {
    //     std::cout << node[i].is_lower << ' ' << node[i].p << ' ' << node[i].v << '\n';
    // }

    if (m == 0) {
        std::cout << presum(n - 1) << '\n';
        return 0;
    }

    ll ans = presum(node[1].p - 1) + presum(n - node[m].p);
    for (int i = 1; i + 1 <= m; ++i) {
        ans += presum(node[i + 1].p - node[i].p);
    }

    int ptr = 1;
    std::multiset<ll> L, R;
    L.insert(0LL);

    auto check = [&]() {
        if (L.empty() || R.empty()) {
            return true;
        }
        return *--L.end() < *R.begin();
    };

    for (int i = 1; i <= m; ++i) {
        if (node[i].is_lower) {
            L.insert(node[i].v);
        } else {
            R.insert(node[i].v);
        }
        while (!check()) {
            if (node[ptr].is_lower) {
                L.erase(L.find(node[ptr].v));
            } else {
                R.erase(R.find(node[ptr].v));
            }
            ++ptr;
        }
        ans += count(ptr, i);
    }

    std::cout << ans << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3620kb

input:

2
2 1

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3564kb

input:

10
2 2 2 2 1 4 5 3 3 2

output:

33

result:

wrong answer 1st numbers differ - expected: '20', found: '33'