codeforces 665E E. Beautiful Subarrays(trie树)

题目链接:

E. Beautiful Subarrays

time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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!

 

Input
 

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.

 

Output
 

Print the only integer c — the number of beautiful subarrays of the array a.

 

Examples
 
input
3 1
1 2 3
output
5
input
3 2
1 2 3
output
3
input
3 3
1 2 3
output
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[N][2],val[N];
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;
}

 



转载于:https://www.cnblogs.com/zhangchengc919/p/5456980.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值