莫队算法。

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn=+;
int a[maxn],pre[maxn];
long long cnt[*maxn];
int pos[maxn];
int n,m,k;
long long ans[maxn];
long long Ans;
int L,R;
struct X
{
int l,r,id;
}s[maxn]; bool cmp(const X&a,const X&b)
{
if(pos[a.l]==pos[b.l]) return a.r<b.r;
return a.l<b.l;
} int main()
{
scanf("%d%d%d",&n,&m,&k); int sz=sqrt(n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
pre[i]=(pre[i-]^a[i]);
pos[i]=i/sz;
} for(int i=;i<=m;i++)
{
scanf("%d%d",&s[i].l,&s[i].r);
s[i].id=i;
} sort(s+,s++m,cmp);
Ans=; cnt[pre[s[].l-]]++; for(int i=s[].l;i<=s[].r;i++)
{
Ans=Ans+cnt[pre[i]^k];
cnt[pre[i]]++;
} L=s[].l; R=s[].r;
ans[s[].id]=Ans; for(int i=;i<=m;i++)
{
while(L<s[i].l)
{
cnt[pre[L-]]--;
Ans=Ans-cnt[pre[L-]^k];
L++;
} while(L>s[i].l)
{
L--;
Ans=Ans+cnt[pre[L-]^k];
cnt[pre[L-]]++;
} while(R<s[i].r)
{
R++;
Ans=Ans+cnt[pre[R]^k];
cnt[pre[R]]++;
} while(R>s[i].r)
{
cnt[pre[R]]--;
Ans=Ans-cnt[pre[R]^k];
R--;
}
ans[s[i].id]=Ans;
} for(int i=;i<=m;i++)
printf("%lld\n",ans[i]); return ;
}

CodeForces 617E XOR and Favorite Number的更多相关文章

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

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

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

  3. Codeforces 617E XOR and Favorite Number莫队

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

  4. Codeforces 617E XOR and Favorite Number(莫队算法)

    题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减 ...

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

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

  6. [CQOI 2018]异或序列&[Codeforces 617E]XOR and Favorite Number

    Description 题库链接1 题库链接2 已知一个长度为 \(n\) 的整数数列 \(a_1,a_2,\cdots,a_n\) ,给定查询参数 \(l,r\) ,问在 \([l,r]\) 区间内 ...

  7. Codeforeces 617E XOR and Favorite Number(莫队+小技巧)

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

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

  9. XOR and Favorite Number CodeForces - 617E -莫队-异或前缀和

    CodeForces - 617E 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k.(注意 i ! =  j) ...

随机推荐

  1. prototype小解

    prototype由来 在理解prototype前,首先得理解js面向对象编程的私有变量.私有函数,静态变量.静态函数,以及实例变量,实例函数 私有变量,私有函数 函数内部通过var定义的变量 fun ...

  2. centos6.5 安装git

    1.安装编译git时需要的包 # yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel # yum ins ...

  3. jQuery Mobile 学习

    jQuery Mobile 学习系列 http://blog.csdn.net/bao990423420/article/details/13995021

  4. java数据结构之有序表查找

    这篇文章是关于有序表的查找,主要包括了顺序查找的优化用法.折半查找.插值查找.斐波那契查找: 顺序优化查找:效率极为底下,但是算法简单,适用于小型数据查找: 折半查找:又称为二分查找,它是从查找表的中 ...

  5. UVALive 2523 Machine Schedule(二分图求最大匹配数)

    题意:有两台机器,上面有多个工作区域,有多个任务,分别可以在两台机器的某一个区域上完成,两台机器一开始都在0区域上工作,每次更改区域,都会重新启动一次,让我们求出最小的重启次数. 思路:将两个区域连线 ...

  6. k-Means和KNN算法简述

    k-means 算法 k-means 算法接受输入量 k :然后将n个数据对象划分为 k个聚类以便使得所获得的聚类满足:同一聚类中的对象相似度较高:而不同聚类中的对象相似度较小.聚类相似度是利用各聚类 ...

  7. 【linux系统学习】计算机硬件核心知识

    (一)企业里PC服务器品牌及型号 互联网公司服务器品牌:DELL,HP,IBM(百度):国内品牌:浪潮,联想,航天联志 DELL服务器品牌:1u = 4.45CM 2010年前 1u 1850,195 ...

  8. Talking about Health

    George: Doctor, what is the best way to stay healthy? Dictor: Having a good diet is probably the mos ...

  9. AJAX开发技术--AJAX简介

    Asynchronous  JavaScript and XML,异步JavaScript和XML 主要目的用于页面的局部刷新.不用全部刷新,提高性能. 在AJAX中主要是通过XMLHttpReque ...

  10. ASP.NET WEB开发,实现上传图片

    protected void btnUp_Click(object sender, EventArgs e) { Boolean fileOK = false; String path = Serve ...