题意:给定一个非负整数序列{a},初始长度为N。

有M个操作,有以下两种操作类型:

1、Ax:添加操作,表示在序列末尾添加一个数x,序列的长度N+1。

2、Qlrx:询问操作,你需要找到一个位置p,满足l<=p<=r,使得:

a[p] xor a[p+1] xor ... xor a[N] xor x 最大,输出最大是多少。

题解:可持久化trie

用前缀异或来建树,查询就变成了last^x和l到r中a【p】异或最大值是多少

先插入一个0,然后像可持久化线段树那样建树即可,还是挺简单的

/**************************************************************
Problem: 3261
User: walfy
Language: C++
Result: Accepted
Time:4584 ms
Memory:179416 kb
****************************************************************/ //#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 998244353
#define ld long double
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
//#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
template<typename T>
inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>
inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;} using namespace std; const double eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=600000+10,maxn=100000+10,inf=0x3f3f3f3f; struct trie{
int sz,cnt,root[N],ch[N*25][2],num[N*25];
void init()
{
sz=cnt=0;
root[++sz]=++cnt;
int now=root[sz];
for(int i=24;i>=0;i--)
{
ch[now][0]=++cnt;
now=ch[now][0];
num[now]++;
}
}
void add(int x)
{
root[++sz]=++cnt;
int now=root[sz],last=root[sz-1];
for(int i=24;i>=0;i--)
{
ch[now][(x>>i)&1]=++cnt;
ch[now][!((x>>i)&1)]=ch[last][!((x>>i)&1)];
now=ch[now][(x>>i)&1];
num[now]=num[ch[last][(x>>i)&1]]+1;
last=ch[last][(x>>i)&1];
}
}
void query(int l,int r,int x)
{
int now=root[r],last=root[l],ans=0;
for(int i=24;i>=0;i--)
{
// if(now==128)printf("%d %d %d %d\n",now,last,num[ch[now][!((x>>i)&1)]],num[ch[last][!((x>>i)&1)]]);
if(num[ch[now][!((x>>i)&1)]]>num[ch[last][!((x>>i)&1)]])
{
now=ch[now][!((x>>i)&1)];
last=ch[last][!((x>>i)&1)];
ans+=(1<<i);
// printf("%d %d\n",now,0);
}
else
{
now=ch[now][(x>>i)&1];//,printf("%d %d\n",now,1);
last=ch[last][(x>>i)&1];
}
// printf("%d--\n",now);
}
printf("%d\n",ans);
}
void debug(int now)
{
printf("%d %d %d %d\n",now,num[now],ch[now][0],ch[now][1]);
if(ch[now][0])debug(ch[now][0]);
if(ch[now][1])debug(ch[now][1]);
}
}tree;
int main()
{
int n,m,sum=0;
scanf("%d%d",&n,&m);
tree.init();
for(int i=1;i<=n;i++)
{
int a;scanf("%d",&a);
a^=sum;sum=a;tree.add(a);
// printf("%d---\n",a);
}
while(m--)
{
char op[5];scanf("%s",&op);
if(op[0]=='A')
{
int x;scanf("%d",&x);
x^=sum;sum=x;tree.add(x);
// printf("%d---\n",x);
}
else
{
int l,r,x;
scanf("%d%d%d",&l,&r,&x);
x^=sum;tree.query(l-1,r,x);
// printf("%d+++\n",x);
}
}
// tree.debug(tree.root[2]);
return 0;
}
/*****************
5 5
2 6 4 3 6
A 1
Q 3 5 4
A 4
Q 5 7 0
Q 3 6 6
*****************/

bzoj3261: 最大异或和 可持久化trie的更多相关文章

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

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

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

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

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

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

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

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

  5. 洛谷.5283.[十二省联考2019]异或粽子(可持久化Trie 堆)

    LOJ 洛谷 考场上都拍上了,8:50才发现我读错了题=-= 两天都读错题...醉惹... \(Solution1\) 先求一遍前缀异或和. 假设左端点是\(i\),那么我们要在\([i,n]\)中找 ...

  6. 【xsy1147】 异或(xor) 可持久化trie

    我的脑回路可能比较奇怪. 我们对这些询问离线,将所得序列${a}$的后缀和建$n$棵可持久化$trie$. 对于一组询问$(l,r,x)$,我们在主席树上询问第$l$棵树$-$第r$+1$棵树中与$s ...

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

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

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

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

  9. BZOJ 3261 最大异或和 可持久化Trie树

    题目大意:给定一个序列,提供下列操作: 1.在数组结尾插入一个数 2.给定l,r,x,求一个l<=p<=r,使x^a[p]^a[p+1]^...^a[n]最大 首先我们能够维护前缀和 然后 ...

随机推荐

  1. 在VMware14上安装centos6.5

    打开vmware14  =>  创建新虚拟机(即再建一个linux),已有光盘映像文件,正常操作即可.

  2. 函数嵌套函数传递this值

    <button onclick="demo()(this)">test</button> function demo(){ return function ...

  3. EF Core In-Memory Database Provider

    原文链接 This can be useful for testing, although the SQLite provider in in-memory mode may be a more ap ...

  4. angularjs启动项目报ERROR in AppModule is not an NgModule解决方法

    这主要是ts编译器版本问题,一般是因为ts编译器版本过高导致. 解决方式: npm uninstall -g typescript npm install -g typescript tsc -v 查 ...

  5. 每天一个小程序—第0001题(uuid模块)

    第 0001 题:  做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)? 一开始以为是 ...

  6. ERR! registry error parsing json

    报错日志: ERR! registry error parsing json ERR! registry error parsing json 解决过程: 从github上克隆一个项目,在npm i的 ...

  7. 【三十五】thinkphp之视图

    1.模板定义 视图属于mvc中的v.一般是html结合php获取的数据提供给用户使用. 每个模板的文件都是独立的(文件名与控制器名称必须一样) 默认的模板文件规则是视图目录/[模板主题]控制器/操作名 ...

  8. LCA 模板

    关于LCA: LCA 指树上两点的公共祖先. 如何 “暴力” 找两点的 LCA : 可以先 DFS 一遍求出每个点的 dep (深度).然后从深度大的点先往上跳,跳到与另一个点相同的深度,如果还没有到 ...

  9. P1262 间谍网络

    传送门 思路: ①在 Tarjan 的基础上加一个 belong 记录每个点属于哪个强连通分量. ②存图完成后,暴力地遍历全图,查找是否要间谍不愿受贿. inline void dfs(int u) ...

  10. MySQL 并发测试中,线程数和数据库连接池的实验

    我一直以来,对性能测试中,连接池的大小要如何配置,不是太清楚: 就我所知道的,就DB自带对连接数的限制,在sqlserver中用select @@connection 可以查到, 在代码中,可以配置D ...