結果

問題 No.3423 Minimum Xor Query
コンテスト
ユーザー pitP
提出日時 2026-01-23 00:11:29
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
結果
WA  
実行時間 -
コード長 5,488 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 6,032 ms
コンパイル使用メモリ 391,424 KB
実行使用メモリ 127,600 KB
最終ジャッジ日時 2026-01-23 00:11:52
合計ジャッジ時間 21,216 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other WA * 3 TLE * 1 -- * 14
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
istream &operator>>(istream &is, modint &a) { long long v; is >> v; a = v; return is; }
ostream &operator<<(ostream &os, const modint &a) { return os << a.val(); }
istream &operator>>(istream &is, modint998244353 &a) { long long v; is >> v; a = v; return is; }
ostream &operator<<(ostream &os, const modint998244353 &a) { return os << a.val(); }
istream &operator>>(istream &is, modint1000000007 &a) { long long v; is >> v; a = v; return is; }
ostream &operator<<(ostream &os, const modint1000000007 &a) { return os << a.val(); } 

typedef long long ll;
typedef vector<vector<int>> Graph;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define FOR(i,l,r) for (int i = l;i < (int)(r); i++)
#define rep(i,n) for (int i = 0;i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define my_sort(x) sort(x.begin(), x.end())
#define my_max(x) *max_element(all(x))
#define my_min(x) *min_element(all(x))
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF = (1<<30) - 1;
const ll LINF = (1LL<<62) - 1;
const int MOD = 998244353;
const int MOD2 = 1e9+7;
const double PI = acos(-1);
vector<int> di = {1,0,-1,0};
vector<int> dj = {0,1,0,-1};

#ifdef LOCAL
#  include <debug_print.hpp>
#  define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)
#else
#  define debug(...) (static_cast<void>(0))
#endif

struct DoubleHeap{
    priority_queue<ll,vector<ll>,greater<ll>> min_hq;
    priority_queue<ll,vector<ll>> max_hq;
    map<ll,ll> mp; // {value, count}
    ll _size = 0;
    ll _total = 0;
    DoubleHeap(){}
    //
    ll count(ll x){
        return mp[x];
    }
    //
    ll size(){
        return _size;
    }
    //
    ll sum(){
        return _total;
    }
    //
    void insert(ll x){
        _size++;
        _total += x;
        min_hq.push(x);
        max_hq.push(x);
        mp[x]++;
    }
    // 削除した個数を返す
    ll erase(ll x, ll n = LINF){
        if (count(x) < n) n = count(x);
        _size -= n;
        _total -= n * x;
        mp[x] -= n;
        // if(mp[x]==0) mp.erase(x);
        while((!min_hq.empty()) && (count(min_hq.top()) == 0)) min_hq.pop();
        while((!max_hq.empty()) && (count(max_hq.top()) == 0)) max_hq.pop();
        return n;
    }
    // 
    void discard(ll x){
        erase(x, 1);
    }
    // 
    ll get_min(){
        return min_hq.top();
    };
    //
    ll get_max(){
        return max_hq.top();
    };
    //
    ll pop_min(){
        ll v = get_min();
        discard(v);
        return v;
    }
    //
    ll pop_max(){
        ll v = get_max();
        discard(v);
        return v;
    }
    
    // debug
    vector<pair<ll,ll>> dump(){
        vector<pair<ll,ll>> res; // (value, cnt)
        for(auto itr = mp.begin(); itr != mp.end(); itr++){
            res.push_back(make_pair(itr->first, itr->second));
        }
        return res;
    }
};

struct MinXorSet {
    multiset<int> ms1;
    DoubleHeap ms2;

    void insert(int v) {
        auto itr = ms1.lower_bound(v);
        if(itr != ms1.begin() && itr != ms1.end()) {
            ms2.erase(*prev(itr) ^ (*itr));
        }
        if(itr != ms1.begin()) {
            ms2.insert(*prev(itr) ^ v);
        }
        if(itr != ms1.end()) {
            ms2.insert(v ^ (*itr));
        }
        ms1.insert(v);
    }

    void erase(int v) {
        ms1.erase(ms1.find(v));
        auto itr = ms1.lower_bound(v);
        if(itr != ms1.begin() && itr != ms1.end()) {
            ms2.insert(*prev(itr) ^ (*itr));
        }
        if(itr != ms1.begin()) {
            ms2.erase(*prev(itr) ^ v);
        }
        if(itr != ms1.end()) {
            ms2.erase(v ^ (*itr));
        }
    }

    int query() {
        if(ms2.size() == 0) return INF;
        else return ms2.get_min();
    }
};

const int B = 534;

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);

    int N, Q;
    cin >> N >> Q;
    vector<int> A(N);
    rep(i, N) cin >> A[i];

    int C = (N + B - 1) / B;
    vector<MinXorSet> st(C);
    rep(i, N)FOR(k, i / B, C) st[k].insert(A[i]);

    while(Q--) {
        int op;
        cin >> op;
        if(op == 1) {
            int i, x;
            cin >> i >> x;
            i--;

            FOR(k, i / B, C) {
                st[k].erase(A[i]);
                st[k].insert(x);
            }

            A[i] = x;
        }
        else{
            int r;
            cin >> r;

            if(r <= B) {
                MinXorSet s;
                rep(i, r) s.insert(A[i]);
                cout << s.query() << "\n";
                continue;
            }

            int k = r / B - 1;
            int ans = st[k].query();
            if(ans == 0) {
                cout << 0 << "\n";
                continue;
            }

            MinXorSet s;
            FOR(i, (k + 1) * B, r) {
                s.insert(A[i]);
            }
            chmin(ans, s.query());
            if(ans == 0) {
                cout << 0 << "\n";
                continue;
            }

            FOR(i, (k + 1) * B, r) {
                st[k].insert(A[i]);

                ans = min(ans, st[k].query());
                if(ans == 0) break;

                st[k].erase(A[i]);
            }

            cout << ans << "\n";
        }
    }
}
0