【CF 617E】 XOR and Favorite Number (Mo's algorithm)
【CF 617E】 XOR and Favorite Number (Mo's algorithm)
4 seconds
256 megabytes
standard input
standard output
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, ..., aj is equal to k.
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.
Print m lines, answer the queries in the order they appear in the input.
6 2 3 1 2 1 1 0 3 1 6 3 5
7 0
5 3 1 1 1 1 1 1 1 5 2 4 1 3
9 4 4
In the first sample the suitable pairs of i and j for the first query are: (1, 2), (1, 4), (1, 5), (2, 3), (3, 6), (5, 6), (6, 6). Not a single of these pairs is suitable for the second query.
In the second sample xor equals 1 for all subarrays of an odd length.
题目大意是找某个区间内连续异或能得到K的区间数
现在先考虑单组区间查询。对于区间[L,R] 可以求个前缀和pre[i] 表示第1~i的数的异或和。这样[L,R]的异或和即为pre[L-1]^pre[R] 换种思路 预存一下pre[L-1]^k 就可以延长它的生命周期。
这样求[L,R]区间内能异或出k的区间数 从L遍历到R 遍历过程中统计遍历过的前缀异或和的个数 当遍历到i时 pre[i]^k出现的次数即为[L,i]中到i的连续的能异或得到k的区间数
这样如果区间向左或向右就可以O(1)的修改 对于多组查询 就可以上莫队算法了
代码如下:
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout)using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 100100;
const double eps = 1e-8;//L的分块
int pos[msz];
struct Range
{int l,r,id;bool operator < (const struct Range a)const{return pos[l] == pos[a.l]? r < a.r: pos[l] < pos[a.l];}
};Range rg[msz];
int a[msz];
//统计出现过的异或数
LL cnt[1048576];
LL ans[msz];
LL num;
int n,m,k;//移动区间边界时更新
void update(int x,int d)
{if(d < 0)cnt[k^a[x]] +=d;num += d*cnt[a[x]];if(d > 0)cnt[k^a[x]] +=d;
}int main()
{scanf("%d%d%d",&n,&m,&k);int dm = ceil(sqrt(n*1.0));a[0] = 0;a[n+1] = 0;for(int i = 1; i <= n; ++i){scanf("%d",&a[i]);a[i] ^= a[i-1];}for(int i = 0; i < m; ++i){scanf("%d%d",&rg[i].l,&rg[i].r);rg[i].l--;pos[rg[i].l] = rg[i].l/dm;rg[i].id = i;}sort(rg,rg+m);int l = 1,r = 0;num = 0;memset(cnt,0,sizeof(cnt));for(int i = 0; i < m; ++i){int id = rg[i].id;if(rg[i].l == rg[i].r-1){ans[id] = (a[rg[i].r]^a[rg[i].l]) == k;continue;}while(r < rg[i].r) update(++r,1);while(r > rg[i].r) update(r--,-1);while(l < rg[i].l) update(l++,-1);while(l > rg[i].l) update(--l,1);ans[id] = num;}for(int i = 0; i < m; ++i)printf("%lld\n",ans[i]);return 0;
}
【CF 617E】 XOR and Favorite Number (Mo's algorithm)相关推荐
- 【Codec系列】连续消除算法-SEA(Successive Elimination Algorithm)
连续消除算法-SEA(Successive Elimination Algorithm)
- 【Vue学习】—Vue UI组件库(二十八)
[Vue学习]-Vue UI组件库(二十八) 一.移动端常用的UI组件库 二.PC端常用的UI组件库 三.具体使用自行查看文档,这里就不做概述了
- 【Vue2.0】—Vue脚手架配置代理(二十二)
[Vue2.0]-Vue脚手架配置代理(二十二) 方法一 方法二
- 【BBC纪录片】无人驾驶汽车的黎明(观后总结)
[BBC纪录片]无人驾驶汽车的黎明(观后总结) 1 引言 2 主要内容 2.1 前景 2.2 Level 1-5 2.3 影响 3 简评 基本信息 出处:BBC地平线纪录片 时间:2017年 英文名: ...
- 【Proteus仿真】T6963C驱动PG12864示例(带中英文显示)
[Proteus仿真]T6963C驱动PG12864示例(带中英文显示) Proteus仿真演示 屏幕为T6963C驱动控制的 汉字取模工具和方式 取模软件:PCtoLCD2002 取模方式;阴码.行 ...
- 【高项】项目成本管理与质量管理(十大管理)
[高项]项目成本管理与质量管理(十大管理) 文章目录 1.项目成本管理 1.1 规划成本管理(规划) 1.2 估算成本(规划) 1.3 制定预算(规划) 1.4 控制成本(监控) 1.5 成本管理的 ...
- 【跃迁之路】【530天】刻意练习系列289(2018.07.20)
@(跃迁之路)专栏 叨叨两句 技术的精进不能只是简单的刷题,而应该是不断的"刻意"练习 该系列改版后正式纳入[跃迁之路]专栏,持续更新 一.语言练习 SQL [跃迁之路]SQL语句 ...
- 【跃迁之路】【473天】刻意练习系列232(2018.05.24)
@(跃迁之路)专栏 叨叨两句 技术的精进不能只是简单的刷题,而应该是不断的"刻意"练习 该系列改版后正式纳入[跃迁之路]专栏,持续更新 一.SQL [跃迁之路]SQL语句刻意练习( ...
- 【MPI编程】矩阵向量乘法--解法三(子矩阵块分解)【高性能计算】
简述 子矩阵分解,就是说,将原来的矩阵给分解为更小的矩阵块. 让所有的线程都共享有向量(但不共享矩阵) 为了试验简单,这里做了几个简单的假设 矩阵为方阵(n*n) comm_sz(线程数量)和n满足下 ...
最新文章
- 合并果子(贪心,优先队列)
- 深度丨如何理解和评价机器学习中的表达能力、训练难度和泛化性能
- face.evoLVe.PyTorch
- 【C 语言】编译过程 分析 ( 预处理 | 编译 | 汇编 | 链接 | 宏定义 | 条件编译 | 编译器指示字 )
- java 通过反射获取调用类方法及属性
- GODOT 3.0 开发进度汇报 #6
- 张书兰 (帮别人名字作诗)
- Python help 函数 - Python零基础入门教程
- sqlbulkcopy能用于mysql吗_混凝土自动上料搅拌车能用于农村盖房吗?
- html巡检脚本,WEB服务器巡检脚本
- 7.4读写锁(ReadWriteLock)
- 计算机网络基础(一)
- 移动安全-IOS越狱
- Java修改图片尺寸
- 按压式按摩靠背的设计
- ShowWindow与UpdateWindow
- SAS PROC SQL
- node安装指定版本 (LTS 长期稳定版本,Current 最新版本)
- 人机混合智能在博弈领域的发展
- linux怎么查找文件