1295 XOR key
题目来源: HackerRank
基准时间限制:1.5 秒 空间限制:262144 KB 分值: 160 难度:6级算法题

给出一个长度为N的正整数数组A,再给出Q个查询,每个查询包括3个数,L, R, X (L <= R)。求A[L] 至 A[R] 这R - L + 1个数中,与X 进行异或运算(Xor),得到的最大值是多少?
Input
第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)
Output
输出共Q行,对应数组A的区间[L,R]中的数与X进行异或运算,所能得到的最大值。
Input示例
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
Output示例
13  
1016  
41  
191  
191  
15  
107  
47
/*
51nod1295 XOR key(可持久化trie) problem:
求[a[l],a[r]]中的数与x异或所能得到的最大值. solve:
要求最大的异或值,通常是从高位到低位进行匹配.
但是要的是区间能得到的最大值,可以用类似于主席树的方法. T[i]如果是添加就在T[i-1]基础上新建节点
否则继承T[i-1]的节点.从而得到[1,i]所有情况的Tire树.
然后利用区间相减进行计算. hhh-2016/09/05-15:23:44
*/
#pragma comment(linker,"/STACK:124000000,124000000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <math.h>
#include <queue>
#include <set>
#include <map>
#define lson i<<1
#define rson i<<1|1
#define ll long long
#define clr(a,b) memset(a,b,sizeof(a))
#define scanfi(a) scanf("%d",&a)
#define scanfs(a) scanf("%s",a)
#define scanfl(a) scanf("%I64d",&a)
#define scanfd(a) scanf("%lf",&a)
#define key_val ch[ch[root][1]][0]
#define eps 1e-7
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
const ll mod = 1000000007;
const int maxn = 50010;
const double PI = acos(-1.0);
const int limit = 33;
int bin[65];
int tot;
int son[maxn*limit*2][2],val[maxn*limit*30];
int T[maxn]; void init()
{
tot = 0;
memset(son,-1,sizeof(son));
memset(val,0,sizeof(val));
} int Insert(int root,int cur)
{
if(cur<0)return -1;
int t = bin[cur-1];
int rt =++tot;
val[rt] = val[root] + 1;
son[rt][t^1] = son[root][t^1]; //不需更新的点
son[rt][t] = Insert(son[root][t],cur-1);
return rt;
} int cal(int root1,int root2,int cur)
{
if(cur < 0)
return 0;
int t = bin[cur-1];
if(val[son[root2][t]] - val[son[root1][t]] > 0)
return cal(son[root1][t],son[root2][t],cur-1) + (1 << (cur-1));
return cal(son[root1][t^1],son[root2][t^1],cur-1);
} int main()
{
int n,q;
int x,l,r;
while(scanfi(n) != EOF)
{
init();
scanfi(q);
int x;
for(int i = 1; i <= n; i++)
{
scanfi(x);
for(int i = 0; i <= limit; i++)
{
bin[i] = x % 2;
x /= 2;
}
T[i] = Insert(T[i-1],limit);
}
for(int i = 1;i <= q;i++)
{
scanfi(x),scanfi(l),scanfi(r);
for(int i = 0; i <= limit; i++)
{
bin[i] = 1-x % 2;
x /= 2;
}
printf("%d\n",cal(T[l],T[r+1],limit));
}
}
return 0;
}

  

51nod1295 XOR key(可持久化trie)的更多相关文章

  1. [多校联考2019(Round 4 T1)][51nod 1295]Xor key(可持久化trie)

    [51nod 1295]Xor key(可持久化trie) 题面 给出一个长度为n的正整数数组A,再给出Q个查询,每个查询包括3个数,L, R, X (L <= R).求A[L] 至 A[R] ...

  2. 51nod 1295 XOR key (可持久化Trie树)

    1295 XOR key  题目来源: HackerRank 基准时间限制:1.5 秒 空间限制:262144 KB 分值: 160 难度:6级算法题   给出一个长度为N的正整数数组A,再给出Q个查 ...

  3. 51nod 1295 XOR key | 可持久化Trie树

    51nod 1295 XOR key 这也是很久以前就想做的一道板子题了--学了一点可持久化之后我终于会做这道题了! 给出一个长度为N的正整数数组A,再给出Q个查询,每个查询包括3个数,L, R, X ...

  4. 51Nod--1295 XOR key (可持久化tire树)

    题目链接 1295 XOR key 可持久化tire树模版题 数组一定要开够 不然数组不够的话就容易tle 吃了两次亏 #include<bits/stdc++.h> using name ...

  5. 51nod1295 XOR key

    第一次写可持久化trie指针版我... //Null 的正确姿势终于学会啦qaq... #include<cstdio> #include<cstring> #include& ...

  6. BZOJ5338 [TJOI2018] Xor 【可持久化Trie树】【dfs序】

    题目分析: 很无聊的一道题目.首先区间内单点对应异或值的询问容易想到trie树.由于题目在树上进行,case1将路径分成两段,然后dfs的时候顺便可持久化trie树做询问.case2维护dfs序,对d ...

  7. 51nod 1295 XOR key 可持久化01字典树

    题意 给出一个长度为\(n\)的正整数数组\(a\),再给出\(q\)个询问,每次询问给出3个数,\(L,R,X(L<=R)\).求\(a[L]\)至\(a[R]\)这\(R-L+1\)个数中, ...

  8. 51Nod - 1295:XOR key (可持久化Trie求区间最大异或)

    给出一个长度为N的正整数数组A,再给出Q个查询,每个查询包括3个数,L, R, X (L <= R).求ALL 至 ARR 这R - L + 1个数中,与X 进行异或运算(Xor),得到的最大值 ...

  9. 51Nod XOR key —— 区间最大异或值 可持久化字典树

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1295 1295 XOR key  题目来源: HackerRa ...

随机推荐

  1. python实现朴素贝叶斯

    参考:<机器学习实战>- Machine Learning in Action 一. 基本思想  简单的说,用概率的高低来决定数据属于哪一类别,这就是贝叶斯决策理论的核心思想,即选择具有最 ...

  2. IOS UITextView自适应高度

    LOFTER app需要实现了一个类似iPhone短信输入框的功能,它的功能其实蛮简单,就是:[UITextView的高度随着内容高度的变化而变化].实现思路应该是: 在UITextView的text ...

  3. vue 手机端开发 小商铺 添加购物车 以及结算 功能

    这个功能绕了我一天!!!            对 就是这个功能  一系列相关联的  四处相关联 现在加班 没时间更 过两天在更

  4. Linux入门(1)_VMware和系统分区和系统安装和远程登陆管理

    1 VMware的安装和使用 注意有 快照 和 克隆 的功能. 快照相当于建立一个 系统还原点, 可以随时恢复到原来状态. 克隆功能可以复制一个和当前一样的系统,并可以选择链接安装,只使用很少的空间就 ...

  5. 剑指offer-数组中出现次数超过一半的数字

    题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...

  6. 链家2018春招Java工程师编程题题解

    Light 题目描述 在小红家里面,有n组开关,触摸每个开关,可以使得一组灯泡点亮.现在问你,使用这n组开关,最多能够使得多少个灯泡点亮呢? 输入 第一行一个n,表示有n组开关.接下来n行,每行第一个 ...

  7. linux下执行java类(运行java定时器)

    假如有一个定时器TimerTest.java import java.io.IOException; import java.util.Timer; public class TimerTest { ...

  8. 新概念英语(1-95)Tickets,please!

    Lesson 95 Tickets, please. 请把车票拿出来. Listen to the tape then answer this question. Why did George and ...

  9. 新概念英语(1-13)A new dress

    What colour is Anna's hat? A:What colour is your new dress? B:It's green.Come upstairs and see it. A ...

  10. Spring Security入门(1-9)Spring Security 的xml 命名空间配置