CF-617E-XOR and Favorite Number(莫队)

本文详细介绍了一种使用莫队算法解决区间异或等于特定值问题的方法。通过实例解释了如何通过预处理和排序查询来实现高效的区间查询,最终达到O(n^1.5)的时间复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 0000 ≤ 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: (12), (14), (15), (23), (36), (56), (66). 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.


题目:http://codeforces.com/problemset/problem/617/E


题意:求i-j区间内异或值等于k的个数。

输入过程中异或a[i]^=a[i-1];则有^[i...j] = a[i-1]^a[j]=k ,   则a[j]^k=a[i-1];莫队处理区间个数。


卿学姐莫队视频讲解:http://www.bilibili.com/video/av4291097/

莫队参考:http://blog.youkuaiyun.com/bossup/article/details/39236275

  要知道我们算完[L,R]的答案后现在要算[L',R']的答案。由于可以在O(1)的时间下得到[L,R-1]和[L,R+1]和[L-1,R]和[L+1,R]的答案.所以计算[L',R']的答案花的时间为|L-L'|+|R-R'|。如果把询问[L,R]看做平面上的点a(L,R).询问[L',R']看做点b(L',R')的话。那么时间开销就为两点的曼哈顿距离。所以对于每个询问看做一个点。我们要按一定顺序计算每个值。那开销就为曼哈顿距离的和。要计算到每个点。那么路径至少是一棵树。所以问题就变成了求二维平面的最小曼哈顿距离生成树。

关于二维平面最小曼哈顿距离生成树。感兴趣的可以参考点击打开链接

这样只要顺着树边计算一次就ok了。可以证明时间复杂度为n*sqrt(n)这个我不会证明。

但是这种方法编程复杂度稍微高了一点。所以有一个比较优雅的替代品。那就是先对序列分块。然后对于所有询问按照L所在块的大小排序。如果一样再按照R排序。然后按照排序后的顺序计算。为什么这样计算就可以降低复杂度呢。

一、i与i+1在同一块内,r单调递增,所以r是O(n)的。由于有n^0.5块,所以这一部分时间复杂度是n^1.5。
二、i与i+1跨越一块,r最多变化n,由于有n^0.5块,所以这一部分时间复杂度是n^1.5
三、i与i+1在同一块内时变化不超过n^0.5,跨越一块也不会超过2*n^0.5,不妨看作是n^0.5。由于有n个数,所以时间复杂度是n^1.5
于是就变成了O(n^1.5)了。



#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#include <iomanip>
using namespace std;
#define ll long long
const int N = 1e6+1e6+10;
int a[N];
int belong[N];
struct node
{
    int a,b;
    int id;
}q[N];
bool cmp(node x,node y)
{
    if(belong[x.a]==belong[y.a])
        return x.b<y.b;
    return belong[x.a]<belong[y.a];
}
long long ans[N];
long long sum[N];
int main()
{
    int n,m,k;
    scanf("%d%d%d",&n,&m,&k);
    int sz=sqrt(n);
    a[0]=0;
    for(int i=1; i<=n; ++i)
    {
        scanf("%d",&a[i]);
        a[i]^=a[i-1];
        belong[i]=i/sz;
    }
    for(int i=0; i<m; ++i)
    {
        scanf("%d%d",&q[i].a,&q[i].b);
        q[i].id=i;
    }
    sort(q,q+m,cmp);
    memset(sum,0,sizeof(sum));
    int A=1,B=0;
    long long ANS=0;
    sum[0]=1;
    for(int i=0; i<m; ++i)
    {
        int l=q[i].a;
        int r=q[i].b;
        while(B<r)
        {
            ++B;
            ANS+=sum[ a[B]^k ];
            sum[a[B]]++;
        }
        while(B>r)
        {
            sum[a[B]]--;
            ANS-=sum[a[B]^k];
            --B;
        }
        while(A<l)
        {
            sum[ a[A-1] ]--;
            ANS-=sum[a[A-1]^k];
            ++A;
        }
        while(A>l)
        {
            --A;
            ANS+=sum[a[A-1]^k];
            sum[a[A-1]]++;
        }
        ans[q[i].id]=ANS;
    }
    for(int i=0; i<m; ++i)
        cout<<ans[i]<<endl;
    return 0;
}
/*
6 2 1
1 2 1 1 0 3
1 1
3 4
1
2
*/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值