結果
問題 | No.3075 Mex Recurrence Formula |
ユーザー |
|
提出日時 | 2025-03-28 22:14:29 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 165 ms / 2,000 ms |
コード長 | 4,608 bytes |
コンパイル時間 | 3,547 ms |
コンパイル使用メモリ | 281,756 KB |
実行使用メモリ | 15,208 KB |
最終ジャッジ日時 | 2025-03-28 22:14:38 |
合計ジャッジ時間 | 9,491 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 46 |
ソースコード
#include <bits/stdc++.h>// #include <atcoder/all>using namespace std;// using namespace atcoder;#define rep(i, a, n) for(int i = a; i < n; i++)#define rrep(i, a, n) for(int i = a; i >= n; i--)#define inr(l, x, r) (l <= x && x < r)#define ll long long#define ld long double// using mint = modint1000000007;// using mint = modint998244353;constexpr int IINF = 1001001001;constexpr ll INF = 1e18;template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}template<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}template <class T, T (*op)(T, T), T (*e)()>class SegmentTree {int _n, size, log;vector<T> d;void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }public:SegmentTree() : SegmentTree(0) {}explicit SegmentTree(int n) : SegmentTree(vector<T>(n, e())) {} // explicit で明示的に型を指定するexplicit SegmentTree(const vector<T> &v) : _n(int(v.size())) {// sizeは_nを超える最小の2のべき乗size = 1;while(size < _n) size *= 2, log++;// log は木の高さ(sizeの桁数)log = 0;while (!(size & (1 << log))) log++;d = vector<T>(2*size, e());for(int i = 0; i < _n; i++) d[size+i] = v[i];for(int i = size-1; i >= 1; i--){update(i);}}void set(int p, T x){assert(0 <= p && p < _n);p += size;d[p] = x;for(int i = 1; i <= log; i++) update(p >> i);}void apply(int p, T x){set(p, op(x, d[p+size]));}T get(int p) const {assert(0 <= p && p < _n);return d[p+size];}T prod(int l, int r) const {assert(0 <= l && l <= r && r <= _n);T sml = e(), smr = e();l += size;r += size;while(l < r){if(l&1) sml = op(sml, d[l++]);if(r&1) smr = op(d[--r], smr);l >>= 1;r >>= 1;}return op(sml, smr);}T all_prod() const {return d[1]; };// f(op(a[l], a[l + 1], ..., a[r - 1])) = trueとなる最大のrtemplate <bool (*f)(T)> int max_right(int l) const {return max_right(l, [](T x) { return f(x); });}template <class F> int max_right(int l, F f) const {assert(0 <= l && l <= _n);assert(f(e()));if (l == _n) return _n;l += size;T sm = e();do {while (l % 2 == 0) l >>= 1;if (!f(op(sm, d[l]))) {while (l < size) {l = (2 * l);if (f(op(sm, d[l]))) {sm = op(sm, d[l]);l++;}}return l - size;}sm = op(sm, d[l]);l++;} while ((l & -l) != l);return _n;}// f(op(a[l], a[l + 1], ..., a[r - 1])) = true となる最小の ltemplate <bool (*f)(T)> int min_left(int r) const {return min_left(r, [](T x) { return f(x); });}template <class F> int min_left(int r, F f) const {assert(0 <= r && r <= _n);assert(f(e()));if (r == 0) return 0;r += size;T sm = e();do {r--;while (r > 1 && (r % 2)) r >>= 1;if (!f(op(d[r], sm))) {while (r < size) {r = (2 * r + 1);if (f(op(d[r], sm))) {sm = op(d[r], sm);r--;}}return r + 1 - size;}sm = op(d[r], sm);} while ((r & -r) != r);return 0;}};using S = ll;S op(S a, S b) {return min(a, b);}S e() {return 9e18;}int main(){ll n, x; cin >> n >> x;x--;vector<ll> a(n);rep(i, 0, n){cin >> a[i];}SegmentTree<S, op, e> st(vector<S>(n+2, 0));rep(i, 0, n) st.set(min(n+1, a[i]), st.get(min(n+1, a[i]))+1);int p = 0;while(st.prod(0, n) == 0){// にぶたんint r = st.max_right(0, [&](ll tmp){return tmp > 0;});a.push_back(r);st.set(r, st.get(r)+1);st.set(min(a[p%n], n+1), st.get(min(a[p%n], n+1))-1);p++;}a.push_back(n);// rep(i, 0, a.size()){// cout << a[i] << ' ';// }// cout << endl;if(x < a.size()){cout << a[x] << endl;}else{int l = a.size()-n-1;x -= a.size();cout << a[l+x%(n+1)] << endl;}return 0;}