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

  详细的莫队可以百度学一下。

  这里讲一下怎么样通过O(1)来修改

  1)我们需要求前缀xor。

  2)[L, R] -> [L, R+1]:这里添加了一个aR+1 , 就可以通过前缀xor T,所以我们需要找 T ^ k  H 的个数(因为H^T=K-> H=T^K)。

      如果H在前面出现过,比如(全部是前缀xor)1 2 1[加入]。这时xor 为1 ,我们当k为0, 那么 0^1 = 1的个数就为1。

      这里的1的数量有什么意义。如果我们要想 1 [2 1] 这一段的xor ,是不是应该  T3(1)xor T1(1) = 0 = k;

    [L, R] -> [L, R-1] : 这里是删除了一个a, num[TaR] --, 我们要求的是 k^ TaR 的个数 。

      1 2 1 1[删除], k = 0, 0^1 = 1 , 这个有2个1。这里2个的意思 1 [2 1 1] ( T4 xor T1 = 0)和 1 2 1 [1]  (T4 xor T3 = 0),因为删去了aR 所以aR 结尾的区间就要减去。

    [L, R] -> [L-1, R] : 这个是增加多一个 aL-1  。就是在[L-1, R] 这个里面找 [L-1, r(r<R)] 使得xor 为k , 所以就需要 k^((L-1)-1)。

    [L, R] -> [L+1, R] :这里是删除了 aL 。就是在 [L, r(r<R)] 找 xor 为 k 的 。T[r]^T[L-1] = k。

      num[0] = 1 。这里是应为一开始前缀异或为0 .

 

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
using namespace std;
typedef long long LL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
const int INF = 0x7fffffff;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+;
const int maxn = +;
int a[maxn];
LL num[];
LL x[maxn];
LL ans[maxn];
struct Point
{
int l, r, id;
}node[maxn];
int unit, n, m, k;
void init() {
ms(a, );
ms(ans, );
ms(num, );
ms(x, );
}
bool cmp(Point x1, Point x2)
{
if(x1.l/unit != x2.l/unit){
return x1.l/unit < x2.l/unit;
}
else
return x1.r < x2.r;
}
void work()
{
for(int i = ;i<=n;i++)
x[i] = x[i-]^a[i]; int L, R;
LL temp = ;
L = , R = ;
num[]++;
for(int i = ;i<m;i++){
while(R<node[i].r){
R++;
temp+=num[k^x[R]];
num[x[R]]++;
}
while(R>node[i].r){
num[x[R]]--;
temp-=num[x[R]^k];
R--;
}
while(L>node[i].l){
L--;
temp+=num[k^x[L-]];
num[x[L-]]++;
}
while(L<node[i].l){
num[x[L-]]--;
temp-=num[x[L-]^k];
L++;
}
ans[node[i].id] = temp;
}
}
void solve() {
scanf("%d%d%d", &n, &m, &k);
unit = (int)sqrt(n);
for(int i = ;i<=n;i++) scanf("%d", &a[i]);
for(int i = ;i<m;i++){
node[i].id = i;
scanf("%d%d", &node[i].l, &node[i].r);
}
sort(node, node+m, cmp);
work();
for(int i = ;i<m;i++){
printf("%lld\n", ans[i]);
}
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
// ios::sync_with_stdio(0);
// cin.tie(0); init();
solve(); return ;
}

codeforces 617 E. XOR and Favorite Number(莫队算法)的更多相关文章

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

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

  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. Codeforces617 E . XOR and Favorite Number(莫队算法)

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

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

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

  6. Codeforces 617E XOR and Favorite Number莫队

    http://codeforces.com/contest/617/problem/E 题意:给出q个查询,每次询问区间内连续异或值为k的有几种情况. 思路:没有区间修改,而且扩展端点,减小端点在前缀 ...

  7. Codeforces 617 E. XOR and Favorite Number

    题目链接:http://codeforces.com/problemset/problem/617/E 一看这种区间查询的题目,考虑一下莫队. 如何${O(1)}$的修改和查询呢? 令${f(i,j) ...

  8. CODEFORCES 340 XOR and Favorite Number 莫队模板题

    原来我直接学的是假的莫队 原题: Bob has a favorite number k and ai of length n. Now he asks you to answer m queries ...

  9. E. XOR and Favorite Number 莫队 2038: [2009国家集训队]小Z的袜子(hose)

    一直都说学莫队,直到现在才学,训练的时候就跪了   T_T,其实挺简单的感觉.其实训练的时候也看懂了,一知半解,就想着先敲.(其实这样是不好的,应该弄懂再敲,以后要养成这个习惯) 前缀异或也很快想出来 ...

随机推荐

  1. TensorFlow学习笔记11-开始用TensorFlow

    TensorFlow运作方式 要用到的代码都在Github上.当然,如果你本地装了TensorFlow,也可以用Everything直接搜索以下文件: mnist.py fully_connected ...

  2. sql中的sp_helptext、sp_help 、sp_depends

    sp_help:用于显示参数清单和其数据类型. sp_depends:用于显示存储过程依据的对象或者依据存储过程的对象. sp_helptext:用于显示存储过程的定义文本

  3. 从头到尾说一次 Java 垃圾回收,写得非常好!

    Java技术栈 www.javastack.cn 优秀的Java技术公众号 作者:聂晓龙(花名:率鸽),阿里巴巴高级开发工程 ⬆️ 图片来源于网络 之前上学的时候有这个一个梗,说在食堂里吃饭,吃完把餐 ...

  4. display:table的几个用法 块级子元素垂直居中

    DIV+CSS的布局已经让表格布局几乎很少用到,除非表格语义性很强的情况. display:table解决了一部分需要使用表格特性但又不需要表格语义的情况, 尤其是DIV+CSS很不方便解决的问题,比 ...

  5. Redis 和 MongoDB 的优缺点??

    MongoDB 和 Redis 都是 NoSQL,采用结构型数据存储.二者在使用场景中,存在一定的区别, 这也主要由于二者在内存映射的处理过程,持久化的处理方法不同.MongoDB 建议集群部署,更多 ...

  6. 创建带标签页的MDI WinForms应用程序

    http://www.cnblogs.com/island/archive/2008/12/02/mditab.html 创建MDI应用程序 先创建”Windows窗体应用程序”解决方案Tabable ...

  7. [书接上一回]在Oracle Enterprise Linux (v5.7) 中安装DB - (3/4)

    安装p10404530_112030_Linux-x86-64_6of7.zip解压下的example. 修改软件路径,为dbhome_1. 安装好数据,则可以进行快照操作! 删除安装文件. 输入db ...

  8. Object of type 'ndarray' is not JSON serializable

    Object of type 'ndarray' is not JSON serializable import numpy as np import json arr=np.asarray([345 ...

  9. 2018-8-10-C#-ValueTuple-原理

    title author date CreateTime categories C# ValueTuple 原理 lindexi 2018-08-10 19:16:52 +0800 2018-2-13 ...

  10. Java虚拟机——Class类文件结构

    Class文件格式采用一种类似C语言结构体的结构来存储数据,这种数据结构只有两种数据类型:无符号数和表.      无符号数属于基本的数据类型,数据项的不同长度分别用u1, u2, u4, u8表示, ...