题目链接: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. JustOj 1414: 潘神的排序

    题目描述 老潘,袁少,小艾都是江理的大个子,他们想按身高排队,现在给你他们的身高,请你算出队伍中站在第二的有多高. 输入 输入三个整数,分别表示三个人的身高.(单位:纳米) 输出 输出身高排第二的身高 ...

  2. Python 监控脚本(硬盘、cpu、内存、网卡、进程)

    #磁盘使用率disk = psutil.disk_partitions()for i in disk:    print "磁盘:%s   分区格式:%s"%(i.device,i ...

  3. 使用Holer远程桌面登录家里电脑和公司内网电脑

    1. Holer工具简介 Holer exposes local servers behind NATs and firewalls to the public internet over secur ...

  4. Java连接数据库 #02# JDBC经典套路

    内容索引 LocalConnectionFactory.java LocalConnectionProxy.java ProfileDAO.java-2.0 ProfileDAOImpl.java-2 ...

  5. IntelliJ IDEA. Debug模式

    资料收集: https://www.bilibili.com/video/av6749471/?p=16 eclipse debug模式. 基础 Intellij Idea--Debug使用 Inte ...

  6. Linux中USB协议栈的框架简介

    文本旨在简单介绍一下Linux中USB协议栈的代码框架: 下图是USB协议栈相关数据结构的关系图: 下面结合上图看一下系统初始化的流程: 1.USB子系统初始化:\drivers\usb\core\u ...

  7. django rest framework跨表取值

  8. Golang并发编程优势与核心goroutine及注意细节

    Go语言为并发编程而内置的上层API基于CSP(communication sequential processes,顺序通信进程)模型.这就意味着显式锁都是可以避免的,比如资源竞争,比如多个进程同时 ...

  9. P3809 【模板】后缀排序

    P3809 [模板]后缀排序 从这学的 后缀数组sa[i]就表示排名为i的后缀的起始位置 x[i]是第i个元素的第一关键字 y[i]表示第二关键字排名为i的数,在第一关键字中的位置 #include& ...

  10. JS截取字符串中数字

    今天项目中需要在一个字符串中截取一个数字,然后数字参与运算.搜了一下,有好多好用的方式截取字符串. 1,使用parseInt() var str ="4500元"; var num ...