結果

問題 No.1526 Sum of Mex 2
ユーザー hitonanodehitonanode
提出日時 2021-06-03 01:11:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 74 ms / 3,000 ms
コード長 1,847 bytes
コンパイル時間 1,217 ms
コンパイル使用メモリ 101,676 KB
実行使用メモリ 17,972 KB
最終ジャッジ日時 2024-04-27 17:00:41
合計ジャッジ時間 2,694 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 4 ms
6,944 KB
testcase_14 AC 6 ms
6,940 KB
testcase_15 AC 6 ms
6,944 KB
testcase_16 AC 23 ms
17,724 KB
testcase_17 AC 16 ms
15,892 KB
testcase_18 AC 4 ms
6,944 KB
testcase_19 AC 3 ms
6,944 KB
testcase_20 AC 11 ms
10,084 KB
testcase_21 AC 20 ms
17,480 KB
testcase_22 AC 13 ms
11,008 KB
testcase_23 AC 21 ms
17,592 KB
testcase_24 AC 22 ms
17,888 KB
testcase_25 AC 20 ms
17,544 KB
testcase_26 AC 21 ms
17,788 KB
testcase_27 AC 22 ms
17,820 KB
testcase_28 AC 22 ms
17,928 KB
testcase_29 AC 22 ms
17,872 KB
testcase_30 AC 23 ms
17,972 KB
testcase_31 AC 22 ms
17,948 KB
testcase_32 AC 22 ms
17,528 KB
testcase_33 AC 74 ms
17,328 KB
evil_largest AC 241 ms
53,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

namespace RangeUpdateRangeSumMax {
using var = unsigned long long;
const var LOWEST = 0;
struct S {
    var sum, max;
    int 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(), LOWEST, 0}; }
S mapping(F f, S x) noexcept { return f._updated ? S{x.size * f._lazy, x.size ? f._lazy : LOWEST, 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