START199——XOR Game · znzryb
## 思路讲解
不难发现,题目中的操作,对题目中总值的影响是
$Sum’=Sum\oplus A_i \oplus A_i \oplus A_j = Sum\oplus A_j$
那么如果把两次操作全部选出来。
$Sum‘=Sum\oplus A_{j1} \oplus A_{j2}$,当然,$i_1≠j_2$,
$i_1=j_2$,那么就是$Sum’=Sum\oplus A_{j1} \oplus A_{i1} \oplus A_{j1}=Sum\oplus A_{i1}$
那么,这种需要用到01trie树。
```cpp
// j1,j2
ll ans=0;
FOR(j1,1,N){
ll a=sum^A[j1];
ll mne=tr.argmin_xor(a),mn=tr.min_xor(a);
bool ise=tr.erase(mne);
// 那么 bob 有两种选择,一种是避开我们删除的mne,另一种是就选我们删除的mne
ll lans=min<ll>(tr.min_xor(a),mne^sum);
// 那么删除 mne 可能导致负面影响,那么我们不删除mne的结果是什么那?就是bob选择mne呗
lans=max(lans,mn);
ans=max<ll>(ans,lans);
if(ise) tr.insert(mne);
}
// tr.erase(sum);
// 就是sum^A[i1] 也是一种单独的,虽然上面是又包含,但是是取Max的
ans=min<ll>(ans,tr.max_xor(sum));
```
## AC代码
[https://www.codechef.com/viewsolution/1183578991](https://www.codechef.com/viewsolution/1183578991)
::: 源代码
```cpp
// by Gospel_rock
#include <bits/stdc++.h>
#define all(vec) vec.begin(),vec.end()
#define PB push_back
#define EB emplace_back
#define EM emplace
#define FI first
#define SE second
#define pct __builtin_popcountll
#define ctz __builtin_ctzll
#define SZ(a) ((long long) a.size())
#define FOR(i, a, b) for (long long i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (long long i = (a); i >= (b); --i)
#define debug(var) cerr << #var <<":"<<var<<"\n";
#define cend cerr<<"\n-----------\n"
#define lson(var) (var<<1)
#define rson(var) (var<<1|1)
#define fsp(x) fixed<<setprecision(x)
#define minn(vec) *min_element(vec.begin(),vec.end())
#define maxx(vec) *max_element(vec.begin(),vec.end())
using namespace std;
#ifdef LOCAL
template<typename T> void _print(T x) { cerr << x; }
// 可以打印多层数组,但是格式不是很好
template<typename T> void _print(vector<T> v) { cerr << "[ "; for (T i : v) _print(i), cerr << " "; cerr << "]"; }
template<typename T> void _print(set<T> s) { cerr << "{ "; for (T i : s) _print(i), cerr << " "; cerr << "}"; }
template<typename T, typename U> void _print(pair<T, U> p) { cerr << "{"; _print(p.first); cerr << ", "; _print(p.second); cerr << "}"; }
template<typename T, typename U> void _print(map<T, U> m) { cerr << "{ "; for (auto &p : m) _print(p), cerr << " "; cerr << "}"; }
// 打印两层数组,格式好一些
template<typename T> void _print(vector<vector<T>> v){cerr<<"{\n";for (auto i : v) _print(i), cerr << "\n";cerr<<" }";}
#endif
using ll = long long;using ull = unsigned long long;
using DB=double;using LD=long double;
using i128 = __int128;
using pdd = pair<DB,DB>;using plb = pair<ll,bool>;
using pll = pair<ll,ll>;using vll=vector<ll>;using vb=vector<bool>;
using tll = tuple<ll,ll,ll>;using vii=vector<int>;
using vpll = vector<pll>;using vtll = vector<tll>;
using vdb = vector<DB>;using vc=vector<char>;
using arr3 = array<ll,3> ;using arr2 = array<ll,2>;
using CD=complex<double>;
static constexpr ll MAXN=(ll)1e6+10,INF=(ll)1e17+3; // 1e17+3是素数 1e18+9是素数
static constexpr ll mod=(ll)1e9+7; // 998244353;
static constexpr double eps=1e-8;const double pi=acos(-1.0);
ll N,M,Q,X,K,T,lT;
inline void __();
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>lT;
while(lT--)
__();
return 0;
}
/*
*/
// https://threadsiiithyderabad.quora.com/Tutorial-on-Trie-and-example-problems
// AC https://judge.yosupo.jp/submission/308823 test min_xor erase insert 有0
// AC https://codeforces.com/contest/706/submission/334623853 test arg_max max
// AC https://www.codechef.com/viewsolution/1183578991 综合运用 有0
struct XORTrie {
int B; // 最高位(含)
struct Node {
int nxt[2];
int cnt; // 经过该节点的数的个数(叶子 = 出现次数)
Node() { nxt[0] = nxt[1] = -1; cnt = 0; }
};
vector<Node> tr;
explicit XORTrie(int max_bit = 63) : B(max_bit) {
tr.reserve(1 << 20);
tr.emplace_back(); // root
}
void clear() { tr.clear(); tr.emplace_back(); }
inline void insert(unsigned long long x) {
int u = 0;
for (int b = B; b >= 0; --b) {
int bit = int((x >> b) & 1ull);
if (tr[u].nxt[bit] == -1) {
tr[u].nxt[bit] = (int)tr.size();
tr.emplace_back();
}
u = tr[u].nxt[bit];
++tr[u].cnt;
}
}
inline int count(unsigned long long x) const {
int u = 0;
for (int b = B; b >= 0; --b) {
int bit = int((x >> b) & 1ull);
int v = tr[u].nxt[bit];
if (v == -1) return 0;
u = v;
}
return tr[u].cnt;
}
inline bool erase(unsigned long long x) {
if (tr.size() == 1) return false;
static int pathBit[70];
int u = 0, depth = 0;
for (int b = B; b >= 0; --b) {
int bit = int((x >> b) & 1ull);
int v = tr[u].nxt[bit];
if (v == -1 || tr[v].cnt == 0) return false;
pathBit[depth++] = bit;
u = v;
}
if (tr[u].cnt == 0) return false;
u = 0;
for (int i = 0; i < depth; ++i) {
int bit = pathBit[i];
int v = tr[u].nxt[bit];
--tr[v].cnt;
if (tr[v].cnt == 0) tr[u].nxt[bit] = -1; // 懒回收
u = v;
}
return true;
}
inline unsigned long long max_xor(unsigned long long x) const {
if (tr.size() == 1) return 0ull;
int u = 0;
unsigned long long ans = 0;
for (int b = B; b >= 0; --b) {
int bit = int((x >> b) & 1ull);
int prefer = tr[u].nxt[bit ^ 1];
int same = tr[u].nxt[bit];
if (prefer != -1 && tr[prefer].cnt > 0) {
u = prefer;
ans |= (1ull << b);
} else {
u = same; // 至少有一条有效路
}
}
return ans;
}
inline unsigned long long min_xor(unsigned long long x) const {
if (tr.size() == 1) return 0ull; // empty
int u = 0;
unsigned long long ans = 0;
for (int b = B; b >= 0; --b) {
int bit = int((x >> b) & 1ull);
int same = tr[u].nxt[bit];
int other = tr[u].nxt[bit ^ 1];
if (same != -1 && tr[same].cnt > 0) {
u = same;
} else {
u = other;
ans |= (1ull << b);
}
}
return ans;
}
inline unsigned long long argmax_xor(unsigned long long y) const {
if (tr.size() == 1) return 0ull;
int u = 0;
unsigned long long chosen = 0;
for (int b = B; b >= 0; --b) {
int bit = int((y >> b) & 1ull);
int prefer = tr[u].nxt[bit ^ 1];
int same = tr[u].nxt[bit];
if (prefer != -1 && tr[prefer].cnt > 0) {
u = prefer;
chosen |= (unsigned long long)(bit ^ 1) << b;
} else {
u = same;
chosen |= (unsigned long long)bit << b;
}
}
return chosen;
}
inline unsigned long long argmin_xor(unsigned long long x) const {
if (tr.size() == 1) return 0ull; // 空集时随便返 0
int u = 0;
unsigned long long chosen = 0;
for (int b = B; b >= 0; --b) {
int bit = int((x >> b) & 1ull);
int same = tr[u].nxt[bit];
int other = tr[u].nxt[bit ^ 1];
if (same != -1 && tr[same].cnt > 0) {
u = same; // 让该位 XOR=0
if (bit) chosen |= (1ull << b); // chosen 的该位 = bit
} else {
u = other; // 只能走相反位,该位 XOR=1
if (bit ^ 1) chosen |= (1ull << b); // chosen 的该位 = bit^1
}
}
return chosen;
}
}tr;
// 处理无符号数
inline void __(){
cin>>N;
vll A(N+5);
tr.clear();
ll sum=0;
FOR(i,1,N){
cin>>A[i];
tr.insert(A[i]);
sum^=A[i];
}
// j1,j2
ll ans=0;
FOR(j1,1,N){
ll a=sum^A[j1];
ll mne=tr.argmin_xor(a),mn=tr.min_xor(a);
bool ise=tr.erase(mne);
// 那么 bob 有两种选择,一种是避开我们删除的mne,另一种是就选我们删除的mne
ll lans=min<ll>(tr.min_xor(a),mne^sum);
// 那么删除 mne 可能导致负面影响,那么我们不删除mne的结果是什么那?就是bob选择mne呗
lans=max(lans,mn);
ans=max<ll>(ans,lans);
if(ise) tr.insert(mne);
}
// tr.erase(sum);
ans=min<ll>(ans,tr.max_xor(sum));
cout<<ans<<"\n";
}
/*
1
4
1 0 3 4
AC
https://www.codechef.com/viewsolution/1183578991
*/
```
:::
## 心路历程(WA,TLE,MLE……)