再也不装逼全部扔进类里面写了 疯狂TLE 难受死了
m
a
x
(
(
a
[
i
]
+
a
[
j
]
)
max( (a[i] + a[j])
max((a[i]+a[j]) xor
a
[
k
]
)
(
i
,
j
,
k
都
不
相
同
)
a[k] ) (i, j, k都不相同)
a[k])(i,j,k都不相同)
题意:
求三个元素,使得两个的和异或第三个的值最大
#include <bits/stdc++.h>
using namespace std;
#define ls st<<1
#define rs st<<1|1
#define fst first
#define snd second
#define MP make_pair
#define PB push_back
#define LL long long
#define PII pair<int,int>
#define VI vector<int>
#define CLR(a,b) memset(a, (b), sizeof(a))
#define ALL(x) x.begin(),x.end()
#define rep(i,s,e) for(int i=(s); i<=(e); i++)
#define tep(i,s,e) for(int i=(s); i>=(e); i--)
const int INF = 0x3f3f3f3f;
const int MAXN = 2e3+10;
const int mod = 1e9+7;
const double eps = 1e-8;
#define LL long long
int read()
{
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return x*f;
}
LL arr[MAXN];
LL sz;
LL ch[32*MAXN][2];
LL val[32*MAXN], ed[32*MAXN];
void init() {
CLR(ch, 0);
CLR(val,0);
CLR(ed,0);
}
void Insert(LL x) {
int u = 0;
for (int i = 32; i >= 0; --i)
{
int c = ((x>>i)&1);
if(!ch[u][c]) {
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
val[u]++;
}
ed[u] = x;
}
void Delete(LL x) {
int u = 0;
for(int i = 32; i >= 0; --i) {
int c = ((x>>i)&1);
u = ch[u][c];
val[u]--;
}
}
LL Query(LL x) {
int u = 0;
for(int i = 32; i >= 0; --i) {
LL c = (x>>i)&1;
if(ch[u][!c] && val[ch[u][!c]]) {
u = ch[u][!c];
} else {
u = ch[u][c];
}
}
return x^ed[u];
}
LL MAX(LL x, LL y) {
if(x > y) return x;
return y;
}
int main(int argc, char const *argv[])
{
int T;
scanf("%d",&T);
while(T--) {
init();
sz = 1;
int n;
scanf("%d",&n);
LL ans = -1;
for(int i = 0; i < n; i++) {
scanf("%d",&arr[i]);
Insert(arr[i]);
}
for(int i = 0; i < n; i++) {
Delete(arr[i]);
for(int j = i+1; j < n; j++) {
Delete(arr[j]);
ans = MAX(ans, Query(arr[i]+arr[j]));
Insert(arr[j]);
}
Insert(arr[i]);
}
printf("%d\n", ans);
}
return 0;
}