题目链接: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. 浅谈矩阵加速——以时间复杂度为O(log n)的算法实现裴波那契数列第n项及前n之和使用矩阵加速法的优化求法

    首先请连矩阵乘法乘法都还没有了解的同学简单看一下这篇博客: https://blog.csdn.net/weixin_44049566/article/details/88945949 首先直接暴力求 ...

  2. 剑指offer--day03

    1.1题目:斐波那契数列:大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0).n<=39 1.2解题思路:斐波那契数列公式为: 这道题递归很好写 ...

  3. Linux 与 Unix 到底有啥区别和联系?

    原文:https://opensource.com/article/18/5/differences-between-linux-and-unix 来源:开源中国社区,译者:Tocy, LinuxTe ...

  4. Lpl and Energy-saving Lamps

    During tea-drinking, princess, amongst other things, asked why has such a good-natured and cute Drag ...

  5. 【CF321E】+【bzoj5311】贞鱼

    决策单调性 + WQS二分 我们首先列出转移式: \(f[i]=Min(f[j]+Sum[j+1 , i])\) 首先我们考虑如果让一段区间的小鱼在一起的代价怎么预处理,我们可以对于一个上三角矩阵求个 ...

  6. Ftp客户端(上传文件)

    #coding=utf-8 import os import socket import hashlib import json # client = socket.socket() #申明socke ...

  7. C# 图片与Base64的相互转化

    public ActionResult UploadSignature2(string src_data) { Class1.Base64StrToImage(src_data, "C:\\ ...

  8. Math对象(min()-max()-ceil()-floor()-round()和abs())

    1.Math.min() 功能:求一组数的最小值,返回值:Number 如果里面有非数字则返回NaN 2.Math.max() 功能:求一组数的最大值,返回值:Number 3.Math.ceil() ...

  9. [转载]浅谈JavaScript函数重载

     原文地址:浅谈JavaScript函数重载 作者:ChessZhang 上个星期四下午,接到了网易的视频面试(前端实习生第二轮技术面试).面了一个多小时,自我感觉面试得很糟糕的,因为问到的很多问题都 ...

  10. .net Console.ReadLine无效

    代码中出现了 Console.ReadLine无效解决办法:把应用程序的输出类型改为 控制台应用程序