結果

問題 No.1526 Sum of Mex 2
ユーザー hitonanode
提出日時 2021-06-03 01:11:07
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 89 ms / 3,000 ms
コード長 1,847 bytes
コンパイル時間 1,184 ms
コンパイル使用メモリ 102,128 KB
最終ジャッジ日時 2025-01-21 21:10:36
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

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