题目链接:

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. Feign-请求不同注册中心的服务

    场景 需要通过Feign Client请求,其他注册中心或者其他Restful服务. 临时方案 Feign 请求转为RestTemplate http请求. 优点:能适应,feign环境和非feign ...

  2. struts1标签库

    Struts提供了五个标签库,即:HTML.Bean.Logic.Template和Nested. HTML标签 : 用来创建能够和Struts 框架和其他相应的HTML 标签交互的HTML 输入表单 ...

  3. 前端vue 里的tab切换 减少dom操作

    <div class="vuedemo"> <div class="all"> <div class="tabone&q ...

  4. vue-cli3.0 生产包去除console.log

    目前负责的公众号又迭代了一个版本,之前打生产包,配置总是和测试包搞混,所以使用了vue-cli3.0的环境变量来控制配置. 但是又发现了一个新问题,写代码的过程中写了很多console.log 来调试 ...

  5. 洛谷 P1483 序列变换

    https://www.luogu.org/problemnew/show/P1483 数据范围不是太大. 一个数组记录给k,记录每个数加了多少. 对于查询每个数的大小,那么就枚举每个数的因子,加上这 ...

  6. (1) zabbix进程构成

    进程介绍 zabbix_agentd客户端守护进程,此进程收集客户端数据,例如cpu负载.内存.硬盘使用情况等 zabbix_getzabbix工具,单独使用的命令,通常在server或者proxy端 ...

  7. verilog random使用

    “$random函数调用时返回一个32位的随机数,它是一个带符号的整形数...”,并给出了一个例子: _________________________________________________ ...

  8. C++代码学习之一:组合模式例子

    #include"AbstractFile.h" void AbstractFile::add(AbstractFile*) { } void AbstractFile::remo ...

  9. verilog behavioral modeling --loop statement

    1.forever 2.repeat 3.while 4.for The for statement accomplishes the same results as the following ps ...

  10. Processed foods make us fatter easily

    From Business Insider Here's an experiment: sit alone in a hospital room for two weeks and eat nothi ...