题目链接: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 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.

题意:

给出一个序列,作m此查询,每次查询的内容为:在区间[l, r]内,有多少个子区间的异或和为k?

题解:

莫队算法:解决区间询问的离线方法,时间复杂度:O(n^1.5)。

代码如下:

 #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = 1e5+; int n, m, k, w, a[maxn];
LL sum, ans[maxn], c[];
//a[i]为前缀异或和,c[i]为在当前区间内,前缀异或和(从1开始)为i的个数。
//可知:a[l-1]^a[r] = val[l]^val[l+1]^………^val[r] struct node
{
int l, r, id;
bool operator<(const node &x)const{
if(l/w==x.l/w) return r<x.r;
return l/w<x.l/w;
}
}q[maxn]; void del(int i)
{
c[a[i]]--;
sum -= c[a[i]^k];
} void add(int i)
{
sum += c[a[i]^k];
c[a[i]]++;
} int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i = ; i<=n; i++)
{
scanf("%d",&a[i]);
a[i] ^= a[i-];
}
for(int i = ; i<=m; i++)
{
scanf("%d%d",&q[i].l,&q[i].r);
q[i].id = i;
} w = sqrt(n);
sort(q+,q++m); int L = , R = ;
c[] = , sum = ;
for(int i = ; i<=m; i++)
{
while(L<q[i].l) del(L-), L++;
while(L>q[i].l) L--, add(L-);
while(R<q[i].r) R++, add(R);
while(R>q[i].r) del(R), R--;
ans[q[i].id] = sum;
} for(int i = ; i<=m; i++)
printf("%lld\n",ans[i]);
return ;
}

Codeforces Round #340 (Div. 2) E. XOR and Favorite Number —— 莫队算法的更多相关文章

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

    E. XOR and Favorite Number 题目连接: http://www.codeforces.com/contest/617/problem/E Descriptionww.co Bo ...

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

    #include<bits/stdc++.h> using namespace std; <<; struct node{ int l,r; int id; }q[N]; in ...

  3. 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 ...

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

    题目链接:http://codeforces.com/contest/617/problem/E 题目大意:有n个数和m次查询,每次查询区间[l, r]问满足ai ^ ai+1 ^ ... ^ aj ...

  5. Codeforces Round #340 (Div. 2) E. XOR and Favorite Number

    time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standa ...

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

    题目链接: E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes i ...

  7. Codeforces617 E . XOR and Favorite Number(莫队算法)

    XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...

  8. CodeForces - 617E XOR and Favorite Number 莫队算法

    https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry,  问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...

  9. [Codeforces Round #340 (Div. 2)]

    [Codeforces Round #340 (Div. 2)] vp了一场cf..(打不了深夜的场啊!!) A.Elephant 水题,直接贪心,能用5步走5步. B.Chocolate 乘法原理计 ...

随机推荐

  1. va_list 简介

    原文:http://blog.sina.com.cn/s/blog_590be5290100qhxr.html va_list是一个宏,由va_start和va_end界定. typedef char ...

  2. Xamarin.Forms的基本页面和基本视图

    Xamarin.Forms的基本页面和基本视图   在Xamarin.Forms中,每个App的界面都是一个页面Page.页面的种类有很多种.其中,最常见的页面就是内容页面ContentPage.项目 ...

  3. VMware虚拟机直连物理网络的两种方式

    VMware虚拟机直连物理网络的两种方式   使用VMware构建虚拟机,通常虚拟机都使用NAT模式.这时,虚拟机有独立的网段.使用NAT模式,虚拟机之间数据都通过虚拟网络传输,不会影响实体机所在的实 ...

  4. java布局(每个名字都是有意义的)

    一.FlowLayout 1.流水布局:从左至右,排满换行 2.构造函数有三种: (1)FlowLayout() (2)FlowLayout(align) (3)FlowLayout(align, h ...

  5. BZOJ1014火星人prefix Splay維護序列 + 字符串哈希

    @[Splay, 哈希] Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:\(madamimadam\), 我们将这个字符串的各个字符予以标号 ...

  6. [NSThread sleepForTimeInterval:3.0];

    在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)laun ...

  7. 【spring mvc】后台的API,测试中,总提示接口实体的某一个字段不能为null,但是明明给值了还提示不能为空

    实体是这三个字段 接口的实现类Controller 前台测试给值 依旧报错 解决方法: 需要添加@RequestBody注解

  8. Go语言_RPC_Go语言的RPC

    一 标准库的RPC RPC(Remote Procedure Call,远程过程调用)是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络细节的应用程序通信协议.简单的说就是要像调用本地函数 ...

  9. Qt中QVector与QList的应用

    首先來看看QVector 的基本使用方式,建立一個可容納兩個元素的QVector ,並使用索引方式存取元素值:QVector<double> vect(2); vect[0] = 1.0; ...

  10. CMake使用hellocmake&&make的使用

    2016-12-11   20:38:32 已经知道cmake这个东西很长的时间了,一直没有试验过,知道它是一个编译工具,在opencv和Linux下都有makefile的内容.感觉现在对源码的编译有 ...