QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#85802#5693. 众数xiafan8050TL 0ms0kbC++203.0kb2023-03-08 15:50:512023-03-08 15:50:53

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-08 15:50:53]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2023-03-08 15:50:51]
  • 提交

answer

/**
 * @author: XiaFan
 * @date: 2023-03-08 14:01
 **/
#include <bits/stdc++.h>
using i64 = long long;

// ! all the range is [l, r)
struct Info {
    i64 sum, min, max;
    bool skip;
    Info(int x = 0) : sum(x), min(sum), max(sum), skip(false) {}
    friend Info operator+(const Info &a, const Info &b) {
        Info c;
        c.sum = a.sum + b.sum;
        c.min = std::min(a.min, b.min);
        c.max = std::max(a.max, b.max);
        return c;
    }
};
struct SegTree {
    const int n;
    const std::plus<Info> merge;
    std::vector<Info> info;
    constexpr int ls(int p) const { return 2 * p + 0; }
    constexpr int rs(int p) const { return 2 * p + 1; }
    SegTree(int n) : n(n), merge(std::plus<Info>()), info(4 << std::__lg(n)) {}
    SegTree(std::vector<Info> init) : SegTree(init.size()) {
        std::function<void(int, int, int)> build = [&](int p, int l, int r) {
            if (r - l == 1) {
                info[p] = init[l];
                return;
            }
            int m = (l + r) / 2;
            build(ls(p), l, m);
            build(rs(p), m, r);
            pull(p);
        };
        build(1, 0, n);
    }
    void pull(int p) {
        info[p] = merge(info[ls(p)], info[rs(p)]);
    }
    i64 range_modify(int p, int l, int r, int x, int y, const i64 &v) {
        if (l >= y || r <= x || info[p].skip) {
            return 0;
        }
        if (x <= l && r <= y && r - l == 1) {
            int d = std::min(info[p].sum, v);
            info[p] = Info(info[p].sum - d);
            if (info[p].sum == 0) {
                info[p].skip = true;
            }
            return d;
        }
        int m = (l + r) / 2;
        i64 res = 0;
        res += range_modify(ls(p), l, m, x, y, v);
        res += range_modify(rs(p), m, r, x, y, v);
        pull(p);
        return res;
    }
    /// @brief modify for [l, r)
    i64 range_modify(int l, int r, const i64 &v) {
        return range_modify(1, 0, n, l, r, v);
    }
    Info range_query(int p, int l, int r, int x, int y) {
        if (l >= y || r <= x) {
            return Info();
        }
        if (x <= l && r <= y) {
            return info[p];
        }
        int m = (l + r) / 2;
        return merge(range_query(ls(p), l, m, x, y), 
                     range_query(rs(p), m, r, x, y));
    }
    /// @brief query for [l, r)
    Info range_query(int l, int r) {
        return range_query(1, 0, n, l, r);
    }
};

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

    int n;
    std::cin >> n;
    std::vector<i64> a(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> a[i];
    }
    std::vector<Info> init(n);
    for (int i = 0; i < n; ++i) {
        init[i] = Info(a[i]);
    }
    SegTree seg(init);
    i64 ans = 0;
    for (int i = n - 1; i >= 0; i--) {
        i64 sum = seg.range_query(i, i + 1).sum;
        sum += seg.range_modify(0, i, a[i]);
        ans += sum * (i + 1);
    }

    std::cout << ans << "\n";

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

99991
3 3 3 4 3 6 4 2 2 5 3 2 3 5 3 3 2 1 2 3 4 2 3 4 3 3 3 4 3 3 3 2 3 2 3 2 4 3 3 2 3 5 3 5 4 2 4 3 3 1 3 2 2 3 4 2 3 3 2 2 4 5 3 2 3 3 1 3 3 4 1 3 4 5 1 1 3 4 2 4 3 2 5 3 3 2 2 2 4 2 4 2 2 4 2 4 3 3 2 3 2 2 1 4 3 1 3 1 2 2 3 1 1 5 2 1 2 2 3 3 1 4 4 4 3 3 3 3 1 3 3 5 5 4 4 3 3 3 4 3 6 5 3 1 1 2 2 ...

output:


result: