[CF617E]XOR and Favorite Number/[CQOI2018]异或序列
题目大意:
给定一个长度为$n(n\leq10^5)$的数列$A$和数$k$$(A_i,k\leq10^6)$。$m$组询问,每次询问区间$[l,r]$中有多少对$i,j(l\leq i\leq j\leq r)$,满足$\oplus_{x=i}^jA_x=k$。
思路:
莫队。
首先求出数列$A$的前缀异或和$pre[i]$。每次加入一个数$x$时,$cnt[x]++$,$ans+=cnt[x\oplus k]$。删除同理。
注意区间的范围,因为$\oplus_{x=i}^jA_x=pre[j]\oplus pre[i-1]$,所以对于询问$[l,r]$,实际维护的区间应该是$[l-1,r]$。
#include<cmath>
#include<cstdio>
#include<cctype>
#include<algorithm>
typedef long long int64;
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=,M=,logK=;
int k,a[N],bel[N],cnt[<<logK];
int64 ans[M],tmp;
struct Query {
int l,r,id;
bool operator < (const Query &another) const {
return bel[l]<bel[another.l]||(bel[l]==bel[another.l]&&bel[r]<bel[another.r]);
}
};
Query q[M];
inline void ins(const int &x) {
tmp+=cnt[a[x]^k];
cnt[a[x]]++;
}
inline void del(const int &x) {
cnt[a[x]]--;
tmp-=cnt[a[x]^k];
}
int main() {
const int n=getint(),m=getint(),block=sqrt(n);
k=getint();
for(register int i=;i<=n;i++) {
a[i]=a[i-]^getint();
bel[i]=i/block;
}
for(register int i=;i<m;i++) {
const int l=getint()-,r=getint();
q[i]=(Query){l,r,i};
}
std::sort(&q[],&q[m]);
for(register int i=,l=,r=-;i<m;i++) {
while(r<q[i].r) ins(++r);
while(l>q[i].l) ins(--l);
while(r>q[i].r) del(r--);
while(l<q[i].l) del(l++);
ans[q[i].id]=tmp;
}
for(register int i=;i<m;i++) {
printf("%lld\n",ans[i]);
}
return ;
}
[CF617E]XOR and Favorite Number/[CQOI2018]异或序列的更多相关文章
- CF617E XOR and Favorite Number
CF617E XOR and Favorite Number 已知一个序列 \(a_1,\ a_2,\ \cdots,\ a_n\) 和 \(k\) ,\(m\) 次询问给出 \(l,\ r\) ,求 ...
- 题解-CF617E XOR and Favorite Number
题面 CF617E XOR and Favorite Number 给定 \(n,m,k\) 和 \(n\) 个数的序列 \(a_i\),\(m\) 次求区间 \([l,r]\) 中异或值为 \(k\ ...
- bzoj 5301 [Cqoi2018]异或序列 莫队
5301: [Cqoi2018]异或序列 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 204 Solved: 155[Submit][Status ...
- bzoj 5301: [Cqoi2018]异或序列 (莫队算法)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=5301 题面; 5301: [Cqoi2018]异或序列 Time Limit: 10 Sec ...
- 「luogu4462」[CQOI2018] 异或序列
「luogu4462」[CQOI2018]异或序列 一句话题意 输入 \(n\) 个数,给定\(k\),共 \(m\) 组询问,输出第 \(i\) 组询问 \(l_i\) \(r_i\) 中有多少个连 ...
- BZOJ5301: [Cqoi2018]异或序列(莫队)
5301: [Cqoi2018]异或序列 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 400 Solved: 291[Submit][Status ...
- [bzoj5301][Cqoi2018]异或序列_莫队
异或序列 bzoj-5301 Cqoi-2018 题目大意:题目链接. 注释:略. 想法: 由于a^a=0这个性质,我们将所有的数变成异或前缀和. 所求就变成了求所有的$l_i\le x<y\l ...
- XOR and Favorite Number Codeforces - 617E || [CQOI2018]异或序列
https://www.luogu.org/problemnew/show/P4462 http://codeforces.com/problemset/problem/617/E 这个是莫队裸题了吧 ...
- BZOJ_5301_[Cqoi2018]异或序列&&CF617E_莫队
Description 已知一个长度为 n 的整数数列 a[1],a[2],…,a[n] ,给定查询参数 l.r ,问在 [l,r] 区间内,有多少连续子 序列满足异或和等于 k . 也就是说,对于所 ...
随机推荐
- lshw
https://linux.die.net/man/1/lshw lshw(Hardware Lister)是另外一个可以查看硬件信息的工具,不仅如此,它还可以用来做一些硬件的benchmark. 这 ...
- iOS如何隐藏状态栏,包括网络标志、时间标志、电池标志等
我们在开发App的时候,在遇到有状态栏时,NavigationBar上面的按钮,是极难点击的,所以这个时候,最好我们能够人为的隐藏掉状态栏. 如果一直需要隐藏的话,直接在info.plist里面,添加 ...
- FreeBSD NTP 简单使用
FreeBSD NTP 简单使用 来源 https://blog.csdn.net/stevexk/article/details/1349506 1.ntptrace xxx.xxx.xxx.xxx ...
- [洛谷P3978][TJOI2015]概率论
题目大意:对于一棵随机生成的$n$个结点的有根二叉树,所有不同构的形态等概率出现(这里同构当且仅当两棵二叉树根相同,并且相同节点的左儿子和右儿子都相同),求叶子节点个数的期望是多少? 题解:令$f_n ...
- [bzoj3456] 城市规划 [递推+多项式求逆]
题面 bzoj权限题面 离线题面 思路 orz Miskcoo ! 先考虑怎么算这个图的数量 设$f(i)$表示$i$个点的联通有标号无向图个数,$g(i)$表示$n$个点的有标号无向图个数(可以不连 ...
- BZOJ4652 [Noi2016]循环之美 【数论 + 莫比乌斯反演 + 杜教筛】
题目链接 BZOJ 题解 orz 此题太优美了 我们令\(\frac{x}{y}\)为最简分数,则\(x \perp y\)即,\(gcd(x,y) = 1\) 先不管\(k\)进制,我们知道\(10 ...
- 【CZY选讲·棋盘迷宫】
题目描述 一个N*M的棋盘,’.’表示可以通过,’#’表示不能通过,给出Q个询问,给定起点和终点,判断两点是否联通,如联通输出“Yes”,否则输出“No”. 数据范围 N,M <=500,Q ...
- MyBatis原理分析
MyBatis原理分析 参考博客: 深入理解mybatis原理: http://blog.csdn.net/luanlouis/article/details/40422941 一 . JDBC的 ...
- UVa10891 Game of Sum
给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能从一端选取.两人都采用最优策略,A先手,问A和B各自得到数字的和的差值最大为多少? 区间DP F[i][j]表示区间i~j内A能得到的最大数 ...
- zepto.js 自定义打包集成其他模块构建流程
1.首先在自己的电脑上要安装Node.js和npm包管理工具: 2.从github上下载zepto.js的源文件包到本地磁盘(例如:E:\Learning\JS): 地址:https://github ...