bzoj3261 最大异或和(可持久化字典树)
2016-09-08 19:47
316 查看
题目链接:点这里!!
题意:
给定一个非负整数序列 {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
xor x 最大,输出最大是多少。
(N,M<=300000)
题解:
可持久化字典树第一题,基础的可持久化字典树。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)
#define key_value ch[ch[root][1]][0]
#pragma comment(linker, "/STACK:102400000000,102400000000")
typedef long long LL;
const LL MOD = 1000000007;
const int N = 6e5+15;
const int maxn = 1e6+15;
const int letter = 130;
const int INF = 1e9;
const double pi=acos(-1.0);
const double eps=1e-10;
using namespace std;
inline 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*10+ch-'0';ch=getchar();}
return x*f;
}
int n,q,a
;
int bin[30],ch[N*25][2],sum[N*25],root
,cnt;
int trie_insert(int x,int val){
int tmp,y;
tmp=y=++cnt;
for(int i=23;i>=0;i--){
ch[y][0]=ch[x][0],ch[y][1]=ch[x][1];
sum[y]=sum[x]+1;
int t=val&bin[i];
t>>=i;
x=ch[x][t];
ch[y][t]=++cnt;
y=ch[y][t];
}
sum[y]=sum[x]+1;
return tmp;
}
int trie_query(int l,int r,int val){
int tmp=0;
for(int i=23;i>=0;i--){
int t=val&bin[i];
t>>=i;
if(sum[ch[r][t^1]]-sum[ch[l][t^1]]>0)
tmp+=bin[i],r=ch[r][t^1],l=ch[l][t^1];
else r=ch[r][t],l=ch[l][t];
}
return tmp;
}
int main(){
bin[0]=1;
for(int i=1;i<30;i++) bin[i]=bin[i-1]<<1;
scanf("%d%d",&n,&q);
n++;
for(int i=2;i<=n;i++) scanf("%d",a+i);
for(int i=1;i<=n;i++) a[i]^=a[i-1];
for(int i=1;i<=n;i++)
root[i]=trie_insert(root[i-1],a[i]);
char s[5];
int l,r,x;
while(q--){
scanf("%s",s);
if(s[0]=='A'){
scanf("%d",&a[++n]),a
^=a[n-1];
root
=trie_insert(root[n-1],a
);
}
else {
scanf("%d%d%d",&l,&r,&x);
printf("%d\n",trie_query(root[l-1],root[r],a
^x));
}
}
return 0;
}
题意:
给定一个非负整数序列 {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
xor x 最大,输出最大是多少。
(N,M<=300000)
题解:
可持久化字典树第一题,基础的可持久化字典树。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)
#define key_value ch[ch[root][1]][0]
#pragma comment(linker, "/STACK:102400000000,102400000000")
typedef long long LL;
const LL MOD = 1000000007;
const int N = 6e5+15;
const int maxn = 1e6+15;
const int letter = 130;
const int INF = 1e9;
const double pi=acos(-1.0);
const double eps=1e-10;
using namespace std;
inline 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*10+ch-'0';ch=getchar();}
return x*f;
}
int n,q,a
;
int bin[30],ch[N*25][2],sum[N*25],root
,cnt;
int trie_insert(int x,int val){
int tmp,y;
tmp=y=++cnt;
for(int i=23;i>=0;i--){
ch[y][0]=ch[x][0],ch[y][1]=ch[x][1];
sum[y]=sum[x]+1;
int t=val&bin[i];
t>>=i;
x=ch[x][t];
ch[y][t]=++cnt;
y=ch[y][t];
}
sum[y]=sum[x]+1;
return tmp;
}
int trie_query(int l,int r,int val){
int tmp=0;
for(int i=23;i>=0;i--){
int t=val&bin[i];
t>>=i;
if(sum[ch[r][t^1]]-sum[ch[l][t^1]]>0)
tmp+=bin[i],r=ch[r][t^1],l=ch[l][t^1];
else r=ch[r][t],l=ch[l][t];
}
return tmp;
}
int main(){
bin[0]=1;
for(int i=1;i<30;i++) bin[i]=bin[i-1]<<1;
scanf("%d%d",&n,&q);
n++;
for(int i=2;i<=n;i++) scanf("%d",a+i);
for(int i=1;i<=n;i++) a[i]^=a[i-1];
for(int i=1;i<=n;i++)
root[i]=trie_insert(root[i-1],a[i]);
char s[5];
int l,r,x;
while(q--){
scanf("%s",s);
if(s[0]=='A'){
scanf("%d",&a[++n]),a
^=a[n-1];
root
=trie_insert(root[n-1],a
);
}
else {
scanf("%d%d%d",&l,&r,&x);
printf("%d\n",trie_query(root[l-1],root[r],a
^x));
}
}
return 0;
}
相关文章推荐
- HDU 6191 Query on A Tree (dfs序+可持久化01Trie)
- BZOJ 3261 最大异或和 && qwb VS 去污棒(可持久化01Trie)
- HDU 4757 树链剖分+可持久化字典树
- HDU 6191 DFS序+可持久化字典树
- HDU 6191 2017广西邀请赛Query on A Tree:可持久化01字典树(区间抑或最大值查询)
- 多种方式垂直水平居中
- Java 求全排列的两种方式
- 关于memset的注意事项
- Java类与对象(一)
- linux查看日志常用命令
- Android----给Ta惊喜,带音乐的动态相册
- JAVA基础6.45——继承概述(1)
- linux系统性能监控常用命令
- POJ 2449 A*k短路
- 浅析MySQL中exists与in的使用
- Qt之QComboBox
- 【IOS学习】NSString copy or not (strong)?
- 创建HelloWorld项目
- 添加阴影
- [置顶] javascript自定义事件应用实例