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 ...
随机推荐
- python log封装
# _*_ coding:utf-8 _*_ import logging import os import sys import time log_path = os.path.dirname(sy ...
- css学习杂记
1.css中的&语法 &是sass的语法,代表上一级选择器. 例如: .el-row { margin-bottom: 20px; &:last-child { margin- ...
- shell中与运算 cut切分行 if while综合在一起的一个例子
前言: 公司要统计 treasury库hive表磁盘空间,写了个脚本,如下: 查询hive仓库表占用hdfs文件大小: hadoop fs -du -h /user/hive/warehouse/t ...
- SMR解析
SMR描述 SMR(Shingled Magnetic Recording)叠瓦式磁记录盘是一种采用新型磁存储技术的高容量磁盘.SMR盘将盘片上的数据磁道部分重叠,就像屋顶上的瓦片一样,这种技术被称为 ...
- 第十二次ScrumMeeting博客
第十二次ScrumMeeting博客 本次会议于11月30日(四)22时整在3公寓725房间召开,持续35分钟. 与会人员:刘畅.辛德泰.张安澜.赵奕.方科栋. 1. 每个人的工作(有Issue的内容 ...
- ES6的新特性(23)——ArrayBuffer
ArrayBuffer ArrayBuffer对象.TypedArray视图和DataView视图是 JavaScript 操作二进制数据的一个接口.这些对象早就存在,属于独立的规格(2011 年 2 ...
- dobule运算
DecimalFormat df = new DecimalFormat("0.00"); double rate = (warnMonNum/totalCustCount)*10 ...
- Bing词典vs有道词典比对测试报告——体验篇之成长性及用户控制权
成长性: 会记住曾经查询过的单词或例句与有道词典实现基本一样,并无特别亮点. 用户有控制权: 必应词典和有道词典都能实现基本的查询前进和后退.以及无法查找结果,能顺利进行反馈. 我们在输入完单词按下回 ...
- Alpha阶段产品功能说明
先展示一下我们的功能流程图吧~ 一.学生用户 1. 学生登陆注册 BuaaClubs是北航所有在校生都可以注册登录的网站. 登陆界面是这样哒~ 2. 浏览报名活动 同学们可以在这个网站上查看所有社团发 ...
- OO第二阶段作业总结
第五次作业: 设计策略: 本次作业设计的基本思路是按照指导书所给的推荐方法来完成的,即共用对象为队列盘,线程有电梯.调度器.以及扫描器,扫描器将控制台输入的有效指令加入到队列盘中,调度 ...