51Nod XOR key —— 区间最大异或值 可持久化字典树
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1295
第1行:2个数N, Q中间用空格分隔,分别表示数组的长度及查询的数量(1 <= N <= 50000, 1 <= Q <= 50000)。
第2 - N+1行:每行1个数,对应数组A的元素(0 <= A[i] <= 10^9)。
第N+2 - N+Q+1行:每行3个数X, L, R,中间用空格分隔。(0 <= X <= 10^9,0 <= L <= R < N)
输出共Q行,对应数组A的区间[L,R]中的数与X进行异或运算,所能得到的最大值。
15 8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
10 5 9
1023 6 6
33 4 7
182 4 9
181 0 12
5 9 14
99 7 8
33 9 13
13
1016
41
191
191
15
107
47
题解:
1.此题(HDU4825 Xor Sum)的加强版
2.由于要求的是x与区间[l,r]的某个数异或值最大,所以在Trie树的基础上,可以模仿可持久化线段树,建立一棵可持久化Trie树。这样就可以得到每插入一个数时的历史版本的Trie树。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e5+; struct Trie
{
int end[*MAXN], next[*MAXN][];
int root[MAXN], L = ;
int newnode()
{
next[L][] = next[L][] = -;
end[L++] = ;
return L-;
}
void init()
{
L = ;
}
void build(LL val) //初始化版本
{
int tmp = root[];
for(int i = ; i>=; i--)
{
int way = (val>>i)&;
if(next[tmp][way]==-) next[tmp][way] = newnode();
tmp = next[tmp][way];
}
}
int insert(int preroot, LL val)
{
int newroot = newnode(), rootbuf = newroot;
for(int i = ; i>=; i--)
{
int way = (val>>i)&;
next[newroot][way] = newnode(); //要走的路,新开出来
next[newroot][!way] = next[preroot][!way]; //另一条路,指向上一个历史版本的 newroot = next[newroot][way];
preroot = next[preroot][way];
end[newroot] = end[preroot]+; //叠加
}
return rootbuf;
}
LL query(int Lroot, int Rroot, LL val)
{
LL ret = ;
for(int i = ; i>=; i--)
{
ret <<= ;
int way = (val>>i)&;
if(next[Lroot][!way]!=- && (end[next[Rroot][!way]]-end[next[Lroot][!way]]>))
{
ret ^= ;
Lroot = next[Lroot][!way];
Rroot = next[Rroot][!way];
}
else
{
Lroot = next[Lroot][way];
Rroot = next[Rroot][way];
}
}
return ret;
}
};
Trie T; LL a[MAXN];
int main()
{
int n, q;
while(scanf("%d%d",&n,&q)!=EOF)
{
T.init();
T.root[] = T.newnode();
for(int i = ; i<=n; i++) //建立初始化版本
{
scanf("%lld",&a[i]);
T.build(a[i]);
}
for(int i = ; i<=n; i++) //每插入一个数,就生成一个历史版本的Trie树
T.root[i] = T.insert(T.root[i-],a[i]); while(q--)
{
int x, l, r;
scanf("%d%d%d",&x,&l,&r);
l++; r++;
printf("%lld\n", T.query(T.root[l-],T.root[r],1LL*x));
}
}
}
51Nod XOR key —— 区间最大异或值 可持久化字典树的更多相关文章
- bzoj 3261 最大异或和 可持久化字典树(01树)
题目传送门 思路: 由异或的性质可得,题目要求的式子可以转化成求$max(pre[n]^x^pre[i])$,$pre[i]$表示前缀异或和,那么我们现在就要求出这个东西,所以用可持久化字典树来求,每 ...
- AcWing 144. 最长异或值路径 01字典树打卡
给定一个树,树上的边都具有权值. 树中一条路径的异或长度被定义为路径上所有边的权值的异或和: ⊕ 为异或符号. 给定上述的具有n个节点的树,你能找到异或长度最大的路径吗? 输入格式 第一行包含整数n, ...
- 51nod 1295 XOR key-区间异或最大值-可持久化01Trie树(模板)
1295 XOR key 2 秒 262,144 KB 160 分 6 级题 给出一个长度为N的正整数数组A,再给出Q个查询,每个查询包括3个数,L, R, X (L <= R).求A[L] ...
- SPOJ MAXOR (分块 || 可持久化字典树 || 异或)(好题)
You are given a sequence A[1], A[2], ..., A[N]. (0 ≤ A[i] < 231, 1 ≤ N ≤ 12000). A query is defin ...
- 【BZOJ 3261】最大异或和【可持久化字典树】
题意 给出一个长度为n的整数序列,给出m个操作.操作有两种.1,Ax表示在序列结尾增加x.2,Qlrx表示找到一个位置p满足 l<=p<=r,使得a[p] xor a[p+1]xor... ...
- Codeforces 979 D. Kuro and GCD and XOR and SUM(异或和,01字典树)
Codeforces 979 D. Kuro and GCD and XOR and SUM 题目大意:有两种操作:①给一个数v,加入数组a中②给出三个数x,k,s:从当前数组a中找出一个数u满足 u ...
- 【bzoj3689】异或之 可持久化Trie树+堆
题目描述 给定n个非负整数A[1], A[2], ……, A[n].对于每对(i, j)满足1 <= i < j <= n,得到一个新的数A[i] xor A[j],这样共有n*(n ...
- [十二省联考2019]异或粽子——可持久化trie树+堆
题目链接: [十二省联考2019]异或粽子 求前$k$大异或区间,可以发现$k$比较小,我们考虑找出每个区间. 为了快速得到一个区间的异或和,将原序列做前缀异或和. 对于每个点作为右端点时,我们维护出 ...
- BZOJ3261: 最大异或和(可持久化trie树)
题意 题目链接 Sol 设\(sum[i]\)表示\(1 - i\)的异或和 首先把每个询问的\(x \oplus sum[n]\)就变成了询问前缀最大值 可持久化Trie树维护前缀xor,建树的时候 ...
随机推荐
- fastjson的常用用法以及自定义排序
fastJson的四种常用方法 JSON 转 POJO public static <T> T getObject(String pojo, Class<T> tclass) ...
- 学习已经被淘汰的flash
一.基本知识介绍 网站动画的分类:二维动画和三维动画 二维动画分类: 1.GIF动画 2.flash动画 flash软件:是矢量软件 选中带有点,并且可以任意变形的对象,叫形状 逐帧动画:在时 ...
- PHP增加$_ENV变量
在日常开发过程中,我们常常会将一些系统或模块配写在配置文件里.这样便于程序维护与修改.通常的配置文件有.ini , .xml等.配置文件的好处在于:1,便于管理.2,可读性高.但是,使用配置文件也会有 ...
- LinkedList 基本示例及源码解析
目录 一.JavaDoc 简介 二.LinkedList 继承接口和实现类介绍 三.LinkedList 基本方法介绍 四.LinkedList 基本方法使用 五.LinkedList 内部结构以及基 ...
- HDFS源码分析EditLog之获取编辑日志输入流
在<HDFS源码分析之EditLogTailer>一文中,我们详细了解了编辑日志跟踪器EditLogTailer的实现,介绍了其内部编辑日志追踪线程EditLogTailerThread的 ...
- Jquery EasyUI弹出窗体
$("#btnCreate").click(function () { $("#modalwindow").html("<iframe widt ...
- WPF之DataGrid篇:DataGridComboBoxColumn
准备数据源 1 准备数据源.基类为Student,数据对象为Student3,数据集为StuList3. END 编辑DataGrid显示列 1 若要填充下拉列表,请首先使用下列选项之一设置 ...
- mongo 介绍
[介绍]:MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.在高负载的情况下,添加更多的节点,可以保证服务器性能.MongoDB 旨在为WEB应用提供可扩展的高性能数据存 ...
- Docker基础原理
前言 Docker是一个开源的软件项目,让用户程序部署在一个相对隔离的环境运行,借此在Linux操作系统上提供一层额外的抽象,以及操作系统层虚拟化的自动管理机制.需要额外指出的是,Docker并不等于 ...
- myql 5.6 安装
环境: centos 6.5 192.168.9.28 4核4G 虚拟机 一. 安装编译源码所需要的工具和库 [root@localhost ~]# yum -y install gcc gcc- ...