zoukankan      html  css  js  c++  java
  • 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
    ",ans);
    
        return 0;
    }


  • 相关阅读:
    Windows 下搭建Android开发环境
    浅谈C/C++中运算符的优先级、运算符的结合性以及操作数的求值顺序
    更新Android SDK到3.0版本时,遇到Failed to rename directory E:\android\tools to E:\android\temp\ToolPackage.old01问题
    单词计数 soj1076
    拓扑排序
    浅谈C和C++中的const关键字
    快速排序
    拓扑排序 soj1075
    集合划分问题
    浅谈C/C++中的顺序点和副作用
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5456980.html
Copyright © 2011-2022 走看看