BZOJ 3261 可持久化字典树

2014-11-24 09:05:36 · 作者: · 浏览: 0

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 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         #include 
        
          #include 
         
           #include 
          
            #include 
           
             #include 
            
              #include 
             
               #include 
              
                #include 
                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<