http://codeforces.com/contest/665/problem/E

给定一个序列,问其中有多少个区间,所有数字异或起来 >= k

看到异或,就应该想到异或的性质,A^B^B = A,所以,用sum[i]表示1--i的异或值,那么i...j的异或值就是sum[j] ^ sum[i - 1]

所以原问题转化为在一个数组中,任选两个数,异或值 >= k

首先所有数字 <= 1e9

那么前缀异或值 <= 2^31 - 1

那么用int就够了,从30位开始插入字典树,(1 << 31)是溢出的

查找的时候,分两类

1、当前的位置中k的二进制位是0,就是第15位,如果k的二进制位是0,那么往反方向走字典树的子树总是可以得,这个时候只需要加上去子树大小即可。

2、如果那一位k是1,我们没法马上判断贡献,那么只能往反方向贪心走字典树。

注意的就是叶子节点有可能没走到,需要最后加上来。

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. using namespace std;
  7. #define inf (0x3f3f3f3f)
  8. typedef long long int LL;
  9.  
  10. #include <iostream>
  11. #include <sstream>
  12. #include <vector>
  13. #include <set>
  14. #include <map>
  15. #include <queue>
  16. #include <string>
  17. const int maxn = 1e6 + ;
  18. const int N = ;
  19. int a[maxn];
  20. int sum[maxn];
  21. int n, k;
  22. struct node {
  23. struct node *pNext[N];
  24. int cnt;
  25. }tree[maxn * ];
  26. int t;
  27. struct node * create() {
  28. struct node *p = &tree[t++];
  29. for (int i = ; i < N; ++i) {
  30. p->pNext[i] = ;
  31. }
  32. p->cnt = ;
  33. return p;
  34. }
  35. void insert(struct node **T, int val) {
  36. struct node *p = *T;
  37. if (p == NULL) {
  38. p = *T = create();
  39. }
  40. for (int i = ; i >= ; --i) {
  41. int id = (val & ( << i)) > ;
  42. if (p->pNext[id] == NULL) {
  43. p->pNext[id] = create();
  44. }
  45. p = p->pNext[id];
  46. p->cnt++;
  47. }
  48. return ;
  49. }
  50. LL find(struct node *T, int val) {
  51. struct node *p = T;
  52. LL ans = ;
  53. if (p == NULL) return ans;
  54. for (int i = ; i >= ; --i) {
  55. int id = (val & ( << i)) > ;
  56. int id_k = (k & ( << i)) > ;
  57. if (id_k == ) {
  58. if (p->pNext[!id] != NULL)
  59. ans += p->pNext[!id]->cnt; //这边的树全部都可以
  60. if (p->pNext[id] == NULL) return ans;
  61. p = p->pNext[id]; //如果走到了叶子节点,我这里return了,不会重复增加
  62. } else { //id_k = 1
  63. if (p->pNext[!id] == NULL) return ans;
  64. p = p->pNext[!id];
  65. }
  66. }
  67. return ans + p->cnt; //叶子节点有可能没算到,参考样例1
  68. }
  69. void work() {
  70. scanf("%d%d", &n, &k);
  71. for (int i = ; i <= n; ++i) {
  72. scanf("%d", &a[i]);
  73. sum[i] = sum[i - ] ^ a[i];
  74. // cout << sum[i] << " ";
  75. }
  76. // printf("%d\n", 1 << 30);
  77. struct node *T = NULL;
  78.  
  79. insert(&T, sum[]);
  80. LL ans = ;
  81. for (int i = ; i <= n; ++i) {
  82. ans += find(T, sum[i]);
  83. insert(&T, sum[i]);
  84. // cout << find(T, sum[i]) << endl;
  85. }
  86. for (int i = ; i <= n; ++i) ans += sum[i] >= k;
  87. cout << ans << endl;
  88. return;
  89. }
  90. int main() {
  91. #ifdef local
  92. freopen("data.txt","r",stdin);
  93. #endif
  94. work();
  95. return ;
  96. }

E. Beautiful Subarrays 字典树的更多相关文章

  1. Educational Codeforces Round 12 E. Beautiful Subarrays 字典树

    E. Beautiful Subarrays 题目连接: http://www.codeforces.com/contest/665/problem/E Description One day, ZS ...

  2. Codeforces 665E. Beautiful Subarrays (字典树)

    题目链接:http://codeforces.com/problemset/problem/665/E (http://www.fjutacm.com/Problem.jsp?pid=2255) 题意 ...

  3. codeforces 665E E. Beautiful Subarrays(trie树)

    题目链接: E. Beautiful Subarrays time limit per test 3 seconds memory limit per test 512 megabytes input ...

  4. Beautiful Subarrays

    Beautiful Subarrays time limit per test 3 seconds memory limit per test 512 megabytes input standard ...

  5. 【Codeforces】665E Beautiful Subarrays

    E. Beautiful Subarrays time limit per test: 3 seconds memory limit per test: 512 megabytes input: st ...

  6. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  7. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  8. 字典树+博弈 CF 455B A Lot of Games(接龙游戏)

    题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...

  9. 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)

    萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...

随机推荐

  1. Asp.net工作流workflow实战之书签(二)

    1.winform(web程序)下使用工作流 怎样才能像控制台那样让winform或web页面窗体阻塞等待工作流的继续执行呢 2.BookMark书签 书签:和一般的书签看书的时候方便查看上次看的内容 ...

  2. 百度地图设置div样式宽高为百分比不显示地图

    如题,不显示地图只要在样式代码里面加以 position:absolute; 代码就可以了 <style type="text/css"> body, html,#al ...

  3. MySQL on Azure高可用性设计 DRBD - Corosync - Pacemaker - CRM (二)

    在上一篇文章中描述了MySQL HA on Azured 设计思路,本篇文章中将描述具体的部署,每个组件的安装和配置. 整体的设计架构如下: 下面将是所有组件的安装配置过程,所有的虚拟机是CentOS ...

  4. Python图片识别——人工智能篇

     一.安装pytesseract和PIL PIL全称:Python Imaging Library,python图像处理库,这个库支持多种文件格式,并提供了强大的图像处理和图形处理能力. 由于PIL仅 ...

  5. C#设计模式(8)——桥接模式

    一.概念 桥接模式即将抽象部分与实现部分脱耦,使它们可以独立变化. 二.模型 三.代码实现 // 客户端调用 // 类似Web应用程序 class Client { static void Main( ...

  6. Project Server 2016 RestAPI调用测试

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  7. 问题:webservice浏览后 无法输入参数;结果:调试Web Service时不能输入参数的解决办法

    使用.NET 开发Web Service,有一个很方便的功能就是可以通过IE直接测试Web Service.当你的Web Service的参数都是元数据类型,那么只要你使用IE浏览Web Servic ...

  8. <正则吃饺子> :关于使用pd创建表时需要注意的地方

    公司项目使用pd设计数据库表.之前用过,但是年代比较久远了,有些细节忘记了,今天重新使用时候,生疏了,现在稍微记录下吧. 1.pd创建表的使用,可以直接从网上搜索,博文比较多,如 “pd 设计数据库表 ...

  9. nmap 快速扫描所有端口

    nmap -sT -sV -Pn -v xxx.xxx.xxx.xxx nmap -sS -p 1-65535 -v 192.168.1.254参数:-sS    TCP SYN扫描    nmap ...

  10. C++使用RabbitMQ类库做客户端与RabbitMQ Server通讯,生成C++可调用的rabbimq.*.dll的过程

    Step: download the latest rabbitmq-c via: https://github.com/alanxz/rabbitmq-c follow the document, ...