题目链接:

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树】的更多相关文章

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

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

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

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

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

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

  4. E. Beautiful Subarrays 字典树

    http://codeforces.com/contest/665/problem/E 给定一个序列,问其中有多少个区间,所有数字异或起来 >= k 看到异或,就应该想到异或的性质,A^B^B ...

  5. codeforces 665E Beautiful Subarrays

    题目链接 给一个数列, 让你找出异或结果大于等于k的子序列的个数. 因为任意一段序列的异或值都可以用前缀异或和来表示, 所以我们先求出前缀异或和. 我们考虑字典树, 对于每一个前缀sum, 我们先查询 ...

  6. 【Codeforces】665E Beautiful Subarrays

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

  7. 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,   ...

  8. Beautiful Subarrays

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

  9. Codeforces 55D Beautiful Number

    Codeforces 55D Beautiful Number a positive integer number is beautiful if and only if it is divisibl ...

随机推荐

  1. 换个语言学一下 Golang (1)

    做技术的总是有些拗.这么多年一直在.net的框框里打转转.直到现在市场上.net工作越来越难找,项目越来越老才发现不做出改变不行了.就从学习Go开始吧. Go语言的特点 简洁.快速.安全 并行.有趣. ...

  2. Cscope的使用(领略Vim + Cscope的强大魅力)

    文章出处:http://blog.csdn.net/dengxiayehu/article/details/6330200 Cscope的使用(领略Vim + Cscope的强大魅力) 1.Cscop ...

  3. HTML5触摸事件

    touchstart .touchmove .touchend 事件 touchstart事件:当手指触摸屏幕时触发,即使有一个手指放在屏幕上也会触发. touchmove事件:当手指在屏幕上滑动时触 ...

  4. (51)zabbix命令:zabbix_get获取item数据

    zabbix_get是什么?有什么作用? 总有人在群里提问,为什么zabbix获取不到数据,为什么zabbix提示Not Support,怎么办?别老问,用zabbix_get试着获取数据即可.在za ...

  5. perl学习之argument

    Arguments are the values you pass to a Perl script. Each value on the command line after the name of ...

  6. Web框架之Django_01初识(三大主流web框架、Django安装、Django项目创建方式及其相关配置、Django基础三件套:HttpResponse、render、redirect)

    摘要: Web框架概述 Django简介 Django项目创建 Django基础必备三件套(HttpResponse.render.redirect) 一.Web框架概述: Python三大主流Web ...

  7. web开发框架之DRF

    RESTful架构解释: # Representational State Transfer 表现层状态转化 到底什么是RESTFul架构: 如果一个架构符合REST原则,就称它为RESTful架构. ...

  8. 【练习】reserving.kr 之Direct3D FPS

    算法函数如图,关键点在标志处,加密字符串如下图 于是写如下脚本: flag_c='436B666B62756C694C455C455F5A461C07252529701734390116494C201 ...

  9. set的应用:UVa10815-Andy's First Dictionary

    Andy's First Dictionary Andy, 8, has a dream - he wants to produce his very own dictionary. This is ...

  10. Java技术——Java中的内存泄漏

    . OOM的常见类型 按照JVM规范,JAVA虚拟机在运行时会管理以下的内存区域: 程序计数器:当前线程执行的字节码的行号指示器,线程私有. JAVA虚拟机栈:Java方法执行的内存模型,每个Java ...