BZOJ 3261 可持久化字典树

3261: 最大异或和

Time Limit: 10 Sec  Memory Limit: 512 MB
Submit: 278  Solved: 110
[ Submit][ Status]

Description

     

给定一个非负整数序列 {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 最大,输出最大是多少。  

Input

第一行包含两个整数 N  ,M,含义如问题描述所示。  
第二行包含 N个非负整数,表示初始的序列 A 。
 
接下来 M行,每行描述一个操作,格式如题面所述。   

Output

假设询问操作有 T个,则输出应该有 T行,每行一个整数表示询问的答案。

Sample Input

5 5
2 6 4 3 6
A 1
Q 3 5 4
A 4
Q 5 7 0
Q 3 6 6
对于测试点 1-2,N,M<=5 。

对于测试点 3-7,N,M<=80000 。
对于测试点 8-10,N,M<=300000 。

其中测试点 1, 3, 5, 7, 9保证没有修改操作。
对于 100% 的数据, 0<=a[i]<=10^7。

Sample Output

4
5
6

HINT

对于      100%  的数据,     0<=a[i]<=10^7 

看别人写的,刚刚理解,建立字典树的时候顺便保留历史版本。

代码:

/* ***********************************************
Author :xianxingwuguan
Created Time :2014-2-14 17:43:27
File Name :1.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int maxn=700000;
const int maxt=16000000;
int root[maxn],tot,s[maxt],ch[maxt][2];
	void insert(int x,int &y,int v,int d){
		s[y=++tot]=s[x]+1;
		if(d<0)return;
		int p=(v>>d)&1;
		ch[y][p^1]=ch[x][p^1];
		insert(ch[x][p],ch[y][p],v,d-1);
	}
	int query(int x,int y,int v,int d){
		if(d<0)return 0;
		int p=(v>>d)&1;
		if(s[ch[y][p^1]]!=s[ch[x][p^1]])
			return (1<<d)+query(ch[x][p^1],ch[y][p^1],v,d-1);
		return query(ch[x][p],ch[y][p],v,d-1);
	}
int a[maxn],b[maxn];
int main()
{
     //freopen("data.in","r",stdin);
     //freopen("data.out","w",stdout);
 	 int n,m;
	 while(~scanf("%d%d",&n,&m)){
		n++;tot=0;
		insert(root[0],root[1],0,24);
		for(int i=2;i<=n;i++){
			scanf("%d",&a[i]);
			insert(root[i-1],root[i],b[i]=b[i-1]^a[i],24);
		}
		while(m--){
			char op[10];
			scanf("%s",op);
			if(op[0]=='A'){
				scanf("%d",&a[++n]);
				insert(root[n-1],root[n],b[n]=b[n-1]^a[n],24);
			}
			else {
				int l,r,x;
				scanf("%d%d%d",&l,&r,&x);
				printf("%d\n",query(root[l-1],root[r],b[n]^x,24));
			}
		}
	 }
     return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值