Educational Codeforces Round 12 E. Beautiful Subarrays trie求两异或值大于等于k对数
2016-04-22 14:39
447 查看
[b]E. Beautiful Subarrays[/b]
One 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!
[b]Input[/b]
The 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.
[b]Output[/b]
Print the only integer c — the number of beautiful subarrays of the array a.
[b]Examples[/b]
[b]input[/b]
[b]output[/b]
[b]题意:[/b]
给你n个数的序列,问你多少区间的异或值大于等于k
[b]题解:[/b]
对于每一个前缀我们插入trie中,
询问当前前缀与之前前缀的异或值大于等于k就好
One 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!
[b]Input[/b]
The 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.
[b]Output[/b]
Print the only integer c — the number of beautiful subarrays of the array a.
[b]Examples[/b]
[b]input[/b]
3 1 1 2 3
[b]output[/b]
5
[b]题意:[/b]
给你n个数的序列,问你多少区间的异或值大于等于k
[b]题解:[/b]
对于每一个前缀我们插入trie中,
询问当前前缀与之前前缀的异或值大于等于k就好
#include<bits/stdc++.h> using namespace std; const int maxn = 2e7+6; struct Tri { int ch[maxn][2]; int sz[maxn]; int tot; void init() { memset(ch,0,sizeof(ch)); memset(sz,0,sizeof(sz)); tot=2; } void insert(int x) { int u=1;sz[u]++; for(int i=30;i>=0;i--) { int p = (x>>i)&1; if(!ch[u][p])ch[u][p]=tot++; u=ch[u][p]; sz[u]++; } } int get(int x,int y) { int u=1; long long ans = 0; for(int i=30;i>=0;i--) { int p = (x>>i)&1^1; int q = (y>>i)&1; if(q==0)ans+=sz[ch[u][p]],u=ch[u][p^1]; else u=ch[u][p]; if(u==1) return ans; } return ans+sz[u]; } }T; int main() { T.init(); int n,k; scanf("%d%d",&n,&k); int pre = 0; long long ans = 0; T.insert(0); for(int i=1;i<=n;i++) { int x;scanf("%d",&x); pre^=x; ans+=T.get(pre,k); T.insert(pre); } cout<<ans<<endl; return 0; }
相关文章推荐
- 指针与数组的比较
- 20160421HTTP Status 500 - java.lang.LinkageError loader constraint violation when resolving method o
- 产品推广都有哪些方法可以用?
- 游戏服务器的数据保存
- 200. Number of Islands
- Android之从TCP/IP、HTTP看Socket通信
- js array 常用方法
- 安卓网络请求框架对比
- <LeetCode OJ> 344. Reverse String
- android 百度地图定位开发2
- open行情
- C++作业4-继承和派生1
- 《翻译漫谈》笔记之5 长难句
- Hive连接MYSQL数据OK,查询出现in thread "Thread-18" java.lang.IllegalArgumentException:Does not contain a vali
- c++之精确度
- android 百度地图定位开发1
- Android PullToRefresh (ListView GridView 下拉刷新) 使用详解
- 淘宝首页性能优化实践
- MySQL学习笔记—视图
- openHevc学习笔记:工程代码基本结构