题目大意:给定一个序列,提供下列操作:

1.在数组结尾插入一个数

2.给定l,r,x,求一个l<=p<=r,使x^a[p]^a[p+1]^...^a[n]最大

首先我们能够维护前缀和 然后就是使x^sum[n]^sum[p-1]最大

x^sum[n]为定值,于是用Trie树贪心就可以

考虑到l-1<=p-1<=r-1,我们不能对于每一个询问都建一棵Trie树,可是我们能够对于Trie数维护前缀和,建立可持久化Trie树

每一个区间[l,r]的Trie树为tree[r]-tree[l-1]

注意0要插入一个数字0。所以把-1作为空节点。然后把数组向前推进一位就可以

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define M 600600
using namespace std;
struct Trie{
int cnt;
Trie *son[2];
}*tree[M],node[14000000];
int n,m,tot,sum[M];
inline int getc() {
static const int L = 1 << 15;
static char buf[L], *S = buf, *T = buf;
if (S == T) {
T = (S = buf) + fread(buf, 1, L, stdin);
if (S == T)
return EOF;
}
return *S++;
}
inline int getint() {
int c;
while(!isdigit(c = getc()));
int tmp = c - '0';
while(isdigit(c = getc()))
tmp = (tmp << 1) + (tmp << 3) + c - '0';
return tmp;
}
inline int getch() {
int c;
while((c = getc()) != 'A' && c != 'Q');
return c;
}
inline Trie* New_Node(int _,Trie*__,Trie*___)
{
node[tot].cnt=_;
node[tot].son[0]=__;
node[tot].son[1]=___;
return &node[tot++];
}
Trie* Build_Tree(Trie *p,int x,int pos)
{
if(!pos)
return New_Node(p->cnt+1,tree[0],tree[0]);
if( (x&pos)==0 )
return New_Node(p->cnt+1,Build_Tree(p->son[0],x,pos>>1),p->son[1]);
else
return New_Node(p->cnt+1,p->son[0],Build_Tree(p->son[1],x,pos>>1));
}
int Get_Ans(Trie *l,Trie *r,int x,int pos)
{
int num=x&pos? 1:0;
if(!pos)
return 0;
if(r->son[!num]->cnt-l->son[!num]->cnt)
return pos + Get_Ans(l->son[!num],r->son[!num],x,pos>>1);
else
return Get_Ans(l->son[num],r->son[num],x,pos>>1);
}
int main()
{
int i,x,l,r;
char p[10];
tree[0]=New_Node(0,0x0,0x0);
tree[0]->son[0]=tree[0]->son[1]=tree[0];
tree[1]=Build_Tree(tree[0],0,1<<25);
cin>>n>>m;
for(i=1;i<=n;i++)
{
x=getint();
sum[i]=sum[i-1]^x;
tree[i+1]=Build_Tree(tree[i],sum[i],1<<25);
}
for(i=1;i<=m;i++)
{
p[0]=getch();
if(p[0]=='A')
{
x=getint();
sum[n+1]=sum[n]^x;
tree[n+2]=Build_Tree(tree[n+1],sum[n+1],1<<25);
++n;
}
else
{
l=getint();
r=getint();
x=getint();
x^=sum[n];--l;--r;
printf("%d\n", Get_Ans(tree[l],tree[r+1],x,1<<25) );
}
}
}

BZOJ 3261 最大异或和 可持久化Trie树的更多相关文章

  1. BZOJ 3261: 最大异或和( 可持久化trie )

    搞成前缀和然后就可以很方便地用可持久化trie维护了.时间复杂度O((N+M)*25) -------------------------------------------------------- ...

  2. bzoj 3261 最大异或和 可持久化字典树(01树)

    题目传送门 思路: 由异或的性质可得,题目要求的式子可以转化成求$max(pre[n]^x^pre[i])$,$pre[i]$表示前缀异或和,那么我们现在就要求出这个东西,所以用可持久化字典树来求,每 ...

  3. [十二省联考2019]异或粽子——可持久化trie树+堆

    题目链接: [十二省联考2019]异或粽子 求前$k$大异或区间,可以发现$k$比较小,我们考虑找出每个区间. 为了快速得到一个区间的异或和,将原序列做前缀异或和. 对于每个点作为右端点时,我们维护出 ...

  4. BZOJ3261: 最大异或和(可持久化trie树)

    题意 题目链接 Sol 设\(sum[i]\)表示\(1 - i\)的异或和 首先把每个询问的\(x \oplus sum[n]\)就变成了询问前缀最大值 可持久化Trie树维护前缀xor,建树的时候 ...

  5. 【bzoj3261】最大异或和 可持久化Trie树

    题目描述 给定一个非负整数序列 {a},初始长度为 N.       有M个操作,有以下两种操作类型:1.A x:添加操作,表示在序列末尾添加一个数 x,序列的长度 N+1.2.Q l r x:询问操 ...

  6. 【bzoj3689】异或之 可持久化Trie树+堆

    题目描述 给定n个非负整数A[1], A[2], ……, A[n].对于每对(i, j)满足1 <= i < j <= n,得到一个新的数A[i] xor A[j],这样共有n*(n ...

  7. 洛谷P4592 [TJOI2018]异或 【可持久化trie树】

    题目链接 BZOJ4592 题解 可持久化trie树裸题 写完就A了 #include<algorithm> #include<iostream> #include<cs ...

  8. [BZOJ4103][Thu Summer Camp 2015]异或运算 可持久化Trie树

    4103: [Thu Summer Camp 2015]异或运算 Time Limit: 20 Sec  Memory Limit: 512 MB Description 给定长度为n的数列X={x1 ...

  9. BZOJ 3261 最大异或和 (可持久化01Trie)

    题目大意:让你维护一个序列,支持在序列末插入一个数,支持询问$[l,r]$区间内选择一个位置$p$,使$xor\sum_{i=p}^{n}a_{i}$最大 可持久化$01Trie$裸题,把 区间异或和 ...

随机推荐

  1. Hibernate 入门示例

    版权声明:本文为博主原创文章,如需转载请标注转载地址 博客地址:http://www.cnblogs.com/caoyc/p/5593406.html  环境: myelipse2015+Hibern ...

  2. 单例模式的Oracle 数据库连接应用

    新建一个类来实现单例模式的Oracle 数据库连接应用 组织架构如下: 类的具体代码如下: package com.zse.dba; import java.sql.*; //设计模式1:单例模式.保 ...

  3. mysql表属性、索引、约束

    1.表属性 创建表的基本语法: create table [if not exists] 表名 (字段列表 [,索引或约束列表])[表选项列表] 其中,字段列表格式如下: 字段名 类型 [属性列表], ...

  4. 前端JS脚本将网页表格导出为Excel

    话不多说,上代码! <!DOCTYPE> <html> <head> <title>Excel Test</title> </head ...

  5. python基础篇---实战---用户登入注册程序

    一.首先了解需求: 1.支持多个用户登入 2.登入成功后显示欢迎,并退出程序 3.登入三次失败后,退出程序,并在下次程序启动尝试登入时,该用户名依然是锁定状态 二.文件代码如下: f = open(& ...

  6. 不懂Git,别说自己是程序猿–20分钟git快速上手(转)

    在Git如日中天的今天,不懂git都不好意思跟人说自己是程序猿.你是不是早就跃跃欲试了,只是苦于没有借口(契机). 好吧,机会就在今天. 给我20分钟,是的,只要20分钟, 让你快速用上git. 我们 ...

  7. mongodb - 查看正在执行的操作

    查看正在执行的操作 db.currentOp() 查看系统执行的操作 db.currentOp(True) kill正在执行的操作 db.killOp(<operation id>) 示例 ...

  8. JAVA设计模式之 命令模式【Command Pattern】

    一.概述 命令模式能够将请求发送者和接收者全然解耦.发送者与接收者之间没有直接引用关系,发送请求的对象仅仅须要知道怎样发送请求,而不必知道怎样完毕请求.核心在于引入了命令类,通过命令类来减少发送者和接 ...

  9. God web

    http://my.csdn.net/yerenyuan_pku http://evilcos.me/?p=336 https://www.zhihu.com/question/21606800 st ...

  10. Makefile 自动变量之 $(@D),$(@F)

    参考:http://www.gnu.org/software/make/manual/make.html '$(@D)'The directory part of the file name of t ...