题目链接: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. myBatis框架之入门(一)

    什么是框架 框架就是一个架子,表演节目,舞台已经搭建好,表演什么节目,看自己的需求了. 框架是一个半成品,对于Java语言来说,框架就是封装了别人的代码.在框架的基础上我们在进一步开发,拿来主义. 框 ...

  2. 禁止单个IP或ip段访问

    //IP禁止判断接口,返回true则为找到 function checkIp($ip, $ipbanned) { $ipbannedFlag = false; if (!empty($ipbanned ...

  3. Golang指针基本介绍及使用案例

    一.指针的相关概念说明 变量:是基本类型,变量存的就是值,也叫值类型 地址:用于引用计算机的内存地址,可理解为内存地址的标签,通俗一点讲就是一间房在小区里的门牌号.如下图① 指针:指针变量存的是一个地 ...

  4. Vue基础进阶 之 计算属性的使用

    计算属性的基本使用 初始小示例: 代码: window.onload = () =>{ new Vue({ el:'div', data:{ msg:'' } }) } </script& ...

  5. P4577 [FJOI2018]领导集团问题

    P4577 [FJOI2018]领导集团问题 我们对整棵树进行dfs遍历,并用一个multiset维护对于每个点,它的子树可取的最大点集. 我们遍历到点$u$时: 不选点$u$,显然答案就为它的所有子 ...

  6. 10: VMware中扩展根分区

    1.1 添加一块硬盘 1.先给VMware添加一块60G硬盘 2.必须重启虚拟机才能识别到新加磁盘 fdisk -l        # 查看刚刚添加的硬盘 3.查看当前磁盘使用情况 df -hl   ...

  7. 闭包引起的onclick不起作用

    问题描述:在html页面绑定onclick="cli()" 方法,定义在$(function( function cli(){} ))不起作用 $(function(){ func ...

  8. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem A - B

    Pronlem A In a small restaurant there are a tables for one person and b tables for two persons. It i ...

  9. CocoaPods创建自己的公开库、私有库

    http://www.cocoachina.com/ios/20180308/22509.html

  10. DOM元素加载之前执行的jQuery代码

    <script type="text/javascript"> (function() { alert("DOM还没加载哦!"); })(jQuer ...