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 31 2 1 1 0 31 63 5

output

70

input

5 3 11 1 1 1 11 52 41 3

output

944

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的序列,然后给你一个数字k,再给出m组询问

询问给出一个区间,问这个区间里面有多少对数异或结果为k。

做法:我们可以先预处理出所有异或结果的前缀结果,a[n]表示前n个数的异或结果。这样做有什么好处?我们知道x^x=0,所以如果我们要求区间[i,j]上的异或结果,可以用a[j]^a[i-1]得到。因此,我们可以从左端点开始,枚举每一个点,枚举的途中用flag[]数组记录前缀和出现的次数,比如前缀和2出现过一次,那flag[2]++;在查询的时候,a[i]^k就是我们要找的前缀和的大小,因为前面知道如果我们要求区间[i,j]上的异或结果,可以用a[j]^a[i-1]得到,所以我们要找之前有多少个前缀,和现在的前缀异或值为k,对应到flag数组去找a[i]^k的个数,并更新答案就行了。以上是异或处理部分。

接下来是莫队算法。莫队算法是一个暴力算法,用于解决区间问题。他的原理是假设我知道了[L,R]的值,我可以在o1的时间求出[L+/-1,R]和[L,R+/-1]的值,那么查询[L+/-k,R]和[L,R+/-k]的值需要k步。如果我们要查询多个区间的问题,其实就是一个最小曼哈顿生成树问题了(最少的边权和走完所有点),可以用最小曼哈顿生成树算法来处理莫队。不过其实还有更方便的算法,就是分块。我们可以根据区间左端点先进行分块,分为sqrt(n)块,块里面的元素按照右端点进行排序。这样排序后,考虑左端点,由于每个块只有sqrt(n)的大小,左端点移动最多也就sqrt(n)的复杂度,n个区间就n*sqrt(n)的复杂度。对于右端点,由于每个块的右端点是有序的,所以我们查询一个块上的值,最多是n(区间的最大值,这题是10*n),又有最多查询sqrt(n)块,复杂度也是n*sqrt(n),总复杂度n*sqrt(n)。

具体代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define X first
#define Y second
#define clr(u,v); memset(u,v,sizeof(u));
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=1<<20;
const int INF=0x3f3f3f3f;
ll pos[maxn];
ll flag[maxn],ans[maxn];
int a[maxn];
struct node
{
    int l,r,id;
}Q[maxn];
bool cmp(node a,node b)
{
    if (pos[a.l]==pos[b.l])
    {
        return a.r<b.r;
    }
    return pos[a.l]<pos[b.l];
}
int n,m,k;
int L=1,R=0;
ll Ans=0;
void add(int x)
{
    Ans+=flag[a[x]^k];
    flag[a[x]]++;
}
void del(int x)
{
    flag[a[x]]--;
    Ans-=flag[a[x]^k];
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    int sz=sqrt(n);
    for (int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        a[i]^=a[i-1];
        pos[i]=i/sz;
    }
    for (int i=1;i<=m;i++)
    {
        scanf("%d%d",&Q[i].l,&Q[i].r);
        Q[i].id=i;
    }
    flag[0]=1;
    sort(Q+1,Q+m+1,cmp);
    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--;
        }
        ans[Q[i].id]=Ans;
    }
    for (int i=1;i<=m;i++)
    printf("%I64d\n",ans[i]);
    return 0;
}

2016-09-25 03:31:38

时间: 2024-08-02 10:55:30

XOR and Favorite Number(莫队算法+分块)的相关文章

Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法

E. XOR and Favorite Number 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

BZOJ 3585 mex 莫队算法+分块

题目大意:给定一个长度为n的数组,m次询问某个区间内的mex值 怒写莫队233 将权值分成√n块,记录每个权值的出现次数以及每块内有多少权值出现过 修改O(1)即可完成 查询时首先扫一遍找到第一个块内有没有覆盖的点的块 然后在块内暴力查找 时间复杂度O(√n) 套个莫队 总时间复杂度O(m√n) #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include

【BZOJ】2038: [2009国家集训队]小Z的袜子(hose)(组合计数+概率+莫队算法+分块)

http://www.lydsy.com/JudgeOnline/problem.php?id=2038 学了下莫队,挺神的orz 首先如果推公式的话很简单吧.对于查询$[l,r]$ $$ans=\frac{\sum \binom{x_i}{2}}{\binom{r-l+1}{2}}$$ //晚修...回来补.. #include <cstdio> #include <cstring> #include <cmath> #include <string> #

莫队算法分块大小玄学调参指南

总算弄懂了这个分块大小怎么算... 两个指针 复杂度 \(O(u*n+\frac{n^2}{u})\) 根据均值不等式, \(u*n+\frac{n^2}{u}\) 在 \(u*n=\frac{n^2}{u}\) 时取最小值 即 \(u=\sqrt{n}\) 三个指针(带修) 复杂度 \(O(u*n+\frac{n^2}{u}+\frac{n^3}{u^2})\) 显然, \(\frac{n^2}{u}<\frac{n^3}{u^2}\) (作商法) 根据均值不等式, \(u*n+\frac{n

CodeFroce Round 340 div2 E XOR and Favorite Number【莫队算法】

题面: 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?

Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫队算法 + 异或和前缀和的巧妙】

任意门:http://codeforces.com/problemset/problem/617/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 yo

codeforces 617 E. XOR and Favorite Number(莫队算法)

题目链接:http://codeforces.com/problemset/problem/617/E 题目: 给你a1 a2 a3 ··· an 个数,m次询问:在[L, R] 里面又多少中 [l, r] 使得 al xor al+1 xor ··· ar 为 k. 题解: 本题只有区间查询没有区间修改,而且数据量不大(10w),所以可以用离线的方法解决. 使用莫队算法来解决,就需要O(1)的修改[L, R+1] .[L, R-1].[L+1, R].[L-1, R]. 详细的莫队可以百度学一

【莫队算法】【权值分块】poj2104 K-th Number / poj2761 Feed the dogs

先用莫队算法保证在询问之间转移的复杂度,每次转移都需要进行O(sqrt(m))次插入和删除,权值分块的插入/删除是O(1)的. 然后询问的时候用权值分块查询区间k小值,每次是O(sqrt(n))的. 所以总共的复杂度是O(m*(sqrt(n)+sqrt(m)))的. 常数极小. 别的按权值维护的数据结构无法做到O(1)地插入删除. poj2104 的输出优化 别忘了处理负数. 完爆主席树,这份代码目前在 poj2761 上 Rank1. Rank Run ID User Memory Time

(莫队算法)CodeForces - 617E XOR and Favorite Number

题意: 长度为n的数列,m次询问,还有一个k.每次询问询问询问从数列的L到R内有多少个连续子序列异或起来等于k. 分析: 因为事先知道这题可以用莫队写,就正好用这题练习莫队. 预处理每个前缀异或和. 然后莫队按分块排序后,不断更新,用一个数组cnt[]记录当前L到R前缀和的数量. R向右拉,新增的数量就是cnt[pre^k],pre表示当前这个R位置的前缀异或和,然后更新一下cnt. 其他的也类似. 算是一个比较好的入门题. 代码: 1 #include <cstdio> 2 #include