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 ...
随机推荐
- HDU 2064 汉诺塔III (递推)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2064 约19世纪末,在欧州的商店中出售一种智力玩具,在一块铜板上有三根杆,最左边的杆上自上而下.由小到 ...
- sql 思路
先 django 定好sql框架 再 sqlalchemy 根据框架写...
- scrapy instantiation
start from scrapy.cmdline import execute execute(['scrapy', 'crawl', 'jokespider']) items.py import ...
- python使用super()调用父类的方法
如果要在子类中引用父类的方法,但是又需要添加一些子类所特有的内容,可通过类名.方法()和super()来调用父类的方法,再个性化子类的对应函数. 直接使用类名.方法()来调用时,还是需要传入self为 ...
- mysql常用的操作命令
在mysql中以半角分号;作为命令的结束符 查看当前系统包含的数据库:mysql> show databases [like ' ']; 从一个数据库切换到另一个:mysql>use ol ...
- 统计py文件中的代码行
希望是输入一个合法的文件夹的路径,然后代码自动读取该文件夹下的每个py结尾的文件内的代码行数,最后汇总一个数,但现在只是有思路,却没时间写,这是能读取同级文件下的某个文件, with open('te ...
- c#简单案例--单位转换器
经过几天学习,写出了一个简单的winform应用程序,贴出源码,以备不时之需. 软件启动后的界面如下图所示: 如图,该程序由6个label.8个comboBox.8个textBox和4个button组 ...
- Idea 破解教程 (2018.11-29最新版)
一.下载安装idea,这里不做赘述.官网-https://www.jetbrains.com/idea/ 二.安装完idea后,需要激活使用.这里需要先下载个工具包 ,下载网址http://idea. ...
- kaggle竞赛_mnist_10%
主要是通过mnist了解kaggle的操作细节,最终这里的结果为: 引入必须的库¶ import pandas as pd import numpy as np import matplotlib ...
- RAC +MVVM
https://blog.csdn.net/capf_sam/article/details/60960530 https://blog.csdn.net/capf_Sam/article/detai ...