zoukankan      html  css  js  c++  java
  • Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法

    E. XOR and Favorite Number
    time limit per test
    4 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is given by a pair li and ri and asks you to count the number of pairs of integers i and j, such that l ≤ i ≤ j ≤ r and the xor of the numbers ai, ai + 1, ..., aj is equal to k.

    Input

    The first line of the input contains integers nm and k (1 ≤ n, m ≤ 100 000, 0 ≤ k ≤ 1 000 000) — the length of the array, the number of queries and Bob's favorite number respectively.

    The second line contains n integers ai (0 ≤ ai ≤ 1 000 000) — Bob's array.

    Then m lines follow. The i-th line contains integers li and ri (1 ≤ li ≤ ri ≤ n) — the parameters of the i-th query.

    Output

    Print m lines, answer the queries in the order they appear in the input.

    Examples
    input
    6 2 3
    1 2 1 1 0 3
    1 6
    3 5
    output
    7
    0
    input
    5 3 1
    1 1 1 1 1
    1 5
    2 4
    1 3
    output
    9
    4
    4
    Note

    In the first sample the suitable pairs of i and j for the first query are: (1, 2), (1, 4), (1, 5), (2, 3), (3, 6), (5, 6), (6, 6). Not a single of these pairs is suitable for the second query.

    In the second sample xor equals 1 for all subarrays of an odd length.

     题意:找出区间有多少对异或和为k的对数;

    思路:莫队算法,主要是如何o(1)更新,a[l]^a[l+1].....^a[r]=pre[r]^pre[l-1]=k;pre[i]表示a[1]^a[2]^....^a[i];

       pos[r]^pre[l-1]=k;   pre[r]=pre[l-1]^k;更新pre;

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    const int N=1e5+10,M=4e6+10,inf=1e9+10,mod=1e9+7;
    const ll INF=1e18+10;
    int si[N];
    struct is
    {
        int l,r,pos;
        bool operator < (const is &b)const
        {
            if(si[l]==si[b.l])
            return r<b.r;
            return si[l]<si[b.l];
        }
    }q[N];
    int a[N];
    int n,m,k;
    int l,r;
    ll out[N],ans;
    int flag[M<<1];
    void add(int pos)
    {
        ans+=flag[k^a[pos]];
        flag[a[pos]]++;
    }
    void del(int pos)
    {
        flag[a[pos]]--;
        ans-=flag[k^a[pos]];
    }
    int main()
    {
        scanf("%d%d%d",&n,&m,&k);
        int kuai=sqrt(n);
        for(int i=1;i<=n;i++)
        scanf("%d",&a[i]),si[i]=(i-1)/kuai+1,a[i]^=a[i-1];
        for(int i=1;i<=m;i++)
        scanf("%d%d",&q[i].l,&q[i].r),q[i].pos=i;
        sort(q+1,q+m+1);
        l=1;
        r=0;
        ans=0;
        flag[0]=1;
        for(int i=1;i<=m;i++)
        {
            while(l<q[i].l)
            {
                del(l-1);
                l++;
            }
            while(l>q[i].l)
            {
                l--;
                add(l-1);
            }
            while(r<q[i].r)
            {
                r++;
                add(r);
            }
            while(r>q[i].r)
            {
                del(r);
                r--;
            }
            out[q[i].pos]=ans;
        }
        for(int i=1;i<=m;i++)   printf("%lld
    ",out[i]);
        return 0;
    }
  • 相关阅读:
    go语言入门(3)运算符及流程控制
    go语言入门(2)数据类型
    go语言入门(1)
    ubuntu上软件下载慢,github下载慢
    密码基础知识(2)以RSA为例说明加密、解密、签名、验签
    让你减少焦虑的一首英文小诗
    使用脚本启动fabric时出错
    Hyperledger Fabric(5)ChainCode的编写步骤
    Hyperledger Fabric(4)链码ChainCode
    设计题专题总结
  • 原文地址:https://www.cnblogs.com/jhz033/p/5864371.html
Copyright © 2011-2022 走看看