Problem - 1658D2 - Codeforces D2. 388535 (Hard Version)

Problem - 1658D2 - Codeforces

dalao的

https://zhuanlan.zhihu.com/p/488629202

Codeforces Round #779 D2(01字典树) - 知乎 (zhihu.com)

codeforce 官方解 : 思维 + 构造

设p为原数组,变化后数组为ap_i\bigoplus x=a_i

变换此式p_i=a_i\bigoplus x可得注意到p_i\bigoplus p_j = 1\Leftrightarrow (a_i\bigoplus x)\bigoplus (a_j\bigoplus x)=a_i\bigoplus aj=1

 将a\bigoplus b=1的两个数进行配对即变换最后一个二进制位的两个数进行匹配

即x与x-1或者x+1配对由上面的等式,原排列 p 中的匹配数与数组  a 中的匹配数相等,

当l,r的奇偶性不是依次为偶奇时,会发生一边或者两边没有配对的情况,我们严格将i与i+1配对,并依据未匹配的值判断合法性之后计算x的值

如果全部匹配,则使范围缩小一位,并使答案增加一位

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
#include<bitset>
#include<cmath>
#include<array>
#include<atomic>
#include<sstream>
#include<stack>

//#define int ll
#define pb push_back
#define endl '\n'
#define x first
#define y second
#define Endl endl
#define pre(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,b,a) for(int i=b;i>=a;i--)
#define si(x) scanf("%d", &x);
#define sl(x) scanf("%lld", &x);
#define ss(x) scanf("%s", x);

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
typedef pair<char, int> PCI;
typedef pair<int, char> PIC;
typedef pair<double, double> PDD;
typedef pair<ll, ll> PLL;
const int N = 200010, M = 2 * N, B = N;
const int INF = 0x3f3f3f3f;
const ll LLINF = 1e18;

int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
int n, m, k;
int a[N];
int cnt[20], acnt[20];

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lowbit(ll x) { return x & -x; }
ll qmi(ll x, ll n, ll mod)
{
    ll res = 1;
    while (n)
    {
        if (n & 1)res = (1ll * res * x) % mod;
        x = (1ll * x * x) % mod;
        n >>= 1;
    }
    return res;
}

void init() {}

void solve()
{
    int l, r; si(l); si(r);
    set<int> S;
    for (int i = l, x; i <= r; i++) {
        si(x); S.insert(x);
    }

    auto check = [&](int x)
    {
        for (auto i : S)
            if ((i ^ x) > r || ((i ^ x) < l))return false;
        return true;
    };

    int ans = 1;
    while (true)
    {
        set<int> T = S;
        for (auto i : S)T.erase(i^1);
        if (l % 2 == 1 && r % 2 == 1) {
            cout << ans * (*T.begin() ^ l) << endl;
            return;
        }
        if (l % 2 == 0 && r % 2 == 0) {
            cout << ans * (*T.begin() ^ r) << endl;
            return;
        }
        if (l % 2 == 1 && r % 2 == 0) {
            if (check(*T.begin() ^ l)) {
                cout << ans * (*T.begin() ^ l) << endl;
                return;
            }
            else {
                cout << ans * (*T.begin() ^ r) << endl;
                return;
            }
        }
        T.clear();
        for (auto i : S)T.insert(i / 2);
        S = T;
        l >>= 1; r >>= 1; ans <<= 1;
    }
}

signed main()
{
    int _;
    cin >> _;
    //_ = 1;
    init();
    while (_--)
    {
        solve();
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值