Codeforces 665E Beautiful Subarrays (Trie树)
2016-05-04 15:36
561 查看
题意:找出异或和>=k的连续子序列个数。
官方题解:http://codeforces.com/blog/entry/44466
官方题解:http://codeforces.com/blog/entry/44466
: [code]#include<cstdio> #include<cstring> using namespace std; typedef long long LL; const int maxn = 1e6+5; const int ML = 30; struct Nod{ int cnt,son[2]; void init(){ son[0] = son[1] = 0; cnt = 0; } }buf[maxn*15]; int n,m,a[maxn],root,tot; int new_node(){ buf[++tot].init(); return tot; } void init(){ tot = 0; buf[0].init(); root = new_node(); } void add(int x){ int i,d,cur = root; for(i = ML;i >= 0;i--){ d = x>>i&1; if(!buf[cur].son[d]) buf[cur].son[d] = new_node(); cur = buf[cur].son[d]; buf[cur].cnt++; } } LL sol(int x){ int i,d,cur = root,val = 0; LL ans = 0; for(i = ML;i >= 0;i--){ d = x>>i&1; if((val|(1<<i)) >= m){ ans += buf[buf[cur].son[d^1]].cnt; cur = buf[cur].son[d]; }else{ val |= (1<<i); cur = buf[cur].son[d^1]; } if(!cur) break; } return ans; } int main(){ int i,j; scanf("%d%d",&n,&m); LL res = 0; init(); add(0); for(i = 1;i <= n;i++){ scanf("%d",&a[i]); a[i] ^= a[i-1]; res += sol(a[i]); //printf("%d %lld\n",i,res); add(a[i]); } printf("%I64d\n",res); return 0; }
相关文章推荐
- java 集合 list 接口 集合的迭代 超级for循环 扩容 链表 map Hash
- 大数模板
- HDU2594 Simpsons’ Hidden Talents
- 第九课等于之关键调用——C#计算器编程教学
- Kettle中调用用户自定义的jar包
- MVC WebApi 将返回值改为JSON格式
- HttpGet 请求(带参数)
- ThreadPoolExecutor使用
- 让iOS工程目录和文件夹保持一致的工具:synx介绍
- [Android]Jack和Jill的阴暗面
- 让iOS工程目录和文件夹保持一致的工具:synx介绍
- poj3270
- 内建模块-struct
- 堆排序的c语言实现
- HttpURLConnection 发送PUT请求,设置请求头参数 json请求体
- 主成分分析之数学推导
- Maven聚合与继承
- FZU2236 第十四个目标 dp+树状数组优化
- linux查看服务器连接数
- MS-OFFICE2013之EXCEL神器-Power Map数据地图可视化范例