結果

問題 No.1082 XORのXOR
ユーザー finefine
提出日時 2020-06-19 23:44:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 3,257 bytes
コンパイル時間 1,911 ms
コンパイル使用メモリ 169,980 KB
実行使用メモリ 4,696 KB
最終ジャッジ日時 2023-09-16 15:58:40
合計ジャッジ時間 2,873 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 5 ms
4,432 KB
testcase_04 AC 4 ms
4,584 KB
testcase_05 AC 5 ms
4,584 KB
testcase_06 AC 5 ms
4,500 KB
testcase_07 AC 4 ms
4,504 KB
testcase_08 AC 5 ms
4,696 KB
testcase_09 AC 5 ms
4,688 KB
testcase_10 AC 5 ms
4,436 KB
testcase_11 AC 5 ms
4,508 KB
testcase_12 AC 4 ms
4,452 KB
testcase_13 AC 4 ms
4,400 KB
testcase_14 AC 4 ms
4,448 KB
testcase_15 AC 5 ms
4,412 KB
testcase_16 AC 5 ms
4,452 KB
testcase_17 AC 5 ms
4,464 KB
testcase_18 AC 4 ms
4,444 KB
testcase_19 AC 5 ms
4,404 KB
testcase_20 AC 5 ms
4,460 KB
testcase_21 AC 4 ms
4,388 KB
testcase_22 AC 5 ms
4,480 KB
testcase_23 AC 5 ms
4,392 KB
testcase_24 AC 4 ms
4,392 KB
testcase_25 AC 5 ms
4,464 KB
testcase_26 AC 4 ms
4,412 KB
testcase_27 AC 5 ms
4,404 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr char newl = '\n';

// http://kazuma8128.hatenablog.com/entry/2018/05/06/022654
template<typename U = unsigned int, int B = 30>
class BinaryTrie {
    struct node {
        int cnt;
        U lazy;
        node *ch[2];
        node() : cnt(0), lazy(0), ch{ nullptr, nullptr } {}
    };
    void push(node* t, int b) {
        if ((t->lazy >> (U)b) & (U)1) swap(t->ch[0], t->ch[1]);
        if (t->ch[0]) t->ch[0]->lazy ^= t->lazy;
        if (t->ch[1]) t->ch[1]->lazy ^= t->lazy;
        t->lazy = 0;
    }
    node* add(node* t, U val, int b = B - 1) {
        if (!t) t = new node;
        t->cnt += 1;
        if (b < 0) return t;
        push(t, b);
        bool f = (val >> (U)b) & (U)1;
        t->ch[f] = add(t->ch[f], val, b - 1);
        return t;
    }
    node* sub(node* t, U val, int b = B - 1) {
        assert(t);
        t->cnt -= 1;
        if (t->cnt == 0) return nullptr;
        if (b < 0) return t;
        push(t, b);
        bool f = (val >> (U)b) & (U)1;
        t->ch[f] = sub(t->ch[f], val, b - 1);
        return t;
    }
    U get_min(node* t, U val, int b = B - 1) {
        assert(t);
        if (b < 0) return 0;
        push(t, b);
        bool f = (val >> (U)b) & (U)1; f ^= !t->ch[f];
        return get_min(t->ch[f], val, b - 1) | ((U)f << (U)b);
    }
    U get(node* t, int k, int b = B - 1) {
        if (b < 0) return 0;
        push(t, b);
        int m = t->ch[0] ? t->ch[0]->cnt : 0;
        return k < m ? get(t->ch[0], k, b - 1) : get(t->ch[1], k - m, b - 1) | ((U)1 << (U)b);
    }
    int count_lower(node* t, U val, int b = B - 1) {
        if (!t || b < 0) return 0;
        push(t, b);
        bool f = (val >> (U)b) & (U)1;
        return (f && t->ch[0] ? t->ch[0]->cnt : 0) + count_lower(t->ch[f], val, b - 1);
    }
    node *root;
public:
    BinaryTrie() : root(nullptr) {}
    int size() const {
        return root ? root->cnt : 0;
    }
    bool empty() const {
        return !root;
    }
    void insert(U val) {
        root = add(root, val);
    }
    void erase(U val) {
        root = sub(root, val);
    }
    void xor_all(U val) {
        if (root) root->lazy ^= val;
    }
    U max_element(U bias = 0) {
        return get_min(root, ~bias);
    }
    U min_element(U bias = 0) {
        return get_min(root, bias);
    }
    int lower_bound(U val) { // return id
        return count_lower(root, val);
    }
    int upper_bound(U val) { // return id
        return count_lower(root, val + 1);
    }
    U operator[](int k) {
        assert(0 <= k && k < size());
        return get(root, k);
    }
    int count(U val) {
        if (!root) return 0;
        node *t = root;
        for (int i = B - 1; i >= 0; i--) {
            push(t, i);
            t = t->ch[(val >> (U)i) & (U)1];
            if (!t) return 0;
        }
        return t->cnt;
    }
};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    BinaryTrie<> bt;
    unsigned int ans = 0;
    for (int i = 0; i < n; i++) {
        unsigned int a;
        cin >> a;
        if (!bt.empty()) ans = max(ans, bt.max_element(a) ^ a);
        bt.insert(a);
    }
    cout << ans << newl;

    return 0;
}
0