E. Beautiful Subarrays 字典树
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,我们没法马上判断贡献,那么只能往反方向贪心走字典树。
注意的就是叶子节点有可能没走到,需要最后加上来。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1e6 + ;
const int N = ;
int a[maxn];
int sum[maxn];
int n, k;
struct node {
struct node *pNext[N];
int cnt;
}tree[maxn * ];
int t;
struct node * create() {
struct node *p = &tree[t++];
for (int i = ; i < N; ++i) {
p->pNext[i] = ;
}
p->cnt = ;
return p;
}
void insert(struct node **T, int val) {
struct node *p = *T;
if (p == NULL) {
p = *T = create();
}
for (int i = ; i >= ; --i) {
int id = (val & ( << i)) > ;
if (p->pNext[id] == NULL) {
p->pNext[id] = create();
}
p = p->pNext[id];
p->cnt++;
}
return ;
}
LL find(struct node *T, int val) {
struct node *p = T;
LL ans = ;
if (p == NULL) return ans;
for (int i = ; i >= ; --i) {
int id = (val & ( << i)) > ;
int id_k = (k & ( << i)) > ;
if (id_k == ) {
if (p->pNext[!id] != NULL)
ans += p->pNext[!id]->cnt; //这边的树全部都可以
if (p->pNext[id] == NULL) return ans;
p = p->pNext[id]; //如果走到了叶子节点,我这里return了,不会重复增加
} else { //id_k = 1
if (p->pNext[!id] == NULL) return ans;
p = p->pNext[!id];
}
}
return ans + p->cnt; //叶子节点有可能没算到,参考样例1
}
void work() {
scanf("%d%d", &n, &k);
for (int i = ; i <= n; ++i) {
scanf("%d", &a[i]);
sum[i] = sum[i - ] ^ a[i];
// cout << sum[i] << " ";
}
// printf("%d\n", 1 << 30);
struct node *T = NULL; insert(&T, sum[]);
LL ans = ;
for (int i = ; i <= n; ++i) {
ans += find(T, sum[i]);
insert(&T, sum[i]);
// cout << find(T, sum[i]) << endl;
}
for (int i = ; i <= n; ++i) ans += sum[i] >= k;
cout << ans << endl;
return;
}
int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
work();
return ;
}
E. Beautiful Subarrays 字典树的更多相关文章
- Educational Codeforces Round 12 E. Beautiful Subarrays 字典树
E. Beautiful Subarrays 题目连接: http://www.codeforces.com/contest/665/problem/E Description One day, ZS ...
- Codeforces 665E. Beautiful Subarrays (字典树)
题目链接:http://codeforces.com/problemset/problem/665/E (http://www.fjutacm.com/Problem.jsp?pid=2255) 题意 ...
- codeforces 665E E. Beautiful Subarrays(trie树)
题目链接: E. Beautiful Subarrays time limit per test 3 seconds memory limit per test 512 megabytes input ...
- Beautiful Subarrays
Beautiful Subarrays time limit per test 3 seconds memory limit per test 512 megabytes input standard ...
- 【Codeforces】665E Beautiful Subarrays
E. Beautiful Subarrays time limit per test: 3 seconds memory limit per test: 512 megabytes input: st ...
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 字典树+博弈 CF 455B A Lot of Games(接龙游戏)
题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...
- 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)
萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...
随机推荐
- bzoj 3730 震波 —— 动态点分治+树状数组
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3730 建点分树,每个点记两个树状数组,存它作为重心管辖的范围内,所有点到它的距离情况和到它在 ...
- Azure CLI下载Azure Storage Container内的所有文件
在某些场景下,客户需要把Azure Storage的某一个container内的内容都下载到本地.当然采用PowerShell可以定时的进行下载的动作,但有时客户的环境是Linux或MacOS,这时需 ...
- 杂项-权限管理:Spring Secutity
ylbtech-杂项-权限管理:Spring Secutity Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在S ...
- 将gridview 的数据导出EXCEL
gridview数据 单击“导出EXCEL”按钮后 1.在上面的代码中,先将gridview绑定到指定的数据源中,然后在button按钮(用来做导出到EXCEL的)的事件中,写入相关的代 ...
- (补)Java解析XML之dom4j
上次记得忘了记最流行的dom4j方法了,用的还是上次的那个XML文件 注:需要添加dom4j.jar文件 package com.xujingyang.dom4j; import org.dom4j. ...
- Ubuntu下crontab命令的用法
cron是一个Linux下的后台进程,用来定期的执行一些任务.因为我用的是Ubuntu,所以这篇文章中的所有命令也只能保证在Ubuntu下有效,但其他系统应该也差不多. 想要让cron执行你指定的任务 ...
- Hadoop的namenode和secondnamenode分开部署在不同服务器
一.系统环境: Hadoop 0.20.2.JDK 1.6.Linux操作系统 二.使用背景 网上关于Hadoop的集群配置,很多情况下,都是把namenode和secondnamenode部署在 ...
- CodeForces 492A Vanya and Cubes
A. Vanya and Cubes time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- java的大小端和转换
一直以为大小端针对的bit的顺序,今天才知道:大小端的分度值是 byte,即每一个byte都是按照正常顺序,但是byte组装成一个int 或者是 long等时每个byte的摆放位置不同. 测试代码: ...
- CSS学习系列1 - CSS中的盒子模型 box model
css中有一个盒子模型的概念. 主要是用来告诉浏览器如何来计算页面元素的宽度和高度, 比如该元素的宽度/高度 是否包括内边距,边框,外边距. 盒子模型有一个属性box-sizing属性来说明是否包括 ...