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;
    }


  • 相关阅读:
    java学习6-java基础类库
    java学习5-面向对象(下)
    java学习4-面向对象(上)
    java学习3-流程控制与数组
    java学习2-数据类型和运算符
    pycharm(社区版2019.1版本)打开README.md文件卡死解决办法
    python深拷贝与浅拷贝的区别
    python中list切片详解
    python使用input().split()接收多个用户输入
    python学习-语言概述(一)
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5456980.html
Copyright © 2011-2022 走看看