結果

問題 No.1526 Sum of Mex 2
ユーザー hitonanodehitonanode
提出日時 2021-06-03 00:54:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 63 ms / 3,000 ms
コード長 1,442 bytes
コンパイル時間 889 ms
コンパイル使用メモリ 100,416 KB
実行使用メモリ 13,700 KB
最終ジャッジ日時 2024-04-27 16:45:52
合計ジャッジ時間 2,504 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 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,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 5 ms
6,944 KB
testcase_14 AC 5 ms
6,940 KB
testcase_15 AC 5 ms
6,940 KB
testcase_16 AC 20 ms
13,420 KB
testcase_17 AC 15 ms
11,748 KB
testcase_18 AC 4 ms
6,940 KB
testcase_19 AC 3 ms
6,944 KB
testcase_20 AC 9 ms
8,064 KB
testcase_21 AC 17 ms
13,108 KB
testcase_22 AC 12 ms
8,960 KB
testcase_23 AC 19 ms
13,248 KB
testcase_24 AC 20 ms
13,572 KB
testcase_25 AC 19 ms
13,248 KB
testcase_26 AC 21 ms
13,428 KB
testcase_27 AC 21 ms
13,408 KB
testcase_28 AC 19 ms
13,700 KB
testcase_29 AC 20 ms
13,560 KB
testcase_30 AC 21 ms
13,548 KB
testcase_31 AC 21 ms
13,656 KB
testcase_32 AC 21 ms
13,372 KB
testcase_33 AC 63 ms
12,892 KB
evil_largest AC 203 ms
37,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using ull = unsigned long long;
struct S {
    ull sum;
    unsigned max, size;
    S() = default;
};
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 {0, 0, 0}; }
S mapping(unsigned f, S x) noexcept { return f ? S{(unsigned long long)x.size * f, x.size ? f : 0, x.size} : x; }
unsigned composition(unsigned f, unsigned g) noexcept { return f ? f : g; }
unsigned id() noexcept { return 0; }

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<S> init(N);
    for (int i = 0; i < N; i++) init[i] = {(unsigned)i, (unsigned)i, 1};
    atcoder::lazy_segtree<S, op, e, unsigned, mapping, composition, id> tree(init);

    for (int h = 0; h < N; h++) {
        int l = 0;
        for (auto i : v2is[h]) {
            int r = tree.max_right(l, [&](S x) { return x.max <= i; });
            if (l < r) tree.apply(l, r, i);
            l = i + 1;
        }
        tree.apply(l, N, 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