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,建树的时候 ...
随机推荐
- yii2操作数据库 mysql 读写分离 主从复制
转载地址:http://www.kuitao8.com/20150115/3471.shtml 开始使用数据库首先需要配置数据库连接组件,通过添加 db 组件到应用配置实现("基础的&quo ...
- 2016.6.21 将Eclipse中项目部署到tomcat下
新建的web项目,各种都配置好,选择run on server之后,发现运行失败,并不能访问需要的网址.而脱离eclipse,将生成的war文件直接放到tomcat的webapp下时,可以正常访问.所 ...
- hdu5417(BC)
题目链接:点这儿 Victor and Machine Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K ( ...
- poj1649 Rescue(BFS+优先队列)
Rescue Time Limit: 2 Seconds Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...
- 【Excle数据透视表】如何重命名数据透视表
如下图,是新生成的一个数据透视简表,现在需要将其数据透视表的名称修改为:汇总数据 解决办法 修改后的效果如下:
- FileOutPutStream 的写操作
package xinhuiji_day07; import java.io.File;import java.io.FileNotFoundException;import java.io.File ...
- Spring配置错误记录
很多其它Spring问题因为发生时未记录而遗忘了~~~~~~~ 如今动动手 解决方式因为不是源头分析因而仅供參考.! ! 严重: Exception sending context destroyed ...
- Hive报错:Failed with exception Unable to rename
之前也安装过hive,操作过无数,也没发现什么错误,今天因为之前安装的hadoop不能用了,不知道为什么,老是提示node 0,所以重新安装了hadoop和hive.安装完测试hive创建表也没发现什 ...
- Jquery EasyUI弹出窗体
$("#btnCreate").click(function () { $("#modalwindow").html("<iframe widt ...
- Ubuntu/CentOS下编译Nginx最基本参数
Ubuntu/CentOS下编译Nginx安装基本参数,做个记录: groupadd www useradd -g www www ./configure --user=www --group=www ...