原来我直接学的是假的莫队

原题:

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 莫队模板题的更多相关文章

  1. CodeForces - 617E XOR and Favorite Number 莫队算法

    https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry,  问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...

  2. Codeforces 617E XOR and Favorite Number莫队

    http://codeforces.com/contest/617/problem/E 题意:给出q个查询,每次询问区间内连续异或值为k的有几种情况. 思路:没有区间修改,而且扩展端点,减小端点在前缀 ...

  3. codeforces 617E. XOR and Favorite Number 莫队

    题目链接 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k. 维护一个前缀异或值就可以了. 要注意的是 区间[l ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. Codeforces617 E . XOR and Favorite Number(莫队算法)

    XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...

  8. E. XOR and Favorite Number 莫队 2038: [2009国家集训队]小Z的袜子(hose)

    一直都说学莫队,直到现在才学,训练的时候就跪了   T_T,其实挺简单的感觉.其实训练的时候也看懂了,一知半解,就想着先敲.(其实这样是不好的,应该弄懂再敲,以后要养成这个习惯) 前缀异或也很快想出来 ...

  9. 【洛谷2709】小B的询问(莫队模板题)

    点此看题面 大致题意: 有一个长度为\(N\)的序列,每个数字在\(1\sim K\)之间,有\(M\)个询问,每个询问给你一个区间,让你求出\(\sum_{i=1}^K c(i)^2\),其中\(c ...

随机推荐

  1. infra 仪表盘效果

    private void Gauge2() { // Infragistics.WebUI.UltraWebGauge.UltraGauge ultraGauge2 = //new Infragist ...

  2. codeforces 484b//Maximum Value// Codeforces Round #276(Div. 1)

    题意:给一个数组,求其中任取2个元素,大的模小的结果最大值. 一个数x,它的倍数-1(即kx-1),模x的值是最大的,然后kx-2,kx-3模x递减.那么lower_bound(kx)的前一个就是最优 ...

  3. 21 模块(collections,time,random,os,sys)

    关于模块importfrom xxx import xxx2. Collections1. Counter 计数器2. 栈: 先进后出.队列:先进先出deque:双向队列3. defaultdict ...

  4. 使用tomcat启动dubbo项目

    首先,黑体标出 官方不推荐使用web容器进行dubbo的启动 但是,有些时候,我们不采用他们的建议. 背景: 之前用的dubbo项目,是由main函数启动的,每次发布项目,需要启动两项: 1. tom ...

  5. ubuntu下没有Language Support

    sudo apt-get installlanguage-selector-gnome

  6. seo-摘自网友

    网页前端制作中的SEO 在SEO盛行的今天到处都在谈优化,对于网站前端制作人员来说,有几点是跟SEO相关的,也就是SEO站内优化中的一部分,下面总结几点: 1.title,<title>页 ...

  7. iOS UI-表格控制器(UITableView)-基本使用

    tableView的常见属性 cell的常见属性 一.一般情况 #import "ViewController.h" @interface ViewController ()< ...

  8. 获取和设置消息队列的属性msgctl,删除消息队列

    消息队列的属性保存在系统维护的数据结构msqid_ds中,用户可以通过函数msgctl获取或设置消息队列的属性. int msgctl(int msqid, int cmd, struct msqid ...

  9. cnblogs插件jiathis

    博客园cnblogs增加分享插件 <!--jiathis button Begin--> <div id="ckepop"> <span class= ...

  10. CAS-登出配置

    该图 当一个web 浏览器登录到应用服务器时,应用服务器(application)会监测用户的session,如果没有session,则应用服务器会把url跳转到CAS server上.要求 用户登录 ...