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 ...
随机推荐
- pytest使用笔记(二)——pytest+allure配置使用
使用环境及预置条件 开发工具:pycharm 操作系统:win10 开发语言:python3.6 使用库:pytest4.0,pytest-allure-adaptor 注意不要安装allure-py ...
- Unity学习笔记(3):一些常用API和应用场景
Mathf.Lerp(float a,float b,float t)插值函数,当a < b时往a中插入t,以此来实现颜色,声音等渐变效果. GameObject.FindWithTag(str ...
- 用UGUI制作可根据手指位置自动定位的隐形遥杆
之前写过遥杆怎么做,这里依然用的是之前的方法,就不介绍了. 之前玩过<蜡烛人>,发现手游版的<蜡烛人>的遥杆是看不见的,手指直接在屏幕左边滑动人物就可以移动,可能是为了增强沉浸 ...
- Keycloak服务器安装和配置
安装地址:https://www.keycloak.org/archive/downloads-4.4.0.html 参考文档:https://www.keycloak.org/docs/latest ...
- CHAPTER 40 Science in Our Digital Age 第40章 我们数字时代的科学
CHAPTER 40 Science in Our Digital Age 第40章 我们数字时代的科学 The next time you switch on your computer, you ...
- Gitlab CI-3.遇到的问题
五.遇到的问题 1. cannot validate certificate for x.x.x.x because it doesn't contain any IP SANs 报错信息:ERROR ...
- java按照字节切割字符串,解决汉字的问题
编写一个截取字符串的函数,输入为一个字符串,截取开始地址,截取字节数,输出为按字节截取的字符串. 但是要保证汉字不被截半个, 如“我ABC”,0,4,应该截为“我AB”,输入“我ABC汉DEF”,1, ...
- CsvReader和CsvWriter操作csv文件
使用方法: 提供把实例数据输出到磁盘csv文件的功能 提供读取csv文件,并封装成指定实例的功能 小工具自己依赖了slf4j+logbak,以及fastJson,如果与系统冲突,可以在pom文件中去除 ...
- roadhog中如何拷贝文件
一:使用 public 目录 我们约定 public 目录下的文件会在 server 和 build 时被自动 copy 到输出目录(默认是 ./dist)下.所以可以在这里存放 favicon, i ...
- js最简单的动画
$(document).ready(function(){ //�ֶ�����ҳ��Ԫ�� $("#reset").click(function(){ $("*" ...