CodeForces - 617E 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 + 1, ..., ajis 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

题意:询问区间内异或和刚好为k的字段个数。思路:莫队+前缀和。这个前缀和比较套路,用的是前缀异或和。字段【l,r】的异或和就是pre[r]^pre[l-1],这种情况下我们在莫队的过程中记录l,r的pre[i]出现的次数,就可以完成更新了。

注意当L<q[i].l时,要先让记录per[L]出现次数的个数减一。

#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 100086;
const int maxm = 1000086;
const int inf = 2.1e9;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1);
int num[maxm*2],pre[maxn],a[maxn];

struct node{
    int l,r;
    int id;
}q[maxn];
ll ans[maxn];
ll anss;
int block;

bool cmp(node a,node b){
    if(a.l/block!=b.l/block){return a.l<b.l;}
    return a.r<b.r;
}

int main()
{
    int n,m,k;
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        pre[i]=pre[i-1]^a[i];
    }

    block=sqrt(n);

    for(int i=1;i<=m;i++){
        scanf("%d%d",&q[i].l,&q[i].r);
        q[i].id=i;
    }

    sort(q+1,q+1+m,cmp);
    int L=1,R=0;
    anss=0;
    num[0]=1;
    for(int i=1;i<=m;i++){
        while(L<q[i].l){
            int t=k^pre[L-1];
            num[pre[L-1]]--;///注意语句顺序
            anss-=num[t];
            L++;
        }
        while(R>q[i].r){
            int t=k^pre[R];
            num[pre[R]]--;
            anss-=num[t];
            R--;
        }
        while(L>q[i].l){
            L--;
            int t=k^pre[L-1];
            anss+=num[t];
            num[pre[L-1]]++;
        }
        while(R<q[i].r){
            R++;
            int t=k^pre[R];
            anss+=num[t];
            num[pre[R]]++;
        }
        ans[q[i].id]=anss;
    }
    for(int i=1;i<=m;i++){
        printf("%lld\n",ans[i]);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/ZGQblogs/p/10863851.html

时间: 2024-07-30 12:52:43

CodeForces - 617E XOR and Favorite Number (莫队+前缀和)的相关文章

(莫队算法)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

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

CodeForces 617E XOR and Favorite Number

莫队算法. #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; const int maxn=100000+10; int a[maxn],pre[maxn]; long long cnt[20*maxn]; int pos[maxn]; int n,m,k; long long ans[maxn]; long long Ans

Codeforces - 617E 年轻人的第一道莫队

我对莫队算法最为纠结的地方就是区间端点处,应该是像代码里那样理解吧 cnt[i]表示i出现的次数 maxn开2e6比较保险 /*H E A D*/ struct Query{ int l,r,id; }q[maxn]; int size; bool cmp(Query a,Query b){ if(a.l/size!=b.l/size) return a.l/size<b.l/size; else return a.r<b.r; } ll cnt[maxn],a[maxn]; ll ans[m

CodeForces - 86D 莫队算法

http://codeforces.com/problemset/problem/86/D 莫队算法就是调整查询的顺序,然后暴力求解. 每回可以通过现有区间解ans(l,r)得到区间(l+1,r),(l-1,r),(l,r+1),(l,r-1)的区间解. 调整方式http://blog.csdn.net/bossup/article/details/39236275 这题比那个还要简单,查询的是K^2*Z,很清楚就是莫队算法,然而做的时候没有学过,回来补题补到 关键是我一直没明白为什么重载小于号

Gym101138D Strange Queries 莫队、前缀和、容斥

传送门 THUWC2019D1T1撞题可还行 以前有些人做过还问过我,但是我没有珍惜,直到进入考场才追悔莫及-- 设\(que_{i,j}\)表示询问\((1,i,1,j)\)的答案,那么询问\((a,b,c,d)=que_{b,d} - que_{a-1 , d} - que_{b , c - 1} + que_{a - 1 , c - 1}\) 把一个询问拆成\(4\)个询问,然后对这\(4\)个询问莫队就可以了 不知道怎么回事THUWC上想到了莫队想到了前缀和想到了容斥就是没想到莫队+前缀

XOR and Favorite Number CodeForces - 617E -莫队-异或前缀和

CodeForces - 617E 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k.(注意 i ! =  j)维护一个前缀异或值就可以了.要注意的是 区间[l, r], 我们需要将pre[l-1]......pre[r]都加进去, pre[l-1]不能少. #include<bits/stdc++.h> using namespace std; #define maxn 1234567 #define l

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]. 详细的莫队可以百度学一