bzoj3261: 最大异或和 可持久化trie
题意:给定一个非负整数序列{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的更多相关文章
- BZOJ3261: 最大异或和(可持久化trie树)
题意 题目链接 Sol 设\(sum[i]\)表示\(1 - i\)的异或和 首先把每个询问的\(x \oplus sum[n]\)就变成了询问前缀最大值 可持久化Trie树维护前缀xor,建树的时候 ...
- 【bzoj3261】最大异或和 可持久化Trie树
题目描述 给定一个非负整数序列 {a},初始长度为 N. 有M个操作,有以下两种操作类型:1.A x:添加操作,表示在序列末尾添加一个数 x,序列的长度 N+1.2.Q l r x:询问操 ...
- BZOJ 3261: 最大异或和( 可持久化trie )
搞成前缀和然后就可以很方便地用可持久化trie维护了.时间复杂度O((N+M)*25) -------------------------------------------------------- ...
- [十二省联考2019]异或粽子——可持久化trie树+堆
题目链接: [十二省联考2019]异或粽子 求前$k$大异或区间,可以发现$k$比较小,我们考虑找出每个区间. 为了快速得到一个区间的异或和,将原序列做前缀异或和. 对于每个点作为右端点时,我们维护出 ...
- 洛谷.5283.[十二省联考2019]异或粽子(可持久化Trie 堆)
LOJ 洛谷 考场上都拍上了,8:50才发现我读错了题=-= 两天都读错题...醉惹... \(Solution1\) 先求一遍前缀异或和. 假设左端点是\(i\),那么我们要在\([i,n]\)中找 ...
- 【xsy1147】 异或(xor) 可持久化trie
我的脑回路可能比较奇怪. 我们对这些询问离线,将所得序列${a}$的后缀和建$n$棵可持久化$trie$. 对于一组询问$(l,r,x)$,我们在主席树上询问第$l$棵树$-$第r$+1$棵树中与$s ...
- [BZOJ4103][Thu Summer Camp 2015]异或运算 可持久化Trie树
4103: [Thu Summer Camp 2015]异或运算 Time Limit: 20 Sec Memory Limit: 512 MB Description 给定长度为n的数列X={x1 ...
- 【bzoj3689】异或之 可持久化Trie树+堆
题目描述 给定n个非负整数A[1], A[2], ……, A[n].对于每对(i, j)满足1 <= i < j <= n,得到一个新的数A[i] xor A[j],这样共有n*(n ...
- BZOJ 3261 最大异或和 可持久化Trie树
题目大意:给定一个序列,提供下列操作: 1.在数组结尾插入一个数 2.给定l,r,x,求一个l<=p<=r,使x^a[p]^a[p+1]^...^a[n]最大 首先我们能够维护前缀和 然后 ...
随机推荐
- Sample Classification Code of CIFAR-10 in Torch
Sample Classification Code of CIFAR-10 in Torch from: http://torch.ch/blog/2015/07/30/cifar.html req ...
- HDU 5583 Kingdom of Black and White(暴力)
http://acm.hdu.edu.cn/showproblem.php?pid=5583 题意: 给出一个01串,现在对这串进行分组,连续相同的就分为一组,如果该组内有x个数,那么就对答案贡献x* ...
- python 安装插件 requests、BeautifulSoup
安装第三方插件库 1. requests , 下载地址 https://github.com/requests/requests 安装: 利用 pip 安装 pip3 install request ...
- hdu 6134 Battlestation Operational 莫比乌斯反演
Battlestation Operational Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- MAC office2016 安装及激活
Office 下载地址: http://www.xitongtiandi.net/soft_yy/4285.html 破解补丁下载地址: https://bbs.feng.com/tencentdow ...
- str_replace 批量查找替换字符串
<?php $str = 'I Love You!'; $str = str_replace('o','O',$str,$count); echo $str.PHP_EOL; // I LOve ...
- maven阿里云镜像及本地仓库
一.添加阿里云镜像 1 找到maven的安装目录,conf文件夹下的setting.xml文件 2 打开setting.xml文件,找到mirrors节点添加阿里镜像库地址: <mirror&g ...
- [原][粒子特效][spark]粒子系统system、主节点group、渲染器render
深入浅出spark粒子特效连接:https://www.cnblogs.com/lyggqm/p/9956344.html system: A class defining a complete sy ...
- Codeforces 36B - Fractal
36B - Fractal 思路:分形 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #def ...
- 类似于placehoder效果的图标展示
在做app开发的时候往往会有那个注册登录啊,什么的页面,里面就会包含这那种类似于placeholder的效果的图标,当时我也是和ios和安卓混合开发一款app里面的页面全是我写,最开始就是登陆啊,注册 ...