結果
| 問題 |
No.1526 Sum of Mex 2
|
| ユーザー |
hitonanode
|
| 提出日時 | 2021-06-03 01:05:40 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 84 ms / 3,000 ms |
| コード長 | 1,811 bytes |
| コンパイル時間 | 996 ms |
| コンパイル使用メモリ | 102,112 KB |
| 最終ジャッジ日時 | 2025-01-21 21:10:29 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 32 |
ソースコード
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
#include <atcoder/lazysegtree>
namespace RangeUpdateRangeSumMax {
using var = unsigned long long;
struct S {
var sum, max, 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(), var(), 0}; }
S mapping(F f, S x) noexcept { return f._updated ? S{x.size * f._lazy, x.size ? f._lazy : 0, 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';
}
hitonanode