题目类型:N个数字,任取两个数字使得他们的XOR值最大
一般采用贪心+字典序解决。
http://blog.csdn.net/u012915516/article/details/49534155
如果涉及到区间内区任取两个数使得XOR的值最大则需要到贪心+可持久化字典树。
BZOJ3166以及BZOJ3261就是涉及到区间内任选两个数的最大XOR值~~~
BZOJ3261:
题意:
给定一个非负整数序列 {a},初始长度为 N。
有 M个操作,有以下两种操作类型:
1 、A x:添加操作,表示在序列末尾添加一个数 x,序列的长度 N+1。
2 、Q l r x:询问操作,你需要找到一个位置 p,满足 l<=p<=r,使得:
a[p] xor a[p+1] xor ... xor a[N] xor x 最大,输出最大是多少。
要使 a[p] xor a[p+1] xor ... xor a[N] xor x 最大
相当于 x xor sum[n] xor sum[p-1],sum[i]:=前i个数字的异或值
也就是问p-1 属于[l-1,r-1]这个区间范围,使得上式最大的p-1~~~~
以sum[i]为点建立可持久化字典树,即可查询区间内使得与X xor的最大值
#include <algorithm>
#include <iostream>
#include<string.h>
#include <fstream>
#include <math.h>
#include <vector>
#include <cstdio>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define exp 1e-8
#define fi first
#define ll long long
#define INF 0x3f3f3f3f3f3f3f3f
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define all(a) a.begin(),a.end()
#define mm(a,b) memset(a,b,sizeof(a));
#define for0(a,b) for(int a=0;a<=b;a++)//0---(b-1)
#define for1(a,b) for(int a=1;a<=b;a++)//1---(b)
#define rep(a,b,c) for(int a=b;a<=c;a++)//b---c
#define repp(a,b,c)for(int a=b;a>=c;a--)///
#define cnt_one(i) __builtin_popcount(i)
#define stl(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
using namespace std;
void bug(string m="here"){cout<<m<<endl;}
template<typename __ll> inline void READ(__ll &m){__ll 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*10+ch-'0';ch=getchar();}m=x*f;}
template<typename __ll>inline void read(__ll &m){READ(m);}
template<typename __ll>inline void read(__ll &m,__ll &a){READ(m);READ(a);}
template<typename __ll>inline void read(__ll &m,__ll &a,__ll &b){READ(m);READ(a);READ(b);}
template<typename __ll>inline void read(__ll &m,__ll &a,__ll &b,__ll &c){READ(m);READ(a);READ(b);READ(c);}
template<typename __ll>inline void read(__ll &m,__ll &a,__ll &b,__ll &c,__ll &d){READ(m);READ(a);READ(b);READ(c);read(d);}
template < class T > inline void out(T a){if(a<0){putchar('-');a=-a;}if(a>9)out(a/10);putchar(a%10+'0');}
template < class T > inline void outln(T a){out(a);puts("");}
template < class T > inline void out(T a,T b){out(a);putchar(' ');out(b);}
template < class T > inline void outln(T a,T b){out(a);putchar(' ');outln(b);}
template < class T > inline void out(T a,T b,T c){out(a);putchar(' ');out(b);putchar(' ');out(c);}
template < class T > inline void outln(T a,T b,T c){out(a);putchar(' ');outln(b);putchar(' ');outln(b);}
template < class T > T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template < class T > T lcm(T a, T b) { return a / gcd(a, b) * b; }
template < class T > inline void rmin(T &a, const T &b) { if(a > b) a = b; }
template < class T > inline void rmax(T &a, const T &b) { if(a < b) a = b; }
template < class T > T pow(T a, T b) { T r = 1; while(b > 0) { if(b & 1) r = r * a; a = a * a; b /= 2; } return r; }
template < class T > T pow(T a, T b, T mod) { T r = 1; while(b > 0) { if(b & 1) r = r * a % mod; a = a * a % mod; b /= 2; } return r; }
const int maxn=600010;
int a[maxn],b[maxn],root[maxn];
int n,m;
struct trie
{
int tot;
int ch[maxn*25][2],sum[maxn*25];
void init()
{
tot=1;
sum[0]=0;
ch[0][0]=ch[0][1]=0; ///构成root[0]的trie树
}
void insert(int &rt,int y,int val)
{
int x=rt=tot++;
for(int i=23;i>=0;i--)
{
sum[x]=sum[y]+1;
ch[x][0]=ch[y][0],ch[x][1]=ch[y][1];
int tmp=(val>>i)&1;
x=ch[x][tmp]=tot++;
y=ch[y][tmp];
}
sum[x]=sum[y]+1;
ch[x][0]=ch[y][0],ch[x][1]=ch[y][1];
}
int query(int l,int r,int val)
{
int ret=0;
for(int i=23;i>=0;i--)
{
int tmp=(val>>i)&1;
if(sum[ch[r][tmp^1]]-sum[ch[l][tmp^1]])
ret|=1<<i,r=ch[r][tmp^1],l=ch[l][tmp^1];
else r=ch[r][tmp],l=ch[l][tmp];
}
return ret;
}
}trie;
int main()
{
read(n,m);n++;///偏移一个位置
for(int i=2;i<=n;i++)read(a[i]);
for1(i,n)b[i]=b[i-1]^a[i];
trie.init();
for1(i,n)trie.insert(root[i],root[i-1],b[i]);
while(m--)
{
char ch[10];
int l,r,k;
scanf("%s",ch);
if(ch[0]=='A')
{
n++;
read(a[n]);
b[n]=b[n-1]^a[n];
trie.insert(root[n],root[n-1],b[n]);
}
else
{
read(l,r,k);
outln(trie.query(root[l-1],root[r],b[n]^k));
}
}
}
BZOJ3166:
题意,任选一个区间,定义这个区间的次大值与这个区间内的任意一个值的XOR为这个区间的价值。问最大的价值时多少~~~~
枚举每个数作为次大的区间,在这个区间内找出与这个次大值XOR最大的值~~~
所以得建立可持久化字典树。
难点在于如何找出这个区间~~
这个区间就是左边第二个比它大的下标+1到右边第二个比它大的下标-1
利用multiset,每次插入val最大的值进行维护
#include <algorithm>
#include <iostream>
#include<string.h>
#include <fstream>
#include <math.h>
#include <vector>
#include <cstdio>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define exp 1e-8
#define fi first
#define ll long long
#define INF 0x3f3f3f3f3f3f3f3f
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define all(a) a.begin(),a.end()
#define mm(a,b) memset(a,b,sizeof(a));
#define for0(a,b) for(int a=0;a<=b;a++)//0---(b-1)
#define for1(a,b) for(int a=1;a<=b;a++)//1---(b)
#define rep(a,b,c) for(int a=b;a<=c;a++)//b---c
#define repp(a,b,c)for(int a=b;a>=c;a--)///
#define cnt_one(i) __builtin_popcount(i)
#define stl(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
using namespace std;
void bug(string m="here"){cout<<m<<endl;}
template<typename __ll> inline void READ(__ll &m){__ll 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*10+ch-'0';ch=getchar();}m=x*f;}
template<typename __ll>inline void read(__ll &m){READ(m);}
template<typename __ll>inline void read(__ll &m,__ll &a){READ(m);READ(a);}
template<typename __ll>inline void read(__ll &m,__ll &a,__ll &b){READ(m);READ(a);READ(b);}
template<typename __ll>inline void read(__ll &m,__ll &a,__ll &b,__ll &c){READ(m);READ(a);READ(b);READ(c);}
template<typename __ll>inline void read(__ll &m,__ll &a,__ll &b,__ll &c,__ll &d){READ(m);READ(a);READ(b);READ(c);read(d);}
template < class T > inline void out(T a){if(a<0){putchar('-');a=-a;}if(a>9)out(a/10);putchar(a%10+'0');}
template < class T > inline void outln(T a){out(a);puts("");}
template < class T > inline void out(T a,T b){out(a);putchar(' ');out(b);}
template < class T > inline void outln(T a,T b){out(a);putchar(' ');outln(b);}
template < class T > inline void out(T a,T b,T c){out(a);putchar(' ');out(b);putchar(' ');out(c);}
template < class T > inline void outln(T a,T b,T c){out(a);putchar(' ');outln(b);putchar(' ');outln(b);}
template < class T > T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template < class T > T lcm(T a, T b) { return a / gcd(a, b) * b; }
template < class T > inline void rmin(T &a, const T &b) { if(a > b) a = b; }
template < class T > inline void rmax(T &a, const T &b) { if(a < b) a = b; }
template < class T > T pow(T a, T b) { T r = 1; while(b > 0) { if(b & 1) r = r * a; a = a * a; b /= 2; } return r; }
template < class T > T pow(T a, T b, T mod) { T r = 1; while(b > 0) { if(b & 1) r = r * a % mod; a = a * a % mod; b /= 2; } return r; }
const int maxn=50010;
struct data
{
int val,pos;
bool operator < (const data &rhs)const{
return val>rhs.val;
}
}dat[maxn];
int n;
int root[maxn];
struct trie
{
int tot;
int ch[maxn*35][2],sum[maxn*35];
void init()
{
tot=1;
sum[0]=0;
ch[0][0]=ch[0][1]=0; ///构成root[0]的trie树
}
void insert(int &rt,int y,int val)
{
int x=rt=tot++;
for(int i=31;i>=0;i--)
{
sum[x]=sum[y]+1;
ch[x][0]=ch[y][0],ch[x][1]=ch[y][1];
int tmp=(val>>i)&1;
x=ch[x][tmp]=tot++;
y=ch[y][tmp];
}
sum[x]=sum[y]+1;
ch[x][0]=ch[y][0],ch[x][1]=ch[y][1];
}
int query(int l,int r,int val)
{
int ret=0;
for(int i=31;i>=0;i--)
{
int tmp=(val>>i)&1;
if(sum[ch[r][tmp^1]]-sum[ch[l][tmp^1]])
ret|=1<<i,r=ch[r][tmp^1],l=ch[l][tmp^1];
else r=ch[r][tmp],l=ch[l][tmp];
}
return ret;
}
}trie;
int main()
{
read(n);
for1(i,n)read(dat[i].val),dat[i].pos=i;
trie.init();
for1(i,n)trie.insert(root[i],root[i-1],dat[i].val);
multiset<int>ss;
ss.insert(0),ss.insert(0),ss.insert(n+1),ss.insert(n+1);
sort(dat+1,dat+1+n);
ss.insert(dat[1].pos);
int ans=0;
for(int i=2;i<=n;i++)
{
multiset<int>::iterator tmp;
tmp=ss.upper_bound(dat[i].pos);
tmp--,tmp--;
int l=(*tmp)+1;
tmp++,tmp++,tmp++;
int r=(*tmp)-1;
ans=max(ans,trie.query(root[l-1],root[r],dat[i].val));
ss.insert(dat[i].pos);
}
outln(ans);
}