CODEFORCES 340 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 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.
1 ≤ n, m ≤ 100 000, 0 ≤ k ≤ 1 000 000, 0 ≤ ai ≤ 1 000 000
题意:
给定一个数列a[i],你可以通过异或前缀和求出s[i],每次求出[l-1,r]内s[i] xor s[j]=k的(i,j)的对数
模板题,先分块,左端点所在块为第一关键字,右端点为第二关键字排序
然后记录一个l和r,每次直接暴力让l和r走到查询的两端点即可
我之前一直以为块间跳转是当两端点中间有一个或多个块的时候加上中间的块的值
经过jjh和sqy大神的指点才知道原来分块就是指排序的时候分块,处理的时候暴力走就可以了
我学了假的分块
有一些细节需要注意,首先所有的操作都是建立在异或前缀和上的所以输入的时候要搞前缀和,而且算答案的时候l要-1
一般n<1e5求数对的个数一般会爆int,要注意longlong
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
#define ll long long
int rd(){int z=,mk=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')mk=-; ch=getchar();}
while(ch>=''&&ch<=''){z=(z<<)+(z<<)+ch-''; ch=getchar();}
return z*mk;
}
struct dcd{int x,y,id;}b[];
int n,m,k,a[];
int blck;
ll cnt[],ans[];
ll bwl=;
inline void ist(int x){ bwl+=cnt[a[x]^k],++cnt[a[x]];}
inline void dlt(int x){ --cnt[a[x]],bwl-=cnt[a[x]^k];}
bool cmp(dcd x,dcd y){ return(x.x/blck==y.x/blck)?(x.y<y.y):(x.x/blck<y.x/blck);}
int main(){//freopen("ddd.in","r",stdin);
cin>>n>>m>>k; blck=(int)sqrt(n*1.0);
for(int i=;i<=n;++i) a[i]=rd(),a[i]^=a[i-];
for(int i=;i<=m;++i) b[i].x=rd(),b[i].y=rd(),b[i].id=i;
sort(b+,b+m+,cmp);
int l=,r=; cnt[]=;
for(int i=;i<=m;++i){
while(l<b[i].x) dlt(l-),++l;
while(l>b[i].x) l--,ist(l-);
while(r<b[i].y) ist(++r);
while(r>b[i].y) dlt(r--);
ans[b[i].id]=bwl;
}
for(int i=;i<=m;++i) printf("%I64d\n",ans[i]);
return ;
}
CODEFORCES 340 XOR and Favorite Number 莫队模板题的更多相关文章
- CodeForces - 617E XOR and Favorite Number 莫队算法
https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry, 问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...
- Codeforces 617E XOR and Favorite Number莫队
http://codeforces.com/contest/617/problem/E 题意:给出q个查询,每次询问区间内连续异或值为k的有几种情况. 思路:没有区间修改,而且扩展端点,减小端点在前缀 ...
- codeforces 617E. XOR and Favorite Number 莫队
题目链接 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k. 维护一个前缀异或值就可以了. 要注意的是 区间[l ...
- Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法
E. XOR and Favorite Number 题目连接: http://www.codeforces.com/contest/617/problem/E Descriptionww.co Bo ...
- Codeforces Round #340 (Div. 2) E. XOR and Favorite Number —— 莫队算法
题目链接:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 ...
- codeforces 617E E. XOR and Favorite Number(莫队算法)
题目链接: E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes i ...
- Codeforces617 E . XOR and Favorite Number(莫队算法)
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...
- E. XOR and Favorite Number 莫队 2038: [2009国家集训队]小Z的袜子(hose)
一直都说学莫队,直到现在才学,训练的时候就跪了 T_T,其实挺简单的感觉.其实训练的时候也看懂了,一知半解,就想着先敲.(其实这样是不好的,应该弄懂再敲,以后要养成这个习惯) 前缀异或也很快想出来 ...
- 【洛谷2709】小B的询问(莫队模板题)
点此看题面 大致题意: 有一个长度为\(N\)的序列,每个数字在\(1\sim K\)之间,有\(M\)个询问,每个询问给你一个区间,让你求出\(\sum_{i=1}^K c(i)^2\),其中\(c ...
随机推荐
- 使用Python Django在Ubuntu下搭建数据库型网站
最近想做一个数据库网站,我对Python很熟悉,也了解到Django很好用,于是说搞就搞. 首先,在快云上买了一个vps,一元试用一个月,Ubuntu系统. 1.安装Django apt-get up ...
- android----AsyncHttpClient的get,post和图片上传
async-http-client库是一个基于回调函数的Http异步通信客户端Android组件,是在Apache的HttpClient库的基础上开发构建而成的. Eclipse使用:导入androi ...
- python-day21--random模块
>>> import random #随机整数 >>> random.randint(1,5) # 大于等于1且小于等于5之间的整数 >>> ra ...
- linux--多进程进行文件拷贝
学习IO的时候,我们都曾经利用文件IO函数,标准IO函数都实现了对文件的拷贝, 对某一个文件进行拷贝时,我们可以考虑一下几种方式: a.单进程拷贝: 假设某一文件需要拷贝100字节,每一个时间片可以完 ...
- 获取Oracle数据库awr报告方法
--登录数据库 sqlplus username/passwd; --运行生成AWR报告脚本 SQL> @?/rdbms/admin/awrrpt.sql; --输入要生成报告的格式:htm ...
- elment-ui table组件 -- 远程筛选排序
elment-ui table组件 -- 远程筛选排序 基于 elment-ui table组件 开发,主要请求后台实现筛选 排序的功能. 需求 排序 筛选 是对后台整个数据进行操作,而不是对当前页面 ...
- JavaScript学习总结(十三)——极简主义法编写JavaScript类
前两天在网上无意中发现了一篇使用极简主义法定义JavaScript类的文章,原文链接,这个所谓的"极简主义法"我还是第一次听说,是荷兰程序员Gabor de Mooij提出来的,这 ...
- Wifi Troughput Test using iperf
learning wifi throughput test using iperf [Purpose] Learning how to do wifi throughput test u ...
- 弹出层小插件之(二) layer&layui
其实layer或者layui相对于上次所说的 sweetalert来说不仅仅有弹出层,它有很多的功能,这也大大的提高了我们的开发效率,根据我们项目的实际需要的效果进行选择.下面介绍下Layer的用法吧 ...
- ural1469
题解: 从左往右加入每一个点 判断一下和,pre,nxt是否相交 删除得时候也要判断 代码: #pragma GCC optimize(2) #include<cstdio> #inclu ...