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 ...
随机推荐
- Windows7安装JDK的环境变量设置javac不是内部命令或外部命令
转自:http://bbs.gfan.com/android-5941970-1-1.html Windows7安装JDK的环境变量设置 Windows7 X64安装“jdk-6u26-windows ...
- 利用Py-Socket模块做的一个不登陆windows服务器自动实现替换或者调用自动拨号功能
xu言: 最近,有个朋友让我帮忙“搞点事情”,然后正好在学习socket模块,这个模块666啊,基本上可以实现远程服务器cmd shell的大部分功能.好,话不多说,直接上码~ 由于很多电信运营商都会 ...
- 20170706xlVBA城中村改造汇总
Public Sub GatherDataPicker() Application.ScreenUpdating = False Application.DisplayAlerts = False A ...
- golang martini 源码阅读笔记之martini核心
继上一篇关于inject注入的笔记,理解了martini的关键核心之一:依赖注入.注入回调函数,由运行时进行主动调用执行.这一篇主要是注解martini的骨架martini.go的实现,下面先从一个简 ...
- hdu3294 manacher
One day, sailormoon girls are so delighted that they intend to research about palindromic strings. O ...
- thinkphp查询缓存
S()函数的使用: ThinkPHP把各种缓存方式都抽象成统一的缓存类来调用,而且ThinkPHP把所有的缓存机制统一成一个S方法来进行操作,所以在使用 不同的缓存方式的时候并不需要关注具体的缓存 ...
- php date()
PHP Date() 函数把时间戳格式化为更易读的日期和时间. date(format,timestamp) format:显示时间的格式. timestamp:可选.规定时间戳.默认是当前时间和日 ...
- oracle传入一个可能为空的参数进行查询
在我们数据库的表中的某些字段可能为空,且传入的查询参数也可能为空. 例如 ,,); 其查询结果集如下 MAPPING_ID PARTY_ID VENDOR_ID SUPPLIER_REG_ID 332 ...
- IOS-底层数据结构
Objective-C底层数据结构 类的数据结构 Class(指针) typedef struct objc_class *Class; /* 这是由编译器为每个类产生的数据结构,这个结构定义了一 ...
- splunk中mongodb作用——存用户相关数据如会话、搜索结果等
About the app key value store The app key value store (or KV store) provides a way to save and retri ...