結果
問題 |
No.3075 Mex Recurrence Formula
|
ユーザー |
![]() |
提出日時 | 2025-03-28 22:08:03 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 389 ms / 2,000 ms |
コード長 | 6,127 bytes |
コンパイル時間 | 3,705 ms |
コンパイル使用メモリ | 296,688 KB |
実行使用メモリ | 22,708 KB |
最終ジャッジ日時 | 2025-03-28 22:08:42 |
合計ジャッジ時間 | 12,615 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 46 |
ソースコード
#include <bits/stdc++.h> #define ALL(x) (x).begin(), (x).end() #define LB(v, x) (int)(lower_bound(ALL(v), x) - (v).begin()) #define UQ(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end()) #define IO ios::sync_with_stdio(false), cin.tie(nullptr); #define chmax(a, b) (a) = (a) < (b) ? (b) : (a) #define chmin(a, b) (a) = (a) < (b) ? (a) : (b) using namespace std; using ll = long long; const int INF = 1e9 + 10; const ll INFL = 4e18; template <typename Monoid> struct SegTree { using Type = typename Monoid::Type; SegTree() = default; SegTree(int n) { this->n = n; dat.assign(n << 1, Monoid::id()); cand.reserve(100), cand_l.reserve(100), cand_r.reserve(100); } SegTree(const vector<Type>& v) { this->n = v.size(); dat.assign(n << 1, Monoid::id()); for (int i = 0; i < n; i++) dat[i + n] = v[i]; for (int i = n - 1; i > 0; i--) dat[i] = Monoid::op(dat[i << 1], dat[i << 1 | 1]); cand.reserve(100), cand_l.reserve(100), cand_r.reserve(100); } void set(int i, Type x) { i += n; dat[i] = x; while (i >>= 1) dat[i] = Monoid::op(dat[i << 1], dat[i << 1 | 1]); } Type fold(int l, int r) { Type retl = Monoid::id(), retr = Monoid::id(); l += n, r += n; while (l < r) { if (l & 1) retl = Monoid::op(retl, dat[l++]); if (r & 1) retr = Monoid::op(dat[--r], retr); l >>= 1, r >>= 1; } return Monoid::op(retl, retr); } template <typename F> int find_right(int l, F f) { assert(f(Monoid::id())); if (l == n) return n; l += n; int r = n + n; cand_l.clear(), cand_r.clear(); while (l < r) { if (l & 1) cand_l.push_back(l++); if (r & 1) cand_r.push_back(--r); l >>= 1, r >>= 1; } cand = cand_l; reverse(cand_r.begin(), cand_r.end()); cand.insert(cand.end(), cand_r.begin(), cand_r.end()); Type val = Monoid::id(); for (int i : cand) { if (f(Monoid::op(val, dat[i]))) { val = Monoid::op(val, dat[i]); } else { while (i < n) { i <<= 1; if (f(Monoid::op(val, dat[i]))) { val = Monoid::op(val, dat[i]); i |= 1; } } return i - n; } } return n; } template <typename F> int find_left(int r, F f) { assert(f(Monoid::id())); if (r == 0) return 0; r += n; int l = n; cand_l.clear(), cand_r.clear(); while (l < r) { if (l & 1) cand_l.push_back(l++); if (r & 1) cand_r.push_back(--r); l >>= 1, r >>= 1; } cand = cand_r; reverse(cand_l.begin(), cand_l.end()); cand.insert(cand.end(), cand_l.begin(), cand_l.end()); Type val = Monoid::id(); for (int i : cand) { if (f(Monoid::op(dat[i], val))) { val = Monoid::op(dat[i], val); } else { while (i < n) { i = (i << 1) | 1; if (f(Monoid::op(dat[i], val))) { val = Monoid::op(dat[i], val); i ^= 1; } } return i - n + 1; } } return 0; } int size() { return n; } Type operator[](int i) { return dat[i + n]; } private: int n; vector<Type> dat; vector<int> cand, cand_l, cand_r; }; namespace Monoid { template <typename T, T max_value = INF> struct Min { using Type = T; static Type id() { return max_value; } static Type op(const Type& a, const Type& b) { return min(a, b); } }; template <typename T, T min_value = -INF> struct Max { using Type = T; static Type id() { return min_value; } static Type op(const Type& a, const Type& b) { return max(a, b); } }; template <typename T> struct Sum { using Type = T; static Type id() { return 0; } static Type op(const Type& a, const Type& b) { return a + b; } }; template <typename T> struct SumPair { using Type = pair<T, int>; static Type id() { return make_pair(T(0), 0); } static Type op(const Type& a, const Type& b) { return {a.first + b.first, a.second + b.second}; } }; } // namespace Monoid namespace RangeQuery { template <typename T, T max_value = INF> struct Min { using Type = struct SegTree<Monoid::Min<T, max_value>>; }; template <typename T, T min_value = -INF> struct Max { using Type = struct SegTree<Monoid::Max<T, min_value>>; }; template <typename T> struct Sum { using Type = struct SegTree<Monoid::Sum<T>>; }; } // namespace RangeQuery struct Mn { using Type = bool; static Type op(Type l, Type r) { return l & r; } static Type id() { return true; } }; int main() { ll N, X; cin >> N >> X; X--; vector<ll> A(N); for (int i = 0; i < N; i++) cin >> A[i]; if (X < N) { cout << A[X] << endl; return 0; } const int mx = 5e5; auto seg = SegTree<Mn>(vector<bool>(mx, false)); vector<int> cnt(mx); for (int i = 0; i < N; i++) if (A[i] < mx) cnt[A[i]]++, seg.set(A[i], true); set<int> st; for (int i = N; i < 5 * N; i++) { int mex = seg.find_right(0, [&](bool x) { return x; }); if (X >= 5 * N && st.count(mex)) { vector<int> B; for (int j = N; j < i; j++) B.push_back(A[j]); X -= N, X %= ssize(B); cout << B[X] << endl; return 0; } if (i == X) { cout << mex << endl; return 0; } st.insert(mex); A.push_back(mex); ++cnt[mex]; seg.set(mex, true); if (A[i - N] < mx && --cnt[A[i - N]] == 0) seg.set(A[i - N], false); } }