Codeforces 655E Beautiful Subarrays【01trie树】
题目链接:
http://codeforces.com/contest/665/problem/E
题意:
求异或值大于给定K的区间个数。
分析:
首先我们可以得到区间前缀的异或值。
这样我们将这个前缀M和K一起走trie树,如果该位K的值为0,那么无论怎么走最后得到的答案都不会比K小,所以直接加上另一边的子树大小,然后继续沿着当前边走。如果该位K的值为1,那么想要大于等于K必须沿着另一边贪心的走。
代码:
#include<cstdio>
#include<iostream>
using namespace std;
const int maxn = 1000000 << 5, maxm = 1e6 + 5;
int f[maxn][2];
int cnt = 1;
int son[maxn], pre[maxm];
long long ans = 0;
#define pl(n) cout<<#n<<": "<<n<<endl;
#define sa(n) scanf("%d", &n)
void insert(int n)
{
int k;
int u = 1;
for(int i = 30; i >= 0; i--){
k = (n >> i) & 1;
if(!f[u][k]) f[u][k] = ++cnt;
son[u]++;
u = f[u][k];
}
son[u]++;
}
void query(int n, int m)
{
int k1, k2, u = 1;
for(int i = 30; i >= 0; i--){
k1 = (n >> i) & 1;
k2 = (m >> i) & 1;
if(k2 == 0){
ans += son[f[u][!k1]];
u = f[u][k1];
}
else u = f[u][!k1];
if(u == 0) break;
}
ans += son[u];
}
int main (void)
{
int n,k;sa(n);sa(k);
pre[0] = 0;
int num;
insert(pre[0]);
for(int i = 1; i <= n; i++){
sa(num);
pre[i] = pre[i - 1] ^ num;
query(pre[i], k);
insert(pre[i]);
//pl(ans);
}
printf("%I64d\n", ans);
return 0;
}
Codeforces 655E Beautiful Subarrays【01trie树】的更多相关文章
- Codeforces 665E. Beautiful Subarrays (字典树)
题目链接:http://codeforces.com/problemset/problem/665/E (http://www.fjutacm.com/Problem.jsp?pid=2255) 题意 ...
- Educational Codeforces Round 12 E. Beautiful Subarrays 字典树
E. Beautiful Subarrays 题目连接: http://www.codeforces.com/contest/665/problem/E Description One day, ZS ...
- codeforces 665E E. Beautiful Subarrays(trie树)
题目链接: E. Beautiful Subarrays time limit per test 3 seconds memory limit per test 512 megabytes input ...
- E. Beautiful Subarrays 字典树
http://codeforces.com/contest/665/problem/E 给定一个序列,问其中有多少个区间,所有数字异或起来 >= k 看到异或,就应该想到异或的性质,A^B^B ...
- codeforces 665E Beautiful Subarrays
题目链接 给一个数列, 让你找出异或结果大于等于k的子序列的个数. 因为任意一段序列的异或值都可以用前缀异或和来表示, 所以我们先求出前缀异或和. 我们考虑字典树, 对于每一个前缀sum, 我们先查询 ...
- 【Codeforces】665E Beautiful Subarrays
E. Beautiful Subarrays time limit per test: 3 seconds memory limit per test: 512 megabytes input: st ...
- Educational Codeforces Round 12 E. Beautiful Subarrays trie求两异或值大于等于k对数
E. Beautiful Subarrays One day, ZS the Coder wrote down an array of integers a with elements a1, ...
- Beautiful Subarrays
Beautiful Subarrays time limit per test 3 seconds memory limit per test 512 megabytes input standard ...
- Codeforces 55D Beautiful Number
Codeforces 55D Beautiful Number a positive integer number is beautiful if and only if it is divisibl ...
随机推荐
- JavaScript -- 语法和数据类型
前戏 前面学了HTML和CSS相关的知识,那JavaScript是做什么的呢?你在网页上看到的那些炫酷的特效都是通过JS来实现的,所以,想要开发一个逼格满满的web页面,JS是必须要会的 什么是Jav ...
- House of Spirit(fastbin)
0x01 fastbin fastbin所包含chunk的大小为16 Bytes, 24 Bytes, 32 Bytes, … , 80 Bytes.当分配一块较小的内存(mem<=64 Byt ...
- vue循环出来列表里面的列表点击click事件只对当前列表有效;
<div id="app"> <div class=‘b’ v-for='item in items' @click="toggle(item)&quo ...
- CSS盒模型总结(一)
一.基本概念 盒子模型是css中一个重要的概念,理解了盒子模型才能更好的排版,盒模型的组成:content padding border margin 二.盒模型的分类 盒子模型有两种,分别是 ie ...
- OpenCV2.4.11+VS2012的环境配置+“fatal error LNK1112: 模块计算机类型“X86”与目标计算机类型“x64”冲突”的问题解决
本来OpenCV环境配置的问题是个基础问题,但是步骤有点小烦,所以几乎每次都要百度一下,加上这次遇到的“fatal error LNK1112: 模块计算机类型“X86”与目标计算机类型“x64”冲突 ...
- WINDOWS下使用Mysql 中碰到的问题记录
问题:在cmd中输入net stop mysql反馈“服务名无效” win+R打开运行窗口,输入 services.msc 查看其中mysql的服务名,比如我的是叫做MySQL80 让我们继续回到最开 ...
- react 列表渲染
https://reactjs.org/docs/lists-and-keys.html#keys 以下代码运行会报错:Warning: Each child in an array or itera ...
- node.js中常用的fs文件系统
fs文件系统模块对于系统文件及目录进行一些读写操作. 模块中的方法均有异步和同步版本,例如读取文件内容的函数有异步的 fs.readFile() 和同步的 fs.readFileSync(). 异步的 ...
- (原)剑指offer之旋转数组
题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋 ...
- LeetCode(112) Path Sum
题目 Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...