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 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 的数列,M次查询 ( l,r )内有多少对 ( i, j )使得 ai ^ ai+1 ^ ... ^ aj = K;

解题思路:

这里的区间查询是离线的,可以用传说中的莫队算法(优雅而华丽的暴力算法)。

异或的题目有一个很巧妙的地方就是利用 a^b^a = b 这个性质。

这道题目我们也要预处理一下 sum(i) 前 i 个数的异或和, 那么 ai ^ ai+1 ^ ... ^ aj = sum( i - 1) ^ sum( j ) 了。

接下来我们就可以按照查询的区间用莫队算法遍历一遍,同时记录当前区间某个前缀和的出现次数;

根据 sum( i - 1) ^ sum( j ) = K ,K  ^ sum( i - 1 ) = sum( j ) || K ^ sum( j ) = sum( i - 1) ,由符合条件的前缀和次数来推出符合条件的( i,j )的个数啦。

Tip:

听了大神的课,这道题有两个坑:

1、答案的数据的数据范围是爆 int 的;

2、虽然是1e6 的 K,但异或的结果要大于 1e6 ;

AC code:

 1 #include <bits/stdc++.h>
 2 #define LL long long int
 3 using namespace std;
 4 const int MAXN = 1<<20;
 5
 6 struct node
 7 {
 8     int l, r, id;  //区间和查询编号
 9 }Q[MAXN];       //记录查询数据(离线)
10
11 int pos[MAXN];  //记录分块
12 LL ans[MAXN];   //记录答案
13 LL flag[MAXN];  //维护前缀异或和出现的次数
14 int a[MAXN];    //原本数据
15 int L=1, R;     //当前区间的左右结点
16 LL res;         //储存当前区间的值
17 int N, M, K;
18 bool cmp(node a, node b)     //排序
19 {
20     if(pos[a.l]==pos[b.l]) return a.r < b.r;  //只有左结点在同一分块才排右结点
21     return pos[a.l] < pos[b.l];               //否则按照左结点分块排
22 }
23 void add(int x)
24 {
25     res+=flag[a[x]^K];
26     flag[a[x]]++;
27 }
28 void del(int x)
29 {
30     flag[a[x]]--;
31     res-=flag[a[x]^K];
32 }
33 int main()
34 {
35     scanf("%d%d%d", &N, &M, &K);
36     int sz = sqrt(N);
37     for(int i = 1;  i <= N; i++){  //读入数据
38         scanf("%d", &a[i]);
39         a[i] = a[i]^a[i-1];    //计算前缀异或和
40         pos[i] = i/sz;         //分块
41     }
42     for(int i = 1; i <= M; i++){    //读入查询
43         scanf("%d%d", &Q[i].l, &Q[i].r);
44         Q[i].id = i;
45     }
46     sort(Q+1, Q+1+M, cmp);
47     flag[0] = 1;
48     for(int i = 1;  i <= M; i++){
49         while(L < Q[i].l){    //当前左结点比查询结点小
50             del(L-1);
51             L++;
52         }
53         while(L > Q[i].l){    //当前左结点比查询左结点大
54             L--;
55             add(L-1);
56         }
57         while(R < Q[i].r){    //当前右结点比查询右结点小
58             R++;
59             add(R);
60         }
61         while(R > Q[i].r){    //当前右节点比查询右节点大
62             del(R);
63             R--;
64         }
65         ans[Q[i].id] = res;
66     }
67     for(int i = 1; i <= M; i++){
68         printf("%lld\n", ans[i]);
69     }
70 }

原文地址:https://www.cnblogs.com/ymzjj/p/9563453.html

时间: 2024-07-28 20:58:46

Codeforces Round #340 (Div. 2) E. 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

Codeforces Round #286 (Div. 2) B. Mr. Kitayuta&#39;s Colorful Graph +foyd算法的应用

B. Mr. Kitayuta's Colorful Graph time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the g

Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题是线段树成段更新,但是不能直接更新,不然只能一个数一个数更新.这样只能把每个数存到一个数组中,长度大概是20吧,然后模拟二进制的位操作.仔细一点就行了. 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath>

Codeforces Round #340 (Div. 2) B. Chocolate

题意:一段01串 分割成段 每段只能有一个1 问一段串有多少种分割方式 思路:两个1之间有一个0就有两种分割方式,然后根据分步乘法原理来做. (不过这里有一组0 1 0这种数据的话就不好直接处理,所以遇到第一个1才开始标记) 1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 long long l=0; 6 int n; 7 cin>>n; 8 int a; 9 int j=0; 10 int s=0; 1

莫队 Codeforces Round #340 (Div. 2) E

题目大意:给你一个长度为n的序列,有m个询问,每次询问一个区间[L,R],表示这个区间内,有多少的a[i]^a[i+1].....^a[j]=k. 思路:莫队去搞就好了 我们定义pre[i]=a[1]^a[2]^....a[i],然后我们再去更新就好了,这里,我们定义区间[L,R]表示我们所要询问的区间即可.然后这里会有一个疑惑,就是为什么while(l<Q[i].l) { Delete(l-1); l++; }和while(l>Q[i].l) { l--; Updata(l-1); }是这样

Codeforces Round #172 (Div. 1) BMaximum Xor Secondary 单调栈

//给一个长度为N的个不相同的序列,找出所有区间中最大值和第二大数的异或值最大的值 //对于所有区间只需要找其最大值和第二大数,所以对于很多区间的结果是重复的 //对于每一个数,它起作用的区间只有在其前面最多只有一个数是大于它的 //可以用一个单调递减栈来做,对于每一个新的数a[i],在它前面第一个大于它的数a[j] //和第二个大于它的数之间的数到a[i]的区间的数的最大值和第二大数为a[j] , a[i] //只需要找a[i],a[j]的所有区间所有情况 #include<cstdio>

Codeforces Round #340 (Div. 2) B

Description Bob loves everything sweet. His favorite chocolate bar consists of pieces, each piece may contain a nut. Bob wants to break the bar of chocolate into multiple pieces so that each part would contain exactly one nut and any break line goes

Codeforces Round #326 (Div. 1) - C. Duff in the Army 树上倍增算法

题意:一个n个点的数, m个人住在其中的某些点上, 每个人的标号1-m, 询问u-v 路径上标号前a个人,并输出标号,a < 10. 作法, 利用倍增, ID[j][i] 表示i到i的第2^j个祖先上前10个人, 那么每次询问直接维护就好了,细节好多, 刚开始不知道怎么求ID[j][i]. 这里把2^j分成两部分, 前2^(j-1)和 后2^(j-1)个, 然后递推的维护. 感觉树链剖分也可以做, 不知道会不会TLE, 树链剖分的话 线段树的每个点维护10个值, 每次合并就行了. #includ

Codeforces Round #460 (Div. 2) 919A. Supermarket 919B. Perfect Number 919C. Seat Arrangements

这场cf有点意思,hack场,C题等于1的特判hack很多人(我hack成功3个人,上分了,哈哈哈,咳咳...) D题好像是树形dp,E题好像是中国剩余定理,F题好像还是dp,具体的不清楚,最近dp的题目好多,一会滚去学dp. 写A,B,C的题解. A. Supermarket time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output