codeforces 665E E. Beautiful Subarrays(trie树)
2016-05-03 23:59
393 查看
题目链接:E. Beautiful Subarraystime limit per test
3 secondsmemory limit per test
512 megabytesinput
standard inputoutput
standard outputOne day, ZS the Coder wrote down an array of integers a with elements a1, a2, ..., an.A subarray of the array a is a sequence al, al + 1, ..., ar for some integers (l, r) such that 1 ≤ l ≤ r ≤ n. ZS the Coder thinks that a subarray of a is beautiful if the bitwise xor of all the elements in the subarray is at least k.Help ZS the Coder find the number of beautiful subarrays of a!InputThe first line contains two integers n and k (1 ≤ n ≤ 106, 1 ≤ k ≤ 109) — the number of elements in the array a and the value of the parameter k.The second line contains n integers ai (0 ≤ ai ≤ 109) — the elements of the array a.OutputPrint the only integer c — the number of beautiful subarrays of the array a.Examplesinput
3 secondsmemory limit per test
512 megabytesinput
standard inputoutput
standard outputOne day, ZS the Coder wrote down an array of integers a with elements a1, a2, ..., an.A subarray of the array a is a sequence al, al + 1, ..., ar for some integers (l, r) such that 1 ≤ l ≤ r ≤ n. ZS the Coder thinks that a subarray of a is beautiful if the bitwise xor of all the elements in the subarray is at least k.Help ZS the Coder find the number of beautiful subarrays of a!InputThe first line contains two integers n and k (1 ≤ n ≤ 106, 1 ≤ k ≤ 109) — the number of elements in the array a and the value of the parameter k.The second line contains n integers ai (0 ≤ ai ≤ 109) — the elements of the array a.OutputPrint the only integer c — the number of beautiful subarrays of the array a.Examplesinput
3 1 1 2 3output
5input
3 2 1 2 3output
3input
3 3 1 2 3output
2 题意: 给n个数,问有多少对数满足a[i]^a[i+1]^...^a[j]>=k; 思路: 处理出前缀异或和,再用trie树搞; AC代码:
#include <bits/stdc++.h> using namespace std; #define Riep(n) for(int i=1;i<=n;i++) #define Riop(n) for(int i=0;i<n;i++) #define Rjep(n) for(int j=1;j<=n;j++) #define Rjop(n) for(int j=0;j<n;j++) #define mst(ss,b) memset(ss,b,sizeof(b)); typedef long long LL; const LL mod=1e9+7; const double PI=acos(-1.0); const int inf=0x3f3f3f3f; const int N=2e7+6; int n,k,cnt=2; int tree [2],val ; void add_num(int num) { int x=1; for(int i=30;i>=0;i--) { int y=(num>>i)&1; if(!tree[x][y])tree[x][y]=cnt++; val[x]++; x=tree[x][y]; } val[x]++; } LL get_ans(int num,int fy) { LL sum=0; int x=1; for(int i=30;i>=0;i--) { int y=(fy>>i)&1; int fx=(num>>i)&1^1; if(y==0) { sum+=(LL)val[tree[x][fx]]; x=tree[x][fx^1]; } else x=tree[x][fx]; } return sum+(LL)val[x]; } int main() { scanf("%d%d",&n,&k); int pre=0; LL ans=0; add_num(0); Riep(n) { int a; scanf("%d",&a); pre^=a; ans+=(LL)get_ans(pre,k); add_num(pre); } printf("%I64d\n",ans); return 0; }
相关文章推荐
- Linux 利用hosts.deny 防止暴力破解ssh(转)
- Matlab绘图详解
- 软件包管理及常用指令
- C++11 正则表达式——实例1
- 【JavaScript】今天开始应该使用 5 个JavaScript调试技巧
- JAVA - 优雅的记录日志(log4j实战篇) (转)
- O(n)获得中位数及获得第K小(大)的数
- PHP学习笔记——二维数组的遍历
- 如何用15分钟开启高效的一天
- HDU 2046 骨牌铺方格(递推+打表)
- Linux基本命令练习(3)
- 程序员的成长阶梯和级别定义(转)
- MATLAB处理神经网络基础练习
- linux命令大全(1)
- VS DevExtreme控件
- 一个程序猿的计划
- LeetCode 347. Top K Frequent Elements
- nginx php环境配置配置
- java日志设置及常用配置
- 什么是序列化,为什么要序列化