题目链接:http://codeforces.com/problemset/problem/617/E


一看这种区间查询的题目,考虑一下莫队。

如何${O(1)}$的修改和查询呢?

令${f(i,j)}$表示区间${\left [ l,r \right ]}$内数字的异或和。

那么:${f(l,r)=f(1,r)~~xor~~f(1,l-1)=k}$

记一下前缀异或和即可维护。


 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
#define maxn 3001000
#define llg long long
#define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
llg n,m,a[maxn],ans,L,R,Ans[maxn],c[maxn],SIZE,k,qzxor[maxn];
struct node
{
llg l,r,num;
}ask[maxn]; inline int getint()
{
int w=,q=; char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
} bool cmp(const node&a,const node&b)
{
if (a.l/SIZE==b.l/SIZE) return a.r<b.r;
return a.l/SIZE<b.l/SIZE;
} void inc(llg x,llg val) {if (x== || x>n) return ;ans+=c[val^k],c[val]++;}
void dec(llg x,llg val) {if (x== || x>n) return ; c[val]--; ans-=c[val^k];} int main()
{
yyj("xor");
cin>>n>>m>>k;
for (llg i=;i<=n;i++) a[i]=getint(),qzxor[i]=qzxor[i-]^a[i];
for (llg i=;i<=m;i++) ask[i].l=getint(),ask[i].r=getint(),ask[i].num=i;
SIZE=sqrt(n);
sort(ask+,ask+m+,cmp);
c[]=;
for (llg i=;i<=m;i++)
{
llg l=ask[i].l,r=ask[i].r;
if (L>l) for (llg i=L-;i>=l;i--) inc(i,qzxor[i-]);
if (r>R) for (llg i=R+;i<=r;i++) inc(i,qzxor[i]);
if (L<l) for (llg i=L;i<l;i++) dec(i,qzxor[i-]);
if (r<R) for (llg i=R;i>r;i--) dec(i,qzxor[i]);
Ans[ask[i].num]=ans;
L=l,R=r;
}
for (llg i=;i<=m;i++) printf("%lld ",Ans[i]);
return ;
}

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

  1. codeforces 617 E. XOR and Favorite Number(莫队算法)

    题目链接:http://codeforces.com/problemset/problem/617/E 题目: 给你a1 a2 a3 ··· an 个数,m次询问:在[L, R] 里面又多少中 [l, ...

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

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

    http://codeforces.com/problemset/problem/617/E 题意:给出n个数,q个询问区间,问这个区间里面有多少个区间[i,j]可以使得ai^ai+1^...^aj ...

  4. 【第400篇题解纪念2016年10月28日】【28.10%】【codeforces 617E】XOR and Favorite Number

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

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

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

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

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

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

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

随机推荐

  1. hashcat使用命令简介

    1.指定HASH类型 在HashCat中--hash-type ?参数可以指定要破解的HASH类型,运行hashcat主程序加上--help参数,在* Generic hash types:中可以看到 ...

  2. 计算概论(A)/基础编程练习2(8题)/7:整数的个数

    #include<stdio.h> int main() { ] = {}; // 输入k个正整数 scanf("%d",&k); // 循环读入和进行算术 w ...

  3. MediaCodec在Android视频硬解码组件的应用

    https://yq.aliyun.com/articles/632892 云栖社区> 博客列表> 正文 MediaCodec在Android视频硬解码组件的应用   cheenc 201 ...

  4. RTP协议全解析(H264码流和PS流)(转)

    源: RTP协议全解析(H264码流和PS流)

  5. centos系统swap设置 查看swap分区的方法

    交换分区swap,意思是“交换”.“实物交易”,它的功能就是在内存不够的情况下,操作系统先把内存中暂时不用的数据,存到硬盘的交换空间,腾出内存来让别的程序运行,和Windows的虚拟内存(pagefi ...

  6. django 函数装饰器 变为 类装饰器

    aaa  

  7. java不常用但很有用的问题排查工具(持续完善)

    因为用的频率不是很多,老忘掉,每次都要搜下,特记录下备忘. 查看进程的启动jvm选项 [root@iZ23nn1p4mjZ ~]# jinfo -flags 16603Attaching to pro ...

  8. Deep Learning Terminologies

    Deep Learning Terminologies batch full batch 已知在梯度下降中,需要对所有样本进行处理过后然后走一步(梯度更新),那么如果我们的样本规模的特别大的话效率就会 ...

  9. KPI 私有CA

    openssl总结及私有CA的搭建 搭建CA服务器 CA(证书颁发机构)服务器配置图解过程(1) 私有CA服务器的搭建 搭建CA服务器 使用OpenSSL搭建CA Linux加密和解密.openssl ...

  10. python简说(十八)导入模块

    1.import xx import 一个模块的实质就是把这个python文件从头到尾执行一遍 2.import模块的查找模块的顺序 1).从当前目录下找 2).sys.path python的安装目 ...