XOR and Favorite Number CodeForces - 617E(前缀异或+莫队)
题意原文地址:https://blog.csdn.net/chenzhenyu123456/article/details/50574169
题意:有n个数和m次查询,每次查询区间[l, r]问满足ai ^ ai+1 ^ ... ^ aj == k的(i, j) (l <= i <= j <= r)有多少对。
思路:离线做。首先预处理前缀异或和sum[],那么ai ^ ... ^ aj == sum[i-1] ^ sum[j]。
这样对一次查询[l, r]的处理,可以从左到右扫一次,统计k ^ sum[i]出现的次数(l <= i <= r)。
假设已经处理到[L, R],对下一次的[l, r]处理——
若L < l,显然多余,需要去掉[L, l-1]的部分,若L > l需要加上[l, L-1]的部分,反之不需要处理。
若R > r,..................[r+1, R]......,若R < r........[R+1, r]......,..............。
用莫队做即可。
#include <bits/stdc++.h>
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
LL pos[maxn], c[maxn], s[maxn], cnt[<<], sum[maxn], out_ans[maxn];
LL n, m, ans, k;
struct node
{
LL r, l, id, res;
}Node[maxn]; int cmp(node a, node b)
{
if(pos[a.l] == pos[b.l])
return a.r < b.r;
return a.l < b.l;
} int cmp_id(node a, node b)
{
return a.id < b.id;
} void add(int x)
{
ans += cnt[sum[x]^k]; // 右区间拓展时: 这样就保证了c[i]^k 是在前面出现过的 因为如果没有出现过 则cnt对应的值为0 左区间同理
cnt[sum[x]]++;
} void dec(int x)
{
cnt[sum[x]]--; //右区间缩小时: 防止c[i]^k 是后边的 左区间同理
ans -= cnt[sum[x]^k]; } int main()
{
scanf("%lld%lld%lld", &n, &m, &k);
for(int i=; i<=n; i++)
{
scanf("%lld", &c[i]);
sum[i] = sum[i-] ^ c[i];
}
int block = sqrt(n);
for(int i=; i<=n; i++)
pos[i] = (i-)/block + ;
for(int i=; i<=m; i++)
{
scanf("%lld%lld", &Node[i].l, &Node[i].r);
Node[i].id = i;
Node[i].l--; //预处理
}
sort(Node+, Node+m+, cmp);
cnt[] = ;
for(int i=, l=, r=; i<=m; i++)
{
for(; r < Node[i].r; ++r)
add(r+);
for(; r > Node[i].r; r--)
dec(r);
for(; l < Node[i].l; ++l)
dec(l);
for(; l > Node[i].l; --l)
add(l-); Node[i].res = ans;
}
sort(Node+, Node+m+, cmp_id); for(int i=; i<=m; i++)
printf("%I64d\n",Node[i].res); return ;
}
XOR and Favorite Number CodeForces - 617E(前缀异或+莫队)的更多相关文章
- XOR and Favorite Number Codeforces - 617E || [CQOI2018]异或序列
https://www.luogu.org/problemnew/show/P4462 http://codeforces.com/problemset/problem/617/E 这个是莫队裸题了吧 ...
- XOR and Favorite Number CodeForces - 617E -莫队-异或前缀和
CodeForces - 617E 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k.(注意 i ! = j) ...
- XOR and Favorite Number CodeForces - 617E
a[i]^a[i+1]--a[j]=k; 处理前缀和pre[i] 那么上式可以表示为pre[i-1]^pre[j]=k; #include<bits/stdc++.h> using nam ...
- Codeforces 877F Ann and Books 莫队
转换成前缀和, 预处理一下然后莫队. #include<bits/stdc++.h> #define LL long long #define fi first #define se se ...
- codeforces 940F 带修改的莫队
F. Machine Learning time limit per test 4 seconds memory limit per test 512 megabytes input standard ...
- Codeforces D. Powerful array(莫队)
题目描述: Problem Description An array of positive integers a1, a2, ..., an is given. Let us consider it ...
- Codeforces 86D Powerful array (莫队)
D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...
- codeforces 86D,Powerful array 莫队
传送门:https://codeforces.com/contest/86/problem/D 题意: 给你n个数,m次询问,每次询问问你在区间l,r内每个数字出现的次数的平方于当前这个数的乘积的和 ...
- CodeForces - 86D D. Powerful array —— 莫队算法
题目链接:http://codeforces.com/problemset/problem/86/D D. Powerful array time limit per test 5 seconds m ...
随机推荐
- 算法设计:UNION-FIND算法实现
在上周的算法设计课程中,我们学习了UNION-FIND算法,该算法用来对不相交集进行查询与合并操作,但任何优秀的算法都必须要用实际的代码来进行实现,接下来我们就来看看具体的代码实现 1. 不相关集数据 ...
- mysql读写分离配置(整理)
mysql读写分离配置 环境:centos7.2 mysql5.7 场景描述: 数据库Master主服务器:192.168.206.100 数据库Slave从服务器:192.168.206.200 M ...
- MUI的踩坑笔记
最近在做公司项目的手机端实现,稍微记录下遇到的坑 1.在app开发中,若要使用HTML5+扩展api,必须等plusready事件发生后才能正常使用,mui将该事件封装成了mui.plusReady( ...
- 我对BP网络的简单的理解
最近在学习tf的神经网络算法,十多年没有学习过数学了,本来高中数学的基础,已经彻底还给数学老师了.所以我把各种函数.公式和推导当做黑盒子来用,理解他们能做到什么效果,至于他们是如何做到的,暂时不去深究 ...
- c++虚继承与虚函数
学习继承与多态时看到这两个概念,记录整理. 虚继承与虚函数都是用virtual关键字实现,虚继承为了防止多重继承,而虚函数为了实现多态. 是几个例子. 虚继承: class A{}; class B: ...
- RabbitMQ理论部分
概念 queue 队列 exchange 交换机 bind 绑定 channel 通道 一个发送消息流程包含上述四个概念.消息经过channel传递给exc ...
- 【探路者】final贡献分配
[探路者]组成员及各位博客地址. 1蔺依铭:http://www.cnblogs.com/linym762/ 2张恩聚:http://www.cnblogs.com/zej87/ 3米赫:http: ...
- 2017秋-软件工程第十二次作业(一)-PSP总结
[回顾]:回顾开学时的博客并回答相关问题 1.回想一下你曾经对计算机专业的畅想当初你是如何做出选择计算机专业的决定的?经过一个学期,你的看法改变了么,为什么?答:当初的决定是以前的事情,没有改变.经历 ...
- [buaa-SE-2017]个人作业-期末总结
个人作业-期末总结 Part1: 阅读作业 在这一部分,首先我将说说我对这次阅读作业中每篇文章的理解,最后结合这次团队项目的经理谈谈自己对软件开发的看法. 1. No Silver Bullet 文章 ...
- 20172313『Java程序设计』课程结对编程练习_四则运算第二周阶段总结
20172313『Java程序设计』课程结对编程练习_四则运算第二周阶段总结 结对伙伴 20172326康皓越 博客地址(http://www.cnblogs.com/326477465-a/p/90 ...