CodeForces -617E XOR and Favorite Number(莫队)
题目链接:点击这里
题目大意:
给定一个长度为 nnn 的序列 a1,a2,...,ana_1,a_2,...,a_na1,a2,...,an ,再给出一个数字 kkk , mmm 组询问每组询问给出 l,rl,rl,r ,求 ala_lal 到 ara_rar 中子序列异或和等于 kkk 的数目
题目分析:
我们考虑异或的性质对区间异或和可得:
aixorai+1xor...xoraja_i\ xor\ a_{i+1}\ xor...xor\ a_jai xor ai+1 xor...xor aj
=(a1xora2xor...xorai−1)xor(a1xora2xor...xoraj)=(a_1\ xor\ a_{2}\ xor...xor\ a_{i-1})xor(a_1\ xor\ a_{2}\ xor...xor\ a_j)=(a1 xor a2 xor...xor ai−1)xor(a1 xor a2 xor...xor aj)
记 sumi=a1xora2xor...xoraisum_i=a_1\ xor\ a_{2}\ xor...xor\ a_isumi=a1 xor a2 xor...xor ai ,此时 题目所求即为区间 [l,r][l,r][l,r] 内有多少对 i,j(i<j)i,j(i<j)i,j(i<j) 使得 sumjxorsumi=ksum_j\ xor \ sum_i=ksumj xor sumi=k ,离线区间问题考虑用莫队,当增加节点 jjj 时,就是要找区间内有多少个 sumi=kxorsumjsum_i=k\ xor\ sum_jsumi=k xor sumj ,我们开一个 cntcntcnt 数组当桶记录一下每个数字出现的次数,区间的贡献就是 cnt[kxorsumj]cnt[k\ xor\ sum_j]cnt[k xor sumj]
注意:
- axorb<a+ba\ xor \ b < a+ba xor b<a+b 所以桶要开到 2e62e62e6
- 块不能因为追求效率直接取 nm∗23\frac n {m*\frac 23}m∗32n ,当 n=10,m=10000n = 10,m = 10000n=10,m=10000 时块大小就被卡成 000 了,所以可以让算出来的块大小和 111 取一下 maxmaxmax
具体细节见代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#define ll long long
#define inf 0x3f3f3f3f
#define int ll
using namespace std;
int read()
{int res = 0,flag = 1;char ch = getchar();while(ch<'0' || ch>'9'){if(ch == '-') flag = -1;ch = getchar();}while(ch>='0' && ch<='9'){res = (res<<3)+(res<<1)+(ch^48);//res*10+ch-'0';ch = getchar();}return res*flag;
}
const int maxn = 1e5+5;
const int maxm = 2e6;
const int mod = 1e9+7;
const double pi = acos(-1);
const double eps = 1e-8;
int n,m,k,block,now,a[maxn],sum[maxn],cnt[maxm],ans[maxn];
struct query{int l,r,id;friend bool operator < (const query &a,const query &b){return a.l/block^b.l/block ? a.l < b.l : (a.l/block)&1 ? a.r<b.r : a.r>b.r;}
}q[maxn];
void add(int pos)
{now += cnt[k^pos];cnt[pos]++;
}
void del(int pos)
{cnt[pos]--;now -= cnt[k^pos];
}
signed main()
{n = read(),m = read(),k = read();for(int i = 1;i <= n;i++){a[i] = read();sum[i] = a[i]^sum[i-1];}block = max(n/sqrt(m*2/3),1.0);for(int i = 1;i <= m;i++){q[i].l = read()-1,q[i].r = read();q[i].id = i;}sort(q+1,q+m+1);int l = 1,r = 0;for(int i = 1;i <= m;i++){int ql = q[i].l,qr = q[i].r;while(l < ql) del(sum[l++]);while(l > ql) add(sum[--l]);while(r < qr) add(sum[++r]);while(r > qr) del(sum[r--]);ans[q[i].id] = now;}for(int i = 1;i <= m;i++)printf("%lld\n",ans[i]);return 0;
}
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 (莫队+前缀和)
Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is g ...
- 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. 思路: ...
最新文章
- CSS布局(五) 网页布局方式
- 一文读懂目前大热的AutoML与NAS!
- golang new与make的一点区别理解
- 洛谷——P1652 圆
- 谷歌浏览器software_reporter_tool关闭
- 清除每隔5000毫秒请求一次接口的定时器(需求:每当我手动核销电子码,页面上的显示数据要实时更新到)...
- 《深入实践Spring Boot》阅读笔记之二:分布式应用开发
- jquery ajax 方法及各参数详解
- Python 算法交易实验23 退而结网1
- 地理加权回归出现多重共线性如何解决
- 电商系统PC商城模块介绍
- 计算机大赛获奖作品动画,全国xx杯说课大赛计算机类一等奖作品:《遮罩动画》说课课件...
- phaser java_java并发编程:Phaser同步工具原理
- 【协程】MyCoroutine轻量级协程框架代码详细剖解
- 2022年湖南省自考考试幼儿园教育活动设计与组织练习题及答案
- x86与x86-64数据格式与常用的汇编指令笔记
- 一个BUG导致3000万损失!涉及资金交易的功能该如何做好测试和项目管理
- 浅谈下二级域名做网站优化的利与弊!
- 不懂优雅停机,搞挂了线上服务,咋办?
- 使用mathtype后,word自动生成目录中出现“Equation Chapter (Next) Section 1字样”