HDU - 5536 Chip Factory ( 01Trie树 带删除

再也不装逼全部扔进类里面写了 疯狂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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值