Time Limit: 2000 ms   Memory Limit: 256 MB

Description

  给定一个含有n个整数的序列 a1, a2,..., an.

  定义 f(x,x) = a[x], f(x,y) = a[x] xor a[x + 1] xor ... xor a[y] (y > x).

  本题设有m组询问,每组询问含有两个参数 (l, r) 。对于每组询问,你需要回答有多少个二元组 (x, y) 满足 l <= x <= y <= r 并且 f(x, y) = k.

Input

  第一行有3个整数, n, m, k(1 <= n <= 100000, 1 <= m <= 100000, 0 <= k < 10^6).
  第二行共有 n 个非负整数代表整个序列,每个整数均不超过 10^6.
  接下来m行,每行两个整数 (li, ri), li <= ri

Output

  对于每组询问,输出一行表示有多少满足上述条件的二元组。

Sample Input

Sample Output

5 2 1
1 0 1 1 0
1 2
2 5

2

4

Hint 

  对于10%的数据,$n, m \leq 500$

  对于30%的数据,$n, m \leq 3000$

  对于50%的数据,$n, m \leq 30000$

  对于100%的数据,$n, m \leq 100000$


题解:

  一看范围就知道是标准$n\sqrt{n}$莫队啊,是我今天感冒脑抽了想不出这么简单的处理吗......

  

  $[l,r]$异或起来的值刚好等于$k$,维护异或前缀和$a$后,等价于判断$a_{l-1}\hat{} a_{r}$是否等于$k$。

  那么用莫队在这个异或前缀和数组上爬。

  维护莫队中统计每种值出现次数的数组$cnt$,$cnt_i$表示值为$i$的有多少。

  这样一来,加入一位$x$对莫队的影响就是$ans+=cnt_{a[x]\hat{} k}$,删去一位对莫队的影响就是$ans-=cnt_{a[x]\hat{} k}$

  当然还要维护$cnt$,加入时先统计影响,再将$cnt_{a[x]}++$;删除时先从$cnt$里抹掉:$cnt_{a[x]}--$,再统计影响。

Tips:

  1.原本$[l,r]$的询问,转换后最大要考虑到$[l-1,r]$,所以把询问的左端点都-1.

  2.异或后值可能大于1000000,需多开一倍。


 #include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N=;
int n,m,k,di,a[N],cnt[];
ll now,out[N];
struct Query{
int l,r,id;
friend bool operator < (Query x,Query y){
x.l++; y.l++;
if(x.l/di!=y.l/di) return x.l/di<y.l/di;
return x.r<y.r;
}
}q[N];
void add(int x){
now+=cnt[a[x]^k];
cnt[a[x]]++;
}
void dec(int x){
cnt[a[x]]--;
now-=cnt[a[x]^k];
}
int main(){
scanf("%d%d%d",&n,&m,&k);
di=(int)sqrt(n);
for(int i=,x;i<=n;i++){
scanf("%d",&x);
a[i]=a[i-]^x;
}
for(int i=;i<=m;i++){
scanf("%d%d",&q[i].l,&q[i].r);
q[i].l--; q[i].id=i;
}
sort(q+,q++m);
int l=,r=;
cnt[a[]]=;
for(int i=;i<=m;i++){
while(r<q[i].r) add(++r);
while(r>q[i].r) dec(r--);
while(l<q[i].l) dec(l++);
while(l>q[i].l) add(--l);
out[q[i].id]=now;
}
for(int i=;i<=m;i++) printf("%lld\n",out[i]);
return ;
}

奇妙代码

XOR (莫队)的更多相关文章

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

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

  2. Codeforeces 617E XOR and Favorite Number(莫队+小技巧)

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

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

  4. XOR and Favorite Number(莫队算法+分块)

    E. 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 340 XOR and Favorite Number 莫队模板题

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

  7. XOR Queries(莫队+trie)

    题目链接: XOR Queries 给出一个长度为nn的数组CC,回答mm个形式为(L, R, A, B)(L,R,A,B)的询问,含义为存在多少个不同的数组下标k \in [L, R]k∈[L,R] ...

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

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

随机推荐

  1. 四、Html常用标签

    1,列表相关标签 <ul>:定义无序列表,只能包含<li>子元素 <ol>:定义有序列表,只能包含<li>子元素,因为这个标签是有序的,所有这个标签还有 ...

  2. PHP如何读取json数据

    1的 <?php $json = '{"a":1,"b":2,"c":3,"d":4,"e": ...

  3. Lambda表达式详解 (转)

    前言 1.天真热,程序员活着不易,星期天,也要顶着火辣辣的太阳,总结这些东西. 2.夸夸lambda吧:简化了匿名委托的使用,让你让代码更加简洁,优雅.据说它是微软自C#1.0后新增的最重要的功能之一 ...

  4. Python之Django rest_Framework框架源码分析

    #!/usr/bin/env python # -*- coding:utf-8 -*- from rest_framework.views import APIView from rest_fram ...

  5. iOS项目——自定义UITabBar与布局

    在上一篇文章iOS项目——基本框架搭建中,我们详细说明了如何对TabBarItem的图片属性以及文字属性进行一些自定义配置.但是,很多时候,我们需要修改TabBarItem的图片和文字属性之外,还需要 ...

  6. python3中time模块的用法及说明

    python中,导入time模块使用的命令是 import time 可以使用以下命令查看time模块内置的能够使用的方法: dir(time) 可以使用以下命令查看time模块中每个内置方法的说明: ...

  7. 解决Bug:Size of a request header field exceeds server limit

    用了cms 发现这玩意真不好,老是有各种奇芭的问题跳出来 有时浏览网页时会出现 Bad Request Your browser sent a request that this server cou ...

  8. 01 Java 搭建环境

    刚接触 Java 语言,为了方便以后查阅,将学习过程中遇到的知识点记录下来,作为学习笔记使用.在学习过程中 JDK 使用版本为 1.8.0_73,操作系统为 macOS Sierra 10.12.6. ...

  9. POJ 3348 Cows [凸包 面积]

    Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9022   Accepted: 3992 Description ...

  10. MIT-线性代数笔记(1-6)

    学习目录 第 01 讲 行图像和列图像 第 02 讲 矩阵消元 第 03 讲 矩阵的乘法和逆矩阵 第 04 讲 矩阵的LU 分解 第 05 讲 转置.置换和空间 第 06 讲 列空间和零空间 第 07 ...