• Codeforces Round #340 (Div. 2) E 莫队+前缀异或和


    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 n, m 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.

    题意:给你一个长度为n的序列 q个查询[l,r]  问[l,r] 有多少个子区间的异或和为k

    题解:莫队+前缀异或和

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<map>
     7 #include<queue>
     8 #include<stack>
     9 #include<vector>
    10 #include<set>
    11 #define ll __int64
    12 using namespace std;
    13 int n,m,k;
    14 struct node
    15 {
    16     int l,r,id;
    17 }N[100005];
    18 int p[100005];
    19 int block;
    20 int a[100005];
    21 int x[100005];
    22 int mp[5000006];
    23 ll ans=0;
    24 ll re[100005];
    25 int cmp(struct node aa,struct node bb)
    26 {
    27     if(p[aa.l]==p[bb.l])
    28         return  aa.r<bb.r;
    29     else
    30         return p[aa.l]<p[bb.l];
    31 }
    32 void update(int w,int h)
    33 {
    34     if(h==1){
    35         ans=ans+mp[x[w]^k];
    36         mp[x[w]]++;
    37     }
    38     else
    39     {
    40         mp[x[w]]--;
    41         ans=ans-mp[x[w]^k];
    42     }
    43 }
    44 int main()
    45 {
    46     scanf("%d %d %d",&n,&m,&k);
    47     for(int i=1;i<=n;i++)
    48         scanf("%d",&a[i]);
    49     x[0]=0;
    50     mp[0]=1;
    51     for(int i=1;i<=n;i++)
    52         x[i]=a[i]^x[i-1];
    53     for(int i=1;i<=m;i++){
    54         scanf("%d %d",&N[i].l,&N[i].r);
    55         N[i].id=i;
    56     }
    57     block=(int)sqrt((double)n);
    58     for(int i=1;i<=n;i++)
    59         p[i]=(i-1)/block+1;
    60     sort(N+1,N+1+m,cmp);
    61     ans=0;
    62     for(int i=1,l=1,r=0;i<=m;i++)
    63     {
    64         for(;r<N[i].r;r++) update(r+1,1);
    65         for(;l>N[i].l;l--) update(l-2,1);// 取异或的原因
    66         for(;r>N[i].r;r--) update(r,-1);
    67         for(;l<N[i].l;l++) update(l-1,-1);//
    68         re[N[i].id]=ans;
    69     }
    70     for(int i=1;i<=m;i++)
    71     printf("%I64d
    ",re[i]);
    72    return 0;
    73 }
    74 /*
    75 10 2 0
    76 0 0 0 0 0 0 0 0 0 0
    77 2 8
    78 2 8
    79 */
  • 相关阅读:
    SequoiaDB数据库集群部署
    初步了解SequoiaDB数据库
    SequoiaDB数据库的一般概念介绍
    SequoiaDB(巨杉数据库)(社区版)安装配置使用图解
    记录用Django搭建博客的问题之一:SITE_ID=1
    解决问题七步法
    python的浅拷贝和深copy
    Python:监控键盘输入、鼠标操作,并将捕获到的信息记录到文件中 (转)
    用PYTHON监听鼠标和键盘事件(转)
    转:python中range和xrange的区别
  • 原文地址:https://www.cnblogs.com/hsd-/p/7049667.html
Copyright © 2020-2023  润新知