题目链接: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. React-onsenui之RouterNavigator组件解读

    var index = 1;// index的最外层初始值,亦是全局 var MyPage = React.createClass({ //构成工具栏组件,根据hasBackButton的值为back ...

  2. Noi2018 归途

    zz:https://blog.csdn.net/dreaming__ldx/article/details/81106748 以海拔为第一关键字对边进行从大到小的排序,然后修建kruskal重构树, ...

  3. 数模常用算法系列Matlab实现-----线性规划

    线性规划的 Matlab 标准形式 线性规划的目标函数可以是求最大值,也可以是求最小值,约束条件的不等号可以是小于号也可以是大于号.为了避免这种形式多样性带来的不便,Matlab 中规定线性 规划的标 ...

  4. mooc-IDEA 高效定位代码--004

    十.IntelliJ IDEA -高效定位代码-精准搜索 1.快速定位类:Navigate->Class...   [Ctrl+N] 2.文件:Navigate->File..   [Ct ...

  5. xshell输入字母空格间距变大

    按一下shift+空格(全角/半角转换的快捷键,引起的问题)

  6. css-sprite 雪碧图的使用,合并多张小图,背景图片当按钮的设置

    背景图片基础: 使用background-image来设置背景图片 语法: background-image:url(相对与css的路径) 如果背景图片大于元素,默认会显示图片的左上角 如果背景图片和 ...

  7. Java学习day2关键字

    java的基本语法(1) 一.关键字 定义:被Java语言赋予特殊含义,用做专门用途的字符串 特点:关键字中的所有字母都为小写 二.标识符 定义:java对各种变量.方法和类等要素命名时所使用的的字符 ...

  8. Golang环境配置

    下载 下载地址 Go官网下载地址:https://golang.org/dl/ Go官方镜像站(推荐):https://golang.google.cn/dl/ 版本选择 安装 Windows安装 示 ...

  9. 固定导航栏(jquery)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  10. Thymeleaf简介

    Thymeleaf Thymeleaf简介 Thymeleaf是一个流行的模板引擎,该模板引擎采用Java语言开发,模板引擎是一个技术名词,是跨领域跨平台的概念,在Java语言体系下有模板引擎,在C# ...