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 ...
随机推荐
- NYOJ-06-喷水装置(一)
http://acm.nyist.net/JudgeOnline/problem.php?pid=6 喷水装置(一) 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 现有 ...
- vue for循环中常见问题 之 求和(合计)
例:求后台返回数据this.dataInfo 中某个字段(item.totalSum)的和,只需添加computed,然后模板中直接可以使用totalSumAll (不需要再data中声明) comp ...
- 关于回顾css发现的一些问题
1.针对于before和after伪元素的用法: <style> .clearfix:before, .clearfix:after{ clear:both; content:" ...
- Python基础篇 -- 运算符和编码
运算符 记熟 ! ! ! 2**1=2 2**2=4 2**3=8 2**4=16 2**5=32 2**6=64 2**7=128 2**8=256 2**9=512 2**10=1024 运算符 ...
- webuploader项目中多文件上传实例
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- shell脚本,文件里面的英文大小写替换方法。
[root@localhost wyb]# cat daxiaoxie qweBNMacb eeeDFSmkl svdIOPtyu [root@localhost wyb]# cat daxiaoxi ...
- Swift 中 String 与 CChar 数组的转换
在现阶段Swift的编码中,我们还是有很多场景需要调用一些C函数.在Swift与C的混编中,经常遇到的一个问题就是需要在两者中互相转换字符串.在C语言中,字符串通常是用一个char数组来表示,在Swi ...
- 【数论 dp】2048
考场上一个DFS优化乱加就对了一个无解的点 题目描述 给定一个长度为 n 的数列,在这个数列中选取一个子序列使得这个子序列中的数能合出2048 对于合并操作,可以选择这个序列中的任意两个数进行合并,当 ...
- JavaScript正则表达式-RegExp对象
RegExp对象方法 exec():与String对象的match()方法功能相同. 参数为被搜索字符串.返回数组或null. test():与String对象的search()方法功能相同. 参数为 ...
- PAT Basic 1045
1045 快速排序 著名的快速排序算法里有一个经典的划分过程:我们通常采用某种方法取一个元素作为主元,通过交换,把比主元小的元素放到它的左边,比主元大的元素放到它的右边. 给定划分后的 N 个互不相同 ...