結果

問題 No.1526 Sum of Mex 2
ユーザー hitonanode
提出日時 2021-06-03 00:54:42
言語 C++17(gcc12)
(gcc 12.4.0 + boost 1.89.0)
コンパイル:
g++-12 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 42 ms / 3,000 ms
コード長 1,442 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 912 ms
コンパイル使用メモリ 101,916 KB
実行使用メモリ 13,824 KB
最終ジャッジ日時 2026-06-20 17:36:00
合計ジャッジ時間 2,505 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#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