Problem - 1658D2 - Codeforces
dalao的
https://zhuanlan.zhihu.com/p/488629202
Codeforces Round #779 D2(01字典树) - 知乎 (zhihu.com)
codeforce 官方解 : 思维 + 构造
设p为原数组,变化后数组为a
变换此式可得注意到
将的两个数进行配对即变换最后一个二进制位的两个数进行匹配
即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;
}