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 ...
随机推荐
- English trip -- VC(情景课)8 C
MP review: 音标(Phonetic symbol) [ɪ] lit adj. 照亮的,点着的(light的过去式及过去分词)n. (Lit)人名:(瑞典)利特:(老)李 [ʊ] g ...
- CAS操作原理分析
一.CAS简单介绍 CAS:Compare and Swap, 翻译成比较并交换. java.util.concurrent包中借助CAS实现了区别于synchronouse同步锁 ...
- Android之ToolBar和自定义ToolBar实现沉浸式状态栏
沉浸式状态栏确切的说应该叫做透明状态栏.一般情况下,状态栏的底色都为黑色,而沉浸式状态栏则是把状态栏设置为透明或者半透明. 沉浸式状态栏是从android Kitkat(Android 4.4)开始出 ...
- GitHub出现Permissiondenied (publickey).
cd ~/.ssh ssh -T git@github.com 出现Permission denied(publickey).意思表示缺少公钥. ssh-keygen -t rsa -C " ...
- FastDFS install - 2
storage install nginx 1. update dependency package yum -y install pcre-devel openssl openssl-devel g ...
- ubuntu系统连接windows系统
1,首先我们要在ubuntu系统是输入:sudo apt-get install rdesktop 2, 用法: rdesktop[options] server[:port] .其实我们就可 ...
- kill word out 1
1● de 使~~ 成为 :离开 ,去掉,向下,变慢 2● dif 不,分开 ,否定,离开 3● deci 十分之一 4● deca 向下,离开 5● deca 十 6● di ...
- HTML绘制三角形的方法:
<!DOCTYPE html> <html> <body> <style> #triangle-up { width: 0px; height: 0px ...
- Java——多线程小例子
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- bjui的validate表单验证的使用
date-rule ="date" 表示格式为yyyy-MM-dd date-rule = "datetime" 表示格式为yyyy-MM-dd HH:mm:s ...