Codeforces_617E: XOR and Favorite Number(莫队算法)
题意大致是说,给出一个长为n(n<=1e5)的数组,给定一个k(k<=1e6),给出m(m<=1e5)个询问,每组询问中回答 从a_l到a_r有多少个连续的子序列满足异或和等于k
这里采用莫队的方法
使用普通莫队的前提:对于序列上的区间询问问题,如果从 [l, r][l,r] 的答案能够 O(1) 扩展到 [l - 1, r], [l + 1, r],[l, r + 1],[l, r - 1][l−1,r],[l+1,r],[l,r+1],[l,r−1] 的答案,那么可以在 O(n√n) 的复杂度内求出所有询问的答案。
降低复杂度的环节:离线处理询问,对每个询问按照 l/sz为第一关键字,r为第二关键字 由小到大排序,这样使复杂度降为了
O(n√n) ,证明略。
下面附上来自jlx的代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL; const int maxn=1e5+;
const int maxv=<<;
int pre[maxn],pos[maxn];
int cnt[maxv],k;
LL ans[maxn];
int L=,R=;
LL Ans; struct Query
{
int l,r,id;
bool operator<(const Query& b) const //pos[l]为第一关键字,r为第二
{
if(pos[l]==pos[b.l]) return r<b.r;
return pos[l]<pos[b.l];
} //按照如此排出来的顺序,可以降低复杂度
}Q[maxn]; void add(int x)
{
Ans+=cnt[pre[x]^k];
++cnt[pre[x]];
}
void del(int x)
{
--cnt[pre[x]];
Ans-=cnt[pre[x]^k];
} int main()
{
ios::sync_with_stdio(false);
int n,m;
cin>>n>>m>>k;
int sz=sqrt(n); //sz:块的大小
for(int i=;i<=n;i++)
{
cin>>pre[i];
pre[i]^=pre[i-];
pos[i]=i/sz;
}
for(int i=;i<=m;i++)
{
cin>>Q[i].l>>Q[i].r;
Q[i].id=i;
}
sort(Q+,Q++m);
Ans=;
cnt[]=;
for(int i=;i<=m;i++)
{
while(L>Q[i].l)
{
--L;
add(L-);
}
while(L<Q[i].l)
{
del(L-);
++L;
}
while(R>Q[i].r)
{
del(R);
--R;
}
while(R<Q[i].r)
{
++R;
add(R);
}
ans[Q[i].id]=Ans;
}
for(int i=;i<=m;i++)
cout<<ans[i]<<endl;
}
Codeforces_617E: XOR and Favorite Number(莫队算法)的更多相关文章
- Codeforces617 E . XOR and Favorite Number(莫队算法)
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...
- 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 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 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 - 617E XOR and Favorite Number 莫队算法
https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry, 问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...
- Codeforces 617E XOR and Favorite Number莫队
http://codeforces.com/contest/617/problem/E 题意:给出q个查询,每次询问区间内连续异或值为k的有几种情况. 思路:没有区间修改,而且扩展端点,减小端点在前缀 ...
- CODEFORCES 340 XOR and Favorite Number 莫队模板题
原来我直接学的是假的莫队 原题: Bob has a favorite number k and ai of length n. Now he asks you to answer m queries ...
- E. XOR and Favorite Number 莫队 2038: [2009国家集训队]小Z的袜子(hose)
一直都说学莫队,直到现在才学,训练的时候就跪了 T_T,其实挺简单的感觉.其实训练的时候也看懂了,一知半解,就想着先敲.(其实这样是不好的,应该弄懂再敲,以后要养成这个习惯) 前缀异或也很快想出来 ...
- codeforces 617E. XOR and Favorite Number 莫队
题目链接 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k. 维护一个前缀异或值就可以了. 要注意的是 区间[l ...
- Codeforces Round #340 (Div. 2) E XOR and Favorite Number 莫队板子
#include<bits/stdc++.h> using namespace std; <<; struct node{ int l,r; int id; }q[N]; in ...
随机推荐
- Python3网络爬虫
# 最近在实验楼学习了爬取妹子图,发现在运行的时候不是很流畅,有些图片下 1 # coding: utf-8 # coding: utf-8 import re import threading fr ...
- ASP.NET MVC5(三):表单和HTML辅助方法
表单的使用 Action和Method特性 Action特性用以告知浏览器信息发往何处,因此,Action特性后面需要包含一个Url地址.这里的Url地址可以是相对的,也可以是绝对的.如下Form标签 ...
- angular表单验证实例----可用的代码
前段时间,公司做一个单页面,就是一个表单验证,早开始在菜鸟教程上关注了angular,所以下派上用场了 angular里面对于表单验证,设置了很多指令. 也就是说不用自己写一些逻辑,直接绑定指令就行. ...
- JavaScript事件与例子
事件,就是预先设置好的一段代码,等到用户触发的时候执行. 一:常见的事件: 1.关于鼠标的事件 onclick 鼠标单击触发 ondblclick 鼠标双击触发 onmouseover 鼠标移上触发 ...
- 转化来的图标用法symbol引用‘font-class引用及Unicode引用
- 由SpringMVC中RequetContextListener说起
零.引言 RequetContextListener从名字结尾Listener来看就知道属于监听器. 所谓监听器就是监听某种动作,在其开始(初始化)和结束(销毁)的时候进行某些操作. 由此可以猜测:该 ...
- 盒子模型,定位技术,负边距,html5 新增标签
盒子模型 /*[margin 外边距] margin属性最多四个 1.只写一个值,四个方向的margin均为这个值 2.写两个值:上,右两个方向,下默认=上,右 默认=左 3.写三个值:上.右.下三个 ...
- js脚本中try与cache捕获异常处理
<script type="text/javascript"> function add_reason(elm){ try{ var pp=$('.pp').val() ...
- SpringMvc+Spring3+MyBatis整合
1.MyBatis 例子 首先,单独使用MyBatis时: import java.io.IOException; import java.io.Reader; import org.apache.i ...
- JAVA基础——内部类详解
JAVA内部类详解 在我的另一篇java三大特性的封装中讲到java内部类的简单概要,这里将详细深入了解java内部类的使用和应用. 我们知道内部类可分为以下几种: 成员内部类 静态内部类 方法内部类 ...