Codeforces 617 E. XOR and Favorite Number
题目链接: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的更多相关文章
- codeforces 617 E. XOR and Favorite Number(莫队算法)
题目链接:http://codeforces.com/problemset/problem/617/E 题目: 给你a1 a2 a3 ··· an 个数,m次询问:在[L, R] 里面又多少中 [l, ...
- 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 ...
- Codeforces 617E:XOR and Favorite Number(莫队算法)
http://codeforces.com/problemset/problem/617/E 题意:给出n个数,q个询问区间,问这个区间里面有多少个区间[i,j]可以使得ai^ai+1^...^aj ...
- 【第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 ...
- 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 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 ...
- Codeforces617 E . XOR and Favorite Number(莫队算法)
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...
- 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 ...
随机推荐
- eclipse格式化代码样式
1.Window->Preferences //Java 格式化 2.Java->Code Style->Formatter->New->Edit->Line Wr ...
- 细数那些不懂Spring底层原理带来的伤与痛
1. 什么是spring? Spring 是个Java企业级应用的开源开发框架.Spring主要用来开发Java应用,但是有些扩展是针对构建J2EE平台的web应用.Spring 框架目标是简化Jav ...
- webform 使用富文本编辑器
<div class="form-group"> <label class="col-xs-2 control-label text-right&quo ...
- Tomcat启动报错:[Failed to start component]的解决方案
在MyEclipse中启动Tomcat,该Tomcat仅部署了一个报错项目,启动Tomcat Server的全部信息如下: usage: java org.apache.catalina.startu ...
- 12: nginx原理及常用配置
1.1 nginx基本介绍 1.nginx高并发原理( 多进程+epoll实现高并发 ) 1. Nginx 在启动后,会有一个 master 进程和多个相互独立的 worker 进程. 2. 每个子进 ...
- python简说(十一)os模块
import osres = os.listdir('/Users/nhy/Desktop') #列出某个目录下的所有文件# os.remove()# os.rename()# os.mkdir(r' ...
- Linux启动报:UNEXPECTED INCONSISTENCY: RUN fsck MANUALLY问题解决
现象: 在此界面输入下root的密码.会进入到修复模式 在修复模式下,输入命令fsck –y /dev/mapper/vg_swnode1-lv_root 这个后面跟的路径就是你上面提示出错的那个路 ...
- 2018-2019-1 20189206 vim.c插件安装
vim插件安装 vim插件安装 由于今天在安装vim.c插件耗费了很多时间,配置文件一直不生效,特此记录以下安装插件的方法. 安装vim.c按照博客的方法 第一步:创建目录~/.vim 这个目录是用来 ...
- linux 计划任务 crontab 简单用法
添加计划任务: 方法1: crontab -e 方法2: vim /etc/crontab #这种方式进去的文件有个sample可供参考 查看已经启动的任务:crontab -l 查看运行状态:ser ...
- win7系统Oracle数据库本地备份
第一步:命令行登录 sqlplus sys/root@orcl as sysdba sqlplus 超级管理员/密码@数据对象 as sysdba 第二步:创建DIRECTORY create dir ...