PROBLEM LINK:
Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4
Author: hjroh0315
Tester: sushil2006
Editorialist: iceknight1093
DIFFICULTY:
Easy
PREREQUISITES:
Familiarity with bitwise XOR
PROBLEM:
Given L, R, X, find the mex of the set \{L\oplus X, (L+1)\oplus X, \ldots, R\oplus X\}.
EXPLANATION:
Since we’re dealing with the XOR operation, it makes sense to consider things one bit at a time.
So, we’ll attempt to build the answer bit-by-bit.
Let \text{ans} denote the answer, initially 0.
We’ll iterate over bits in descending order from 30 to 0, and see if the answer includes each bit.
Suppose we’re processing bit b, and we’ve finished processing all higher bits.
If all the values \{\text{ans}, (\text{ans}+1), \ldots, (\text{ans}+2^b - 1)\} appear in our sequence, then the MEX is at least \text{ans} + 2^b (meaning bit b should be set); otherwise the MEX is strictly less than that and b won’t be set.
So, all we really need to check is whether all the values of \text{ans} + i for 0 \leq i \lt 2^b appear in our XOR sequence.
Because XOR is invertible, this is equivalent to checking if all the values (\text{ans} + i)\oplus X for 0 \leq i \lt 2^b appear in the range [L, R].
Let’s take a closer look at the (\text{ans} + i)\oplus X values.
Since \text{ans} contains only bits greater than b, and i contains only bits less than b (remember i \lt 2^b), \text{ans} and i don’t share any bits - so \text{ans} + i is effectively \text{ans} \oplus i.
So, we’re really looking at the set of all values \text{ans}\oplus i \oplus X for 0 \leq i \lt 2^b.
We want to check if all of these values lie in the range [L, R], which is possible if and only if the minimum and maximum value among them both lie in [L, R].
Since i can change only the bits lower than b, the bits above b are all fixed here independent of i: they’ll simply be the set bits in \text{ans} \oplus X that are \geq b.
Let \text{mask}_b denote the mask of bits \geq b of \text{ans}\oplus X.
As for the lower bits: since we have all possible i from 0 to 2^{b} - 1, we in fact have all possible 2^b combinations of lower bits available to us.
The minimum is, of course, setting them all to 0, giving a value of 0.
The maximum is similarly obtained by setting them all to 1, giving a value of 2^b - 1.
So, the minimum value among all the (\text{ans}\oplus i\oplus X) values is simply \text{mask}_b, while the maximum value is (\text{mask}_b + 2^b - 1).
If these two values lie in [L, R], increase \text{ans} by 2^b; otherwise do nothing.
TIME COMPLEXITY:
\mathcal{O}(\log R) per testcase.
CODE:
Author's code (C++)
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int main()
{
cin.tie(0)->sync_with_stdio(0);
// freopen("in", "r", stdin);
// freopen("out", "w", stdout);
int q;cin>>q;
while(q--)
{
ll L,R,X;
cin>>L>>R>>X;
R++; // to make range half-open
vector<pair<ll,ll>>ranges;
// left border, right border, current bit
// , real left border, real right border
auto rec=[&](auto rec,ll li,ll ri,ll bi,ll Li,ll Ri)->void
{
if(L<=li&&ri<=R)
{
ranges.emplace_back(Li,Ri);
return;
}
if(ri<=L||li>=R||bi<0)return;
if(X>>bi&1)
{
rec(rec,(li+ri)/2,ri,bi-1,Li,(Li+Ri)/2);
rec(rec,li,(li+ri)/2,bi-1,(Li+Ri)/2,Ri);
}
else
{
rec(rec,li,(li+ri)/2,bi-1,Li,(Li+Ri)/2);
rec(rec,(li+ri)/2,ri,bi-1,(Li+Ri)/2,Ri);
}
};
rec(rec,0,1LL<<61,60,0,1LL<<61);
ll ans=0;
for(auto xi:ranges)
{
int li = xi.first;
int ri = xi.second;
if(li==ans)ans=ri;
else break;
}
cout<<ans<<"\n";
}
}
Tester's code (C++)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)
template<typename T>
void amin(T &a, T b) {
a = min(a,b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a,b);
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
/*
*/
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
void solve(int test_case){
ll l,r,x; cin >> l >> r >> x;
// min m s.t m^x < l
ll m = 0;
ll ans = inf2;
rev(bit,29,0){
ll f = 1<<bit;
ll b1 = 0, b2 = 0;
if(x&f) b1 = 1;
if(l&f) b2 = 1;
if(b2){
amin(ans,m+b1*f);
}
m += (b1^b2)*f;
}
// min m s.t m^x > r
m = 0;
rev(bit,29,0){
ll f = 1<<bit;
ll b1 = 0, b2 = 0;
if(x&f) b1 = 1;
if(r&f) b2 = 1;
if(!b2){
amin(ans,m+(b1^1)*f);
}
m += (b1^b2)*f;
}
cout << ans << endl;
}
int main()
{
fastio;
int t = 1;
cin >> t;
rep1(i, t) {
solve(i);
}
return 0;
}
Editorialist's code (PyPy3)
for _ in range(int(input())):
l, r, x = map(int, input().split())
ans = 0
for b in reversed(range(30)):
# does everything in [ans, ans + 2**b) occur?
# ans^x, ..., (ans+2**b-1)^x should all lie in [L, R]
# find min, max value among them
# min: lower b bits are all 0
mask = x & ~((2**b) - 1)
mn = ans ^ mask
mx = mn + 2**b - 1
if mn >= l and mx <= r: ans += 2**b
print(ans)