CodeForces - 617E XOR and Favorite Number (莫队+前缀和)
Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is given by a pair li and ri and asks you to count the number of pairs of integers i and j, such that l ≤ i ≤ j ≤ r and the xor of the numbers ai, ai + 1, ..., ajis equal to k.
Input
The first line of the input contains integers n, m and k (1 ≤ n, m ≤ 100 000, 0 ≤ k ≤ 1 000 000) — the length of the array, the number of queries and Bob's favorite number respectively.
The second line contains n integers ai (0 ≤ ai ≤ 1 000 000) — Bob's array.
Then m lines follow. The i-th line contains integers li and ri (1 ≤ li ≤ ri ≤ n) — the parameters of the i-th query.
Output
Print m lines, answer the queries in the order they appear in the input.
Examples
6 2 31 2 1 1 0 31 63 5
70
5 3 11 1 1 1 11 52 41 3
944 题意:询问区间内异或和刚好为k的字段个数。思路:莫队+前缀和。这个前缀和比较套路,用的是前缀异或和。字段【l,r】的异或和就是pre[r]^pre[l-1],这种情况下我们在莫队的过程中记录l,r的pre[i]出现的次数,就可以完成更新了。 注意当L<q[i].l时,要先让记录per[L]出现次数的个数减一。
#include<iostream> #include<algorithm> #include<vector> #include<stack> #include<queue> #include<map> #include<set> #include<cstdio> #include<cstring> #include<cmath> #include<ctime> #define fuck(x) cout<<#x<<" = "<<x<<endl; #define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl; #define ls (t<<1) #define rs ((t<<1)+1) using namespace std; typedef long long ll; typedef unsigned long long ull; const int maxn = 100086; const int maxm = 1000086; const int inf = 2.1e9; const ll Inf = 999999999999999999; const int mod = 1000000007; const double eps = 1e-6; const double pi = acos(-1); int num[maxm*2],pre[maxn],a[maxn];struct node{int l,r;int id; }q[maxn]; ll ans[maxn]; ll anss; int block;bool cmp(node a,node b){if(a.l/block!=b.l/block){return a.l<b.l;}return a.r<b.r; }int main() {int n,m,k;scanf("%d%d%d",&n,&m,&k);for(int i=1;i<=n;i++){scanf("%d",&a[i]);pre[i]=pre[i-1]^a[i];}block=sqrt(n);for(int i=1;i<=m;i++){scanf("%d%d",&q[i].l,&q[i].r);q[i].id=i;}sort(q+1,q+1+m,cmp);int L=1,R=0;anss=0;num[0]=1;for(int i=1;i<=m;i++){while(L<q[i].l){int t=k^pre[L-1];num[pre[L-1]]--;///注意语句顺序anss-=num[t];L++;}while(R>q[i].r){int t=k^pre[R];num[pre[R]]--;anss-=num[t];R--;}while(L>q[i].l){L--;int t=k^pre[L-1];anss+=num[t];num[pre[L-1]]++;}while(R<q[i].r){R++;int t=k^pre[R];anss+=num[t];num[pre[R]]++;}ans[q[i].id]=anss;}for(int i=1;i<=m;i++){printf("%lld\n",ans[i]);}return 0; }
View Code
转载于:https://www.cnblogs.com/ZGQblogs/p/10863851.html
CodeForces - 617E XOR and Favorite Number (莫队+前缀和)相关推荐
- codeforces 617E XOR and Favorite Number 莫队
https://vjudge.net/problem/CodeForces-617E 题目大意:给nnn个数,mmm个询问,以及一个数kkk,每次询问要输出[l,r][l,r][l,r]内满足a[i] ...
- CodeForces - 617E XOR and Favorite Number(莫队)
题目链接:点击查看 题目大意:给出一个由n个数组成的数列,现在给出m组询问,每次询问包含一个l和一个r,要求回答在闭区间[l,r]中有多少组(i,j)满足[i,j]闭区间内的所有数的异或和等于k 题目 ...
- CodeForces -617E XOR and Favorite Number(莫队)
题目链接:点击这里 题目大意: 给定一个长度为 nnn 的序列 a1,a2,...,ana_1,a_2,...,a_na1,a2,...,an ,再给出一个数字 kkk , mmm 组询问每组询 ...
- Codeforces 617E XOR and Favorite Number
Discription Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Eac ...
- 莫队算法(普通莫队、带修莫队、树上莫队、不删除莫队)学习笔记【理解+套路/核心代码+例题及题解】
一.理解 我的理解就是巧妙的暴力,利用双指针以及分块思想,巧妙的移动双指针,时间复杂度可以达到O(NlogN). 强推博客:写的又好又全.链接 二.套路 1.普通莫队 [1]核心代码 bool cmp ...
- Codeforces617E XOR and Favorite Number (普通莫队)
题目链接: XOR and Favorite Number 2021.11.11更新了解题思路和代码 大致题意 给定一个长度为nnn的序列aaa, 和一个固定的数值kkk. 有mmm次询问, 每次询问 ...
- XOR and Favorite Number CF340E 莫队算法
题目链接 题意:求给定询问区间[L,R]问有多少连续区间异或值等k,多次询问可以离线. a[i]^a[i+1]^a[i+2]^a[n]=(a[1]^a[2]^a[3]^...^a[i-1])^(a[1 ...
- CodeForces 617 E. XOR and Favorite Number
题目链接: CodeForces 617 E. XOR and Favorite Number 题目描述: 给出n个数,m次查询,每次查询在区间[l, r]里面有多少对(i, j),满足ai ^ ai ...
- CF 617E 莫队
CF 617E 题目链接: http://codeforces.com/problemset/problem/617/E 题意: 问[L,R]区间内,有多少个子区间,它们里元素的异或和为k. 思路: ...
最新文章
- Eclipse相关问题总结
- 打印系列一:(套打解决方案(支持B/S和C/S))
- 电信诈骗?一招让骗子血本无归!
- android如何不用系统签名,更新Android系统应用程序,带/不带平台签名
- Leetcode每日一题:514.freedom-trail(自由之路)
- 21天Jmeter打卡Day12 逻辑控制器之循环控制器
- linux下默认分区system的id,Linux学习笔记之系统中的分区和文件系统
- C语言解决约瑟夫环问题
- 如何修改SAO用户密码
- Siamese系列跟踪网络之SiamFC、SiamRPN、DaSiamRPN、SiamRPN++、SiamMask
- [算法·思想]囚徒困境中Tit For Tat策略为什么就是最好的,之JAVA代码模拟实现
- AI 工程师进阶路线
- Sparkling Logic SMARTS 实时决策分析模块介绍
- Mybatis逆向工程同名表找错数据库的问题
- 石墨文档支持的几种markdown格式
- BlueHost和SiteGround美国主机商对比评测
- 好用的十六进制编辑工具010 Editor
- 基于有源钳位三电平的有源电力滤波器(ANPC-APF)MATLAB仿真,包括自建的DSOGI锁相模块和PQ谐波检测模块
- while循环+无限循环.py
- 灵汐科技携类脑芯片KA200及类脑系列产品亮相国家“十三五”科技创新成就展
热门文章
- callbackend entry point - iwfndcl_mgw_request_manager~read_entity
- Kubernetes里ingress配置的一些例子
- SAP S/4HANA CDS View的访问控制实现:DCL介绍 1
- ABAP Development Tools的语法高亮实现原理
- 在SAP UI中使用纯JavaScript显示产品主数据的3D模型视图
- Netweaver和CloudFoundry里的trace开关
- antd 获取table选中行数据_Excel数据匹配:函数法+PQ法+PB法
- php网站搭建致命错误203终止,PHP致命错误:无法重新分配$this
- oracle一个月内的数据,oracle 查询前一小时、一天、一个月、一年的数据
- 线程id 获取线程名称_016 线程及初步网络编程