結果

問題 No.1526 Sum of Mex 2
ユーザー hitonanodehitonanode
提出日時 2021-06-03 01:05:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 83 ms / 3,000 ms
コード長 1,811 bytes
コンパイル時間 1,071 ms
コンパイル使用メモリ 101,876 KB
実行使用メモリ 18,100 KB
最終ジャッジ日時 2024-04-27 16:56:09
合計ジャッジ時間 3,099 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 6 ms
6,940 KB
testcase_14 AC 6 ms
6,944 KB
testcase_15 AC 7 ms
6,940 KB
testcase_16 AC 25 ms
17,708 KB
testcase_17 AC 20 ms
15,756 KB
testcase_18 AC 4 ms
6,944 KB
testcase_19 AC 3 ms
6,944 KB
testcase_20 AC 12 ms
10,144 KB
testcase_21 AC 25 ms
17,392 KB
testcase_22 AC 14 ms
10,980 KB
testcase_23 AC 27 ms
17,640 KB
testcase_24 AC 26 ms
17,976 KB
testcase_25 AC 26 ms
17,472 KB
testcase_26 AC 26 ms
17,800 KB
testcase_27 AC 26 ms
17,740 KB
testcase_28 AC 26 ms
18,036 KB
testcase_29 AC 26 ms
17,880 KB
testcase_30 AC 26 ms
18,052 KB
testcase_31 AC 27 ms
18,100 KB
testcase_32 AC 26 ms
17,804 KB
testcase_33 AC 83 ms
17,376 KB
evil_largest AC 268 ms
54,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <utility>
#include <vector>
using namespace std;
#include <atcoder/lazysegtree>

namespace RangeUpdateRangeSumMax {
using var = unsigned long long;
struct S {
    var sum, max, size;
    static S init(var x) { return S{x, x, 1}; }
};
struct F {
    var _lazy;
    bool _updated;
    static F update(var x) noexcept { return {x, true}; }
};
S op(S l, S r) noexcept { return {l.sum + r.sum, l.max > r.max ? l.max : r.max, l.size + r.size}; }
S e() noexcept { return {var(), var(), 0}; }
S mapping(F f, S x) noexcept { return f._updated ? S{x.size * f._lazy, x.size ? f._lazy : 0, x.size} : x; }
F composition(F fnew, F gold) noexcept { return fnew._updated ? fnew : gold; }
F id() noexcept { return {var(), false}; }
using segtree = atcoder::lazy_segtree<S, op, e, F, mapping, composition, id>;
} // namespace RangeUpdateRangeSumMax

int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    unsigned N;
    cin >> N;
    vector<vector<unsigned>> v2is(N);
    for (unsigned i = 0; i < N; i++) {
        unsigned a;
        cin >> a;
        v2is[a - 1].push_back(i);
    }
    long long ret = 0;

    vector<RangeUpdateRangeSumMax::S> init(N);
    for (int i = 0; i < N; i++) init[i] = RangeUpdateRangeSumMax::S::init(i);
    RangeUpdateRangeSumMax::segtree tree(init);

    for (int h = 0; h < N; h++) {
        int l = 0;
        for (auto i : v2is[h]) {
            int r = tree.max_right(l, [&](auto x) { return x.max <= i; });
            if (l < r) tree.apply(l, r, RangeUpdateRangeSumMax::F::update(i));
            l = i + 1;
        }
        tree.apply(l, N, RangeUpdateRangeSumMax::F::update(N));
        auto tmp = (long long)N * N - tree.all_prod().sum;
        ret += tmp;
        if (!tmp) break;
    }
    cout << ret + (long long)N * (N + 1) / 2 << '\n';
}
0