hdu4825
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long long LL;
typedef pair<int, int> P;
typedef pair<P, ll> LP;
const ll inf = 1e9 + 100;
const int N = 1e5 + 10;
const ll mod = 1e9 + 7;
const int base = 131;
const double pi = acos ( -1 );
const double eps = 1e-8;
unordered_map<ll, ll> mp;
ll l[N], r[N], a[N], num[N], block;
ll t, p, vis[N], b[N];
ll x, y, n, m, ans;
ll res[N];
vector<ll> v[N];
vector<ll> gr;
int tol; //节点个数
LL val[32*N]; //点的值
int ch[32*N][2]; //边的值
void init()
{ //初始化
tol=1;
ch[0][0]=ch[0][1]=0;
}
void insert(LL x)
{ //往 01字典树中插入 x
int u=0;
for(int i=32;i>=0;i--)
{
int v=(x>>i)&1;
if(!ch[u][v])
{ //如果节点未被访问过
ch[tol][0]=ch[tol][1]=0; //将当前节点的边值初始化
val[tol]=0; //节点值为0,表示到此不是一个数
ch[u][v]=tol++; //边指向的节点编号
}
u=ch[u][v]; //下一节点
}
val[u]=x; //节点值为 x,即到此是一个数
}
LL query(LL x)
{ //查询所有数中和 x异或结果最大的数
int u=0;
for(int i=32;i>=0;i--)
{
int v=(x>>i)&1;
//利用贪心策略,优先寻找和当前位不同的数 如果找最小的即寻找相同的数
if(ch[u][v^1]) u=ch[u][v^1];
else u=ch[u][v];
}
return val[u]; //返回结果
}
int main()
{
//ios::sync_with_stdio ( false );
//cin.tie ( 0 );
//cout.tie ( 0 );
cin >> t ;
for ( int tt = 1; tt <= t; tt++ )
{
cin >> n >> m;
init();
for ( int i = 1; i <= n; i++ )
{
cin >> x;
insert ( x );
}
printf ( "Case #%d:\n", tt );
while ( m-- )
{
cin >> x;
cout << query ( x ) << endl;
}
}
}
HDU 5536 (带删除)
http://acm.hdu.edu.cn/showproblem.php?pid=5536
带有删除的01字典树,先把每个元素插入字典树中,然后O(n2)O(n2)的复杂度枚举两个相加,并且把它们从字典树中暂时去掉,然后查询,然后取最大值
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long long LL;
typedef pair<int, int> P;
typedef pair<P, ll> LP;
const ll inf = 1e9 + 100;
const int N = 1e5 + 10;
const ll mod = 1e9 + 7;
const int base = 131;
const double pi = acos ( -1 );
const double eps = 1e-8;
//#define a(i,j) a[(i-1)*m+(j)]
//#define b(i,j) b[(i-1)*m+(j)]
unordered_map<ll, ll> mp;
ll t,n,a[N],x,ans;
int tol; //节点个数
ll val[32*N]; //点的值
int ch[32*N][2]; //边的值
int num[32*N];
void init()
{ //初始化
tol=1;
ch[0][0]=ch[0][1]=0;
}
void insert(ll x)
{ //往 01字典树中插入 x
int u=0;
for(int i=32;i>=0;i--)
{
int v=(x>>i)&1;
if(!ch[u][v])
{ //如果节点未被访问过
ch[tol][0]=ch[tol][1]=0; //将当前节点的边值初始化
val[tol]=0; //节点值为0,表示到此不是一个数
num[tol]=0;
ch[u][v]=tol++; //边指向的节点编号
}
u=ch[u][v]; //下一节点
num[u]++;
}
val[u]=x; //节点值为 x,即到此是一个数
}
void update(ll a,int d)
{
int u=0;
for(int i=32;i>=0;i--)
{
int c=((a>>i)&1);
u=ch[u][c];
num[u]+=d;
}
}
ll query(ll x)
{ //查询所有数中和 x异或结果最大的数
int u=0;
for(int i=32;i>=0;i--)
{
int v=((x>>i)&1);
//利用贪心策略,优先寻找和当前位不同的数
if(ch[u][v^1]&&num[ch[u][v^1]]) u=ch[u][v^1];
else u=ch[u][v];
}
return x^val[u]; //返回结果
}
int main()
{
ios::sync_with_stdio ( false );
cin.tie ( 0 );
//cout.tie ( 0 );
cin >> t ;
for ( int tt = 1; tt <= t; tt++ )
{
cin >> n ;
init();
for ( int i = 1; i <= n; i++ )
{
cin >> a[i];
insert ( a[i] );
}
// printf ( "Case #%d:\n", tt );
ans=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j) continue;
update(a[i],-1);update(a[j],-1);
ans=max(ans,query(a[i]+a[j]));
update(a[i],1);update(a[j],1);
}
}
cout<<ans<<endl;
}
}