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. 1行:2个数N, Q中间用空格分隔,分别表示数组的长度及查询的数量(1 <= N <= 50000, 1 <= Q <= 50000)。
  2. 2 - N+1行:每行1个数,对应数组A的元素(0 <= A[i] <= 10^9)。
  3. N+2 - N+Q+1行:每行3个数X, L, R,中间用空格分隔。(0 <= X <= 10^90 <= L <= R < N)
Output
  1. 输出共Q行,对应数组A的区间[L,R]中的数与X进行异或运算,所能得到的最大值。
Input示例
  1. 15 8  
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6
  8. 7
  9. 8
  10. 9
  11. 10
  12. 11
  13. 12
  14. 13
  15. 14
  16. 15
  17. 10 5 9
  18. 1023 6 6
  19. 33 4 7
  20. 182 4 9
  21. 181 0 12
  22. 5 9 14
  23. 99 7 8
  24. 33 9 13
Output示例
  1. 13  
  2. 1016  
  3. 41  
  4. 191  
  5. 191  
  6. 15  
  7. 107  
  8. 47
  1. /*
  2. 51nod1295 XOR key(可持久化trie)
  3.  
  4. problem:
  5. 求[a[l],a[r]]中的数与x异或所能得到的最大值.
  6.  
  7. solve:
  8. 要求最大的异或值,通常是从高位到低位进行匹配.
  9. 但是要的是区间能得到的最大值,可以用类似于主席树的方法. T[i]如果是添加就在T[i-1]基础上新建节点
  10. 否则继承T[i-1]的节点.从而得到[1,i]所有情况的Tire树.
  11. 然后利用区间相减进行计算.
  12.  
  13. hhh-2016/09/05-15:23:44
  14. */
  15. #pragma comment(linker,"/STACK:124000000,124000000")
  16. #include <algorithm>
  17. #include <iostream>
  18. #include <cstdlib>
  19. #include <cstdio>
  20. #include <cstring>
  21. #include <vector>
  22. #include <math.h>
  23. #include <queue>
  24. #include <set>
  25. #include <map>
  26. #define lson i<<1
  27. #define rson i<<1|1
  28. #define ll long long
  29. #define clr(a,b) memset(a,b,sizeof(a))
  30. #define scanfi(a) scanf("%d",&a)
  31. #define scanfs(a) scanf("%s",a)
  32. #define scanfl(a) scanf("%I64d",&a)
  33. #define scanfd(a) scanf("%lf",&a)
  34. #define key_val ch[ch[root][1]][0]
  35. #define eps 1e-7
  36. #define inf 0x3f3f3f3f3f3f3f3f
  37. using namespace std;
  38. const ll mod = 1000000007;
  39. const int maxn = 50010;
  40. const double PI = acos(-1.0);
  41. const int limit = 33;
  42. int bin[65];
  43. int tot;
  44. int son[maxn*limit*2][2],val[maxn*limit*30];
  45. int T[maxn];
  46.  
  47. void init()
  48. {
  49. tot = 0;
  50. memset(son,-1,sizeof(son));
  51. memset(val,0,sizeof(val));
  52. }
  53.  
  54. int Insert(int root,int cur)
  55. {
  56. if(cur<0)return -1;
  57. int t = bin[cur-1];
  58. int rt =++tot;
  59. val[rt] = val[root] + 1;
  60. son[rt][t^1] = son[root][t^1]; //不需更新的点
  61. son[rt][t] = Insert(son[root][t],cur-1);
  62. return rt;
  63. }
  64.  
  65. int cal(int root1,int root2,int cur)
  66. {
  67. if(cur < 0)
  68. return 0;
  69. int t = bin[cur-1];
  70. if(val[son[root2][t]] - val[son[root1][t]] > 0)
  71. return cal(son[root1][t],son[root2][t],cur-1) + (1 << (cur-1));
  72. return cal(son[root1][t^1],son[root2][t^1],cur-1);
  73. }
  74.  
  75. int main()
  76. {
  77. int n,q;
  78. int x,l,r;
  79. while(scanfi(n) != EOF)
  80. {
  81. init();
  82. scanfi(q);
  83. int x;
  84. for(int i = 1; i <= n; i++)
  85. {
  86. scanfi(x);
  87. for(int i = 0; i <= limit; i++)
  88. {
  89. bin[i] = x % 2;
  90. x /= 2;
  91. }
  92. T[i] = Insert(T[i-1],limit);
  93. }
  94. for(int i = 1;i <= q;i++)
  95. {
  96. scanfi(x),scanfi(l),scanfi(r);
  97. for(int i = 0; i <= limit; i++)
  98. {
  99. bin[i] = 1-x % 2;
  100. x /= 2;
  101. }
  102. printf("%d\n",cal(T[l],T[r+1],limit));
  103. }
  104. }
  105. return 0;
  106. }

  

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. JQuery 动态加载iframe.

    html: <iframe id="ifm" style="width:inherit;height:inherit" runat="serve ...

  2. JAVA_SE基础——12.运算符的优先级

    优先级 操作符 含义 关联性 用法 ---------------------------------------------------------------- 1 [ ] 数组下标 左 arra ...

  3. 构建自己的PHP框架--构建模版引擎(3)

    之前我们实现了最简单的echo命令的模版替换,就是将{{ $name }}这样一段内容替换成<?php echo $name ?>. 现在我们来说下其他的命令,先来回顾下之前的定义 输出变 ...

  4. LeetCode & Q35-Search Insert Position-Easy

    Array Binary Search Description: Given a sorted array and a target value, return the index if the ta ...

  5. Python内置函数(12)——str

    英文文档: class str(object='') class str(object=b'', encoding='utf-8', errors='strict') Return a string  ...

  6. java double相加

    public class DoubleUtil { private static final int DEF_DIV_SCALE = 10; /** * 相加 * * @param d1 * @par ...

  7. 操作MP3文件的元数据

    参见:http://jingyan.baidu.com/article/03b2f78c4d5eae5ea237aee7.html 一.MP3文件的元数据 一个规则的MP3文件大致含有3个部分: TA ...

  8. Linux知识积累(2)dirname的使用方法

    linux中的cd "$(dirname "$0")"/是什么意思呢? 分析如下: 1.$0 表示当前动行的命令名,一般用于shell 脚本中 2.dirnam ...

  9. 阿里云API网关(12)为员工创建子账号,实现分权管理API:使用RAM管理API

    网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...

  10. kafka---broker 保存消息

    1 .存储方式 物理上把 topic 分成一个或多个 patition(对应 server.properties 中的 num.partitions=3 配置),每个 patition 物理上对应一个 ...