zoukankan      html  css  js  c++  java
  • Beautiful Subarrays

    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
    分析:trie树,保留每个前缀再异或即可;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, rt<<1
    #define Rson mid+1, R, rt<<1|1
    const int maxn=2e7+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,ch[maxn][2],sz[maxn],tot;
    ll ans;
    void insert(int p)
    {
        int now=0;
        for(int i=30;i>=0;i--)
        {
            int q=(p>>i)&1;
            if(!ch[now][q])ch[now][q]=++tot;
            now=ch[now][q],sz[now]++;
        }
    }
    ll get(int p)
    {
        int now=0;
        ll ans=0;
        for(int i=30;i>=0;i--)
        {
            int q=(p>>i)&1^1,t=(m>>i)&1;
            if(!t)ans+=sz[ch[now][q]],now=ch[now][q^1];
            else now=ch[now][q];
            if(!now)return ans;
        }
        return ans+sz[now];
    }
    int main()
    {
        int i,j;
        insert(0);
        scanf("%d%d",&n,&m);
        while(n--)
        {
            scanf("%d",&j),k^=j;
            ans+=get(k);
            insert(k);
        }
        printf("%lld
    ",ans);
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    9-10-堆 Windows消息队列(25 分)
    9-7 二叉搜索树的结构(30 分)
    9-4 笛卡尔树(25 分)
    9-3 搜索树判断(25 分)
    7-9 堆中的路径(25 分)
    个人总结
    软工网络15个人作业4——alpha阶段个人总结
    软件工程网络15个人作业3——案例分析(201521123029 郑佳明)
    软件工程15 结对编程作业
    软件工程网络15个人阅读作业2(201521123029 郑佳明)
  • 原文地址:https://www.cnblogs.com/dyzll/p/5899107.html
Copyright © 2011-2022 走看看