結果

問題 No.1526 Sum of Mex 2
ユーザー V_Melville
提出日時 2026-08-09 02:47:21
言語 C++23
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 46 ms / 3,000 ms
+ 873µs
コード長 1,419 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,711 ms
コンパイル使用メモリ 380,132 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2026-08-09 02:47:29
合計ジャッジ時間 7,455 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;
using ll = long long;

struct S {
    ll sum;
    int mx, w;
};
S op(S a, S b) {
    return {a.sum+b.sum, max(a.mx, b.mx), a.w+b.w};
}
S e() {
    return {0, -1, 0};
}

S mapping(int f, S x) {
    if (f == -1) return x;
    return {(ll)f*x.w, f, x.w};
}
int composition(int f, int g) {
    if (f == -1) return g;
    return f;
}
int id() {
    return -1;
}

int main() {
    int n;
    cin >> n;

    vector<int> a(n);
    rep(i, n) cin >> a[i];
    
    vector<int> m(n);
    vector<bool> vis(n+2);
    int mex = 1;
    rep(i, n) {
        vis[a[i]] = true;
        while (vis[mex]) ++mex;
        m[i] = mex;
    }

    vector<int> nxt(n);
    vector<int> last(n+2, n);
    for (int i = n-1; i >= 0; --i) {
        nxt[i] = last[a[i]];
        last[a[i]] = i;
    }
    
    vector<S> init(n);
    rep(i, n) init[i] = {m[i], m[i], 1};
    
    lazy_segtree<S, op, e, int, mapping, composition, id> seg(init);
    
    ll ans = 0;
    rep(l, n) {
        ans += seg.prod(l, n).sum;
        
        int x = a[l];
        int r = nxt[l]-1;
        if (l <= r) {
            int p = seg.max_right(l, [&](S s) { return s.mx <= x; });
            if (p <= r) {
                seg.apply(p, r+1, x);
            }
        }
    }
    
    cout << ans << '\n';
    
    return 0;
}
0