XOR (莫队)
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 |
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 (莫队)的更多相关文章
- Codeforces617 E . XOR and Favorite Number(莫队算法)
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...
- 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 ...
- 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 ...
- XOR and Favorite Number(莫队算法+分块)
E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- CodeForces - 617E XOR and Favorite Number 莫队算法
https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry, 问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...
- 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 ...
- XOR Queries(莫队+trie)
题目链接: XOR Queries 给出一个长度为nn的数组CC,回答mm个形式为(L, R, A, B)(L,R,A,B)的询问,含义为存在多少个不同的数组下标k \in [L, R]k∈[L,R] ...
- 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 ...
- 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 ...
随机推荐
- JS与ajax遍历list
示例: <script> <c:forEach var="yanan" items="${yananList}"> ...
- Redis Crackit漏洞防护
Redis Crackit漏洞利用和防护 注意:本文只是阐述该漏洞的利用方式和如何预防.根据职业道德和<中华人民共和国计算机信息系统安全保护条例>,如果发现的别人的漏洞,千万不要轻易入侵, ...
- 结合find和cp批量查找文件并复制到指定文件夹中
find . -name JA1_*001_027 | xargs -i cp {} F:/ 说明: . 表示当前文件夹及其子文件夹中查找 -name 指定待查找文件,可以使用通配符 F:/ 表示 ...
- 小谈ConcurrentHashMap
面试的时候被面试官问了点相关知识,再次记录一些自己的总结 一. 1.HashTable也可实现线程安全,但是它是用synchronized实现的,所以其他线程访问HashTable的同步方法时,可能会 ...
- ulua c#调用lua中模拟的类成员函数
项目使用ulua,我神烦这个东西.lua单纯在lua环境使用还好,一旦要跟外界交互,各种月经不调就来了.要记住贼多的细节,你才能稍微处理好.一个破栈,pop来push去,位置一会在-1,一会在-3,2 ...
- remap.config文件配置模板
# # URL Remapping Config File # # Using remap.config allows you to accomplish two things: # # 1) Rew ...
- Numpy基础学习
Numpy(Numerical Python的简称)是高性能科学计算和数据分析的基础包. 主要的功能: 1.ndarray,一个具有矢量运算和复杂广播工能的快速且节省空间的多维数组 2.用于对整组数据 ...
- 《Thinking in Java》学习笔记(六)
1.Class相关知识 Class类可以理解为类的图纸,通过Class类可以分析类的结构.构建出类的实例. Class.forName("test.TestClass").newI ...
- 前端系列之JavaScript基础知识概述
微信公众号:compassblog 欢迎关注,欢迎转发,互相学习,共同进步! 有任何问题,请后台留言联系! 1.什么是JavaScript (1).JavaScript是web上一种功能强大的编程语 ...
- Centos7搭建FTP服务器
从网上搜索了好多搭建Centos7搭建服务器的教程都没有成功唯独这个,利用Windows资源管理器连接测试成功. 一.通过yum安装vsftpd yum install -y vsftpd 二.修改v ...