您的位置:首页 > 其它

Codeforces 665E Beautiful Subarrays (Trie树)

2016-05-04 15:36 561 查看
题意:找出异或和>=k的连续子序列个数。

官方题解: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: