0
点赞
收藏
分享

微信扫一扫

Codeforces Round #779 (Div. 2) D2. 388535 (Hard Version)

泠之屋 2022-04-06 阅读 104
算法

传送门

prob. : 给出l,r,和一个数列a,a是l到r中间这些数的排列,数列b是由数列a每个数异或上一个x得到,给出数列b要求x。

idea:

刚开始的想法是对于每一位考虑,在l到r的区间内这一位为1次数记为cnt1和在b数组中这一位为1的次数记为cnt2,但由于l不一定为0,当 c n t 1 = = c n t 2 cnt1 == cnt2 cnt1==cnt2 c n t 1 = = n / 2 cnt1 == n/2 cnt1==n/2的时候不知道这一位到底要不要取,有想过一些奇怪的判定方法都给否了。

考虑到异或的性质,存在 b i = x ⨁ l b_i = x \bigoplus l bi=xl,则x一定出现在一个 b i ⨁ l b_i \bigoplus l bil 中,将所有的 b i ⨁ l b_i \bigoplus l bil取出判断,成立的一个必要条件是 b i ⨁ l b_i \bigoplus l bil与数列b能异或出的最小恰好是l,最大值恰好是r

暂时没有充分性证明

code

#include <bits/stdc++.h>

using namespace std;

const int N = (1 << 18) + 10;

int cnt = 0;
int nex[N * 20][2];

void insert(int x) {
    int p = 0;
    for (int i = 17; i >= 0; --i) {
        int c = (x >> i) & 1;
        if (!nex[p][c]) {
            nex[p][c] = ++cnt;
            nex[cnt][0] = nex[cnt][1] = 0; //init
        }
        p = nex[p][c];
    }
}

int getMx(int x) {
    int p = 0, ans = 0;
    for (int i = 17; i >= 0; --i) {
        int c = (x >> i) & 1;
        if (nex[p][!c]) {
            p = nex[p][!c];
            ans = ans ^ (1 << i);
        } else p = nex[p][c];
    }
    return ans;
}

int getMn(int x) {
    int p = 0, ans = 0;
    for (int i = 17; i >= 0; --i) {
        int c = (x >> i) & 1;
        if (nex[p][c]) p = nex[p][c];
        else {
            p = nex[p][!c];
            ans = ans ^ (1 << i);
        }
    }
    return ans;
}

signed main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

    int T;
    cin >> T;
    while (T--) {
        vector<int> vec;
        cnt = 0;
        nex[0][0] = nex[0][1] = 0;

        int l, r;
        cin >> l >> r;
        int n = r - l + 1;
        for (int i = 1, x; i <= n; ++i) {
            cin >> x;
            insert(x);
            vec.push_back(x ^ l);
        }
        int ans;
        for (auto tt : vec) {
            if (getMn(tt) == l && getMx(tt) == r) {
                ans = tt;
            }
        }
        cout << ans << endl;
    }
    return 0;
}
举报

相关推荐

0 条评论