题面

Bzoj

洛谷

题解

显然,如果让你查询整个数列的最大异或和,建一颗$01Trie$,每给定一个$p$,按照二进制后反方向跳就行了(比如当前二进制位为$1$,则往$0$跳,反之亦反)。

但是现在要支持在最末尾插入和区间查询,将这颗$Trie$可持久化一下就好了(可持久化$Trie$敲板)

#include <cstdio>
#include <cstring>
#include <algorithm>
using std::min; using std::max;
using std::sort; using std::swap;
typedef long long ll; template<typename T>
void read(T &x) {
int flag = 1; x = 0; char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') flag = -flag; ch = getchar(); }
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= flag;
} const int N = 6e5 + 10;
int trie[N * 24][2], latest[N * 24];
int s[N], rt[N], n, m, tot; void insert(int i, int k, int p, int q) { // q -> p s[i][k]
if(k < 0) { latest[q] = i; return ; }
int c = (s[i] >> k) & 1;
if(p) trie[q][c ^ 1] = trie[p][c ^ 1];
trie[q][c] = ++tot;
insert(i, k - 1, trie[p][c], trie[q][c]);
latest[q] = max(latest[trie[q][0]], latest[trie[q][1]]);
} int query(int now, int val, int k, int limit) {
if(k < 0) return s[latest[now]] ^ val;
int c = (val >> k) & 1;
if(latest[trie[now][c ^ 1]] >= limit)
return query(trie[now][c ^ 1], val, k - 1, limit);
else return query(trie[now][c], val , k - 1, limit);
} int main () {
read(n), read(m);
latest[0] = -1, rt[0] = ++tot;
insert(0, 23, 0, rt[0]);
for(int i = 1; i <= n; ++i) {
int x; read(x);
s[i] = s[i - 1] ^ x, rt[i] = ++tot;
insert(i, 23, rt[i - 1], rt[i]);
}
for(int i = 1; i <= m; ++i) {
char opt[3]; scanf("%s", opt);
if(opt[0] == 'A') {
int x; read(x);
rt[++n] = ++tot, s[n] = s[n - 1] ^ x;
insert(n, 23, rt[n - 1], rt[n]);
} else {
int l, r, x; read(l), read(r), read(x);
printf("%d\n", query(rt[r - 1], x ^ s[n], 23, l - 1));
}
}
return 0;
}

Bzoj3261/洛谷P4735 最大异或和(可持久化Trie)的更多相关文章

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

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

  2. 洛谷 P4735 最大异或和 解题报告

    P4735 最大异或和 题目描述 给定一个非负整数序列\(\{a\}\),初始长度为\(N\). 有\(M\)个操作,有以下两种操作类型: A x:添加操作,表示在序列末尾添加一个数\(x\),序列的 ...

  3. 【题解】洛谷P4735最大异或和

    学习了一下可持久化trie的有关姿势~其实还挺好理解的,代码也短小精悍.重点在于查询某个历史版本的trie树上的某条边是否存在,同样我们转化到维护前缀和来实现.同可持久化线段树一样,我们为了节省空间继 ...

  4. [洛谷P4735]最大异或和

    题目大意:有一串初始长度为$n$的序列$a$,有两种操作: $A\;x:$在序列末尾加一个数$x$ $Q\;l\;r\;x:$找一个位置$p$,满足$l\leqslant p\leqslant r$, ...

  5. 【洛谷P4735】最大异或和

    题目大意:给定一个长度为 N 的序列,支持两个操作:在序列末尾添加一个新的数字,查询序列区间 \([l,r]\) 内使得 \(a_p\oplus a_{q+1}\oplus ... a_N\oplus ...

  6. 【洛谷 P4735】 最大异或和 (可持久化Trie)

    题目链接 维护整个数列的异或前缀和和\(s\),然后每次就是要求\(s[N]\text{^}x\text{^}s[k],l-1<=k<=r-1\)的最大值 如果没有\(l\)的限制,那么直 ...

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

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

  8. 洛谷 P3359 改造异或树

    题目描述 给定一棵n 个点的树,每条边上都有一个权值.现在按顺序删掉所有的n-1条边,每删掉一条边询问当前有多少条路径满足路径上所有边权值异或和为0. 输入输出格式 输入格式: 第一行一个整数n. 接 ...

  9. [洛谷P4592][TJOI2018]异或

    题目大意:有一棵$n$个点的树,第$i$个点权值为$w_i$,有两种操作: $1\;x\;y:$询问节点$x$的子树中与$y$异或结果的最大值 $2\;x\;y\;z:$询问路径$x$到$y$上点与$ ...

随机推荐

  1. 解决VSCode终端中文乱码问题

    VSCode终端其实调用的是cmd.exe,所以当这里出现中文乱码的时候要解决的是cmd的编码设置问题. 可以通过chcp命令查看cmd的编码设置,GBK2312的代码页编号是936,然后改成utf- ...

  2. Codeforces 221 D. Little Elephant and Array

    D. Little Elephant and Array time limit per test 4 seconds memory limit per test 256 megabytes input ...

  3. 2017 ACM Arabella Collegiate Programming Contest(solved 11/13)

    省选考前单挑做点ACM练练细节还是很不错的嘛- 福利:http://codeforces.com/gym/101350 先来放上惨不忍睹的virtual participate成绩(中间跑去食堂吃饭于 ...

  4. new Date('2014/04/30') 和 new Date('2014-04-30') 的区别

    new Date('2014/04/30') Wed Apr 30 2014 00:00:00 GMT+0800 (中国标准时间) new Date('2014-04-30'); Wed Apr 30 ...

  5. CodeForces 869B

    Even if the world is full of counterfeits, I still regard it as wonderful. Pile up herbs and incense ...

  6. h5 canvas动画,不知道谁写的

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  7. 数据类型的判断 --Object.prototype.toString.call(obj)精准检测对象类型

    数据类型的判断 typeof typeof返回一个表示数据类型的字符串,返回结果包括:number.boolean.string.symbol.object.undefined.function等7种 ...

  8. 对Feign的请求url 重写

    需求:对当前请求的 url 重新构建 debug feign 的执行可知,重写 LoadBalancerFeignClient 类中的 execute 方法即可控制当前请求的url 代码分析 当引入  ...

  9. qq上传文件进行测试要点分析

    功能 QQ 兼容性 1.Win系统/Mac系统  Android/IOS 品牌 传 1.上传方式:直接拖拽,按回车键上传 2.多个文件同时上传给一人/多人(考虑稳定性,是否存在内存泄露) 3.不是好友 ...

  10. python设计模式之装饰器详解(三)

    python的装饰器使用是python语言一个非常重要的部分,装饰器是程序设计模式中装饰模式的具体化,python提供了特殊的语法糖可以非常方便的实现装饰模式. 系列文章 python设计模式之单例模 ...