zoukankan      html  css  js  c++  java
  • codeforces 617E 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.

    题意:给你n个数,有m个询问,问[l,r]之间有多少对i和j满足a[i]^a[i+1]^...^a[j]=k;

    思路:暴力绝对绝对绝对是不行的,所有就要用复杂度更低的算法啦,所以我就去学了莫队算法,莫队算法处理的是一种离线算法,是对询问进行分块排序来优化查询的算法;

    这题还要对异或运算要了解;我要去写个位运算的小总结;

    AC代码:

    #include <bits/stdc++.h>
    using namespace std;
    const int N=1e6+4;
    struct node
    {
        friend bool operator< (node x,node y)
        {
            if(x.pos==y.pos)return x.r<y.r;
            return x.l<y.l;
        }
        int l,r,id;
        int pos;
    };
    node qu[N];
    int n,m,k;
    int b[N],num[2*N];
    long long ans[N];
    void solve()
    {
        memset(num,0,sizeof(num));
        int le=1,ri=0;
        long long temp=0;
        for(int i=1;i<=m;i++)
        {
            while(ri<qu[i].r)
            {
                ri++;
                temp+=num[b[ri]^k];//num[]数组是当前le到ri内每个b[i](le<=i<=ri)异或一个要添加的数==k的数目;所以num[b[ri]^k]就是在已有的区间上再加一个
                num[b[ri]]++;//ri前能和ri异或==k的数目;后面这些操作一样
            }
            while(ri>qu[i].r)
            {
                num[b[ri]]--;
                temp-=num[b[ri]^k];
                ri--;
            }
            while(le>qu[i].l-1)
            {
                le--;
                temp+=num[b[le]^k];
                num[b[le]]++;
            }
            while(le<qu[i].l-1)
            {
                num[b[le]]--;
                temp-=num[b[le]^k];
                le++;
            }
            ans[qu[i].id]=temp;
        }
    }
    int main()
    {
        b[0]=0;
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&b[i]);
            b[i]^=b[i-1];
        }
        int s=sqrt(n);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&qu[i].l,&qu[i].r);
            qu[i].id=i;
            qu[i].pos=qu[i].l/s;
        }
        sort(qu+1,qu+m+1);
        solve();
        for(int i=1;i<=m;i++)
        {
            cout<<ans[i]<<"
    ";
        }
        return 0;
    }
  • 相关阅读:
    数据库 | 建表常用语句
    心得 | 撰写项目申报书
    工具 | 时间转化
    SpringBoot | 启动异常 | 显示bulid success 无 error信息
    120. 三角形最小路径和
    63. 不同路径 II
    SpringBoot | Velocity template
    SpringBoot | quartz | @DisallowConcurrentExecution
    SpringBoot | Hibernate @Transient 注解
    Java | 基础归纳 | 静态方法与实例方法的区别
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5308939.html
Copyright © 2011-2022 走看看