388535 (Hard Version) (字典树)

388535 (Hard Version)

[Link](Problem - D2 - Codeforces)

题意

给你一个 [ l , r ] [l,r] [l,r]的排列 p p p,设 a i = p i ⨁ x a_i=p_i\bigoplus x ai=pix,现在给你 a a a数组和 [ l , r ] [l,r] [l,r]区间,问你 x x x是多少 ? ? ?

思路

​ 涉及异或因此从二进制来考虑,我们知道 a ⨁ b = b ⨁ a , a ⨁ a = 0 a\bigoplus b=b \bigoplus a, a\bigoplus a=0 ab=ba,aa=0。因为 [ l , r ] [l,r] [l,r]任意两个数不同所以 a a a中的任意两个数也均不同,假设 a ≠ b , a ⊕ x = b ⊕ x , 有   a ⊕ x ⊕ b ⊕ x = 0 , 则 a ⊕ b = 0 → a = b a\neq b, a\oplus x = b \oplus x,有\ a\oplus x\oplus b \oplus x = 0,则a\oplus b = 0\to a=b a=b,ax=bx axbx=0ab=0a=b,因此 a a a中任意两数不同。

​ 考虑将其还原,即 p i ⊕ x ⊕ x = p i p_i\oplus x\oplus x=p_i pixx=pi,因此枚举 x x x,让每个 x x x a i a_i ai中的每个数 ⊕ \oplus ,如果可以复原即可,显然复杂度过高,因为每个数均不同,所以我们只需要满足 m a x ( x ⊕ a i ) = = r , m i n ( x ⊕ a i ) = = l max(x\oplus a_i)==r,min(x\oplus a_i) ==l max(xai)==r,min(xai)==l即可(因为还原后最大为 r r r最小为 l l l且各不相同所以一定是成立的),这个可以用字典树来做,直接枚举 x x x复杂度过高,可以枚举 a i ⊕ l a_i\oplus l ail,因为一定存在某个 a i = l ⊕ x a_i=l\oplus x ai=lx

Code

#include <bits/stdc++.h>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1e6 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
    e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k;
int a[N];
int tr[N][2];
int num[N];
void init() {
    idx = 0;
    tr[0][0] = tr[0][1] = 0;
}
void insert(int x) {
    int p = 0;
    for (int i = 17; i >= 0; i --) {
        int u = x >> i & 1;
        if (!tr[p][u]) tr[p][u] = ++ idx;        
        p = tr[p][u];
    }    
}
int query_mx(int x) {
    int p = 0, res = 0;
    for (int i = 17; i >= 0; i --) {
        int s = x >> i & 1;
        if (tr[p][!s]) {
            res += 1 << i;
            p = tr[p][!s];
        }
        else p = tr[p][s];
    }
    return res;
}
int query_mn(int x) {
    int p = 0, res = 0;
    for (int i = 17; i >= 0; i --) {
        int s = x >> i & 1;
        if (tr[p][s]) p = tr[p][s];        
        else p = tr[p][!s], res += 1 << i;
    }
    return res;
}
int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int T;
    cin >> T;
    while (T -- ) {
        int l, r;
        cin >> l >> r;
        for (int i = 0; i <= idx; i ++)
            tr[i][0] = tr[i][1] = 0;
        idx = 0;
        for (int i = l; i <= r; i ++) {
            cin >> a[i];
            insert(a[i]);
        }

        int res = 0;
        for (int i = l; i <= r; i ++) {
            int x = a[i] ^ l;
            if (query_mx(x) == r && query_mn(x) == l) {
                res = x;
                break;
            }
        }
        
        cout << res << '\n';
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值