XOR and Favorite Number(莫队算法+分块)
4 seconds
256 megabytes
standard input
standard output
Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is given by a pair li and ri and asks you to count the number of pairs of integers i and j, such that l ≤ i ≤ j ≤ r and the xor of the numbers ai, ai + 1, ..., aj is equal to k.
The first line of the input contains integers n, m and k (1 ≤ n, m ≤ 100 000, 0 ≤ k ≤ 1 000 000) — the length of the array, the number of queries and Bob's favorite number respectively.
The second line contains n integers ai (0 ≤ ai ≤ 1 000 000) — Bob's array.
Then m lines follow. The i-th line contains integers li and ri (1 ≤ li ≤ ri ≤ n) — the parameters of the i-th query.
Print m lines, answer the queries in the order they appear in the input.
6 2 3
1 2 1 1 0 3
1 6
3 5
7
0
5 3 1
1 1 1 1 1
1 5
2 4
1 3
9
4
4
In the first sample the suitable pairs of i and j for the first query are: (1, 2), (1, 4), (1, 5), (2, 3), (3, 6), (5, 6), (6, 6). Not a single of these pairs is suitable for the second query.
In the second sample xor equals 1 for all subarrays of an odd length.
题意:给你一个大小为n的序列,然后给你一个数字k,再给出m组询问
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define X first
#define Y second
#define clr(u,v); memset(u,v,sizeof(u));
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=<<;
const int INF=0x3f3f3f3f;
ll pos[maxn];
ll flag[maxn],ans[maxn];
int a[maxn];
struct node
{
int l,r,id;
}Q[maxn];
bool cmp(node a,node b)
{
if (pos[a.l]==pos[b.l])
{
return a.r<b.r;
}
return pos[a.l]<pos[b.l];
}
int n,m,k;
int L=,R=;
ll Ans=;
void add(int x)
{
Ans+=flag[a[x]^k];
flag[a[x]]++;
}
void del(int x)
{
flag[a[x]]--;
Ans-=flag[a[x]^k];
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
int sz=sqrt(n);
for (int i=;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]^=a[i-];
pos[i]=i/sz;
}
for (int i=;i<=m;i++)
{
scanf("%d%d",&Q[i].l,&Q[i].r);
Q[i].id=i;
}
flag[]=;
sort(Q+,Q+m+,cmp);
for (int i=;i<=m;i++)
{
while (L<Q[i].l)
{
del(L-);
L++;
}
while (L>Q[i].l)
{
L--;
add(L-);
}
while (R<Q[i].r)
{
R++;
add(R);
}
while (R>Q[i].r)
{
del(R);
R--;
}
ans[Q[i].id]=Ans;
}
for (int i=;i<=m;i++)
printf("%I64d\n",ans[i]);
return ;
}
2016-09-25 03:31:38
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 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 莫队算法
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 - 617E XOR and Favorite Number 莫队算法
https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry, 问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...
- 【bzoj3585/bzoj3339】mex/Rmq Problem 莫队算法+分块
原文地址:http://www.cnblogs.com/GXZlegend/p/6805283.html 题目描述 有一个长度为n的数组{a1,a2,...,an}.m次询问,每次询问一个区间内最小没 ...
- 【bzoj3809/bzoj3236】Gty的二逼妹子序列/[Ahoi2013]作业 莫队算法+分块
原文地址:http://www.cnblogs.com/GXZlegend/p/6805252.html bzoj3809 题目描述 Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了 ...
- 【BZOJ】2038: [2009国家集训队]小Z的袜子(hose)(组合计数+概率+莫队算法+分块)
http://www.lydsy.com/JudgeOnline/problem.php?id=2038 学了下莫队,挺神的orzzzz 首先推公式的话很简单吧... 看的题解是从http://for ...
- BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块
Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数. 为了方便,我们 ...
随机推荐
- vps使用(centos)2
SVN Server搭建 1.安装软件包 [root@cloud ~]# yum install subversion mysql-server httpd mod-dav_svn mod-perl ...
- Android——另外一种增删查改的方式(ContentProvider常用)
以下介绍另外一种增删查改的方式 package com.njupt.sqllist; import java.util.ArrayList; import java.util.List; import ...
- Asp.Net 网站访问人数及在线人数
利用Application对象和Session对象可以统计历史访问人数和当前在线人数. 在会话开始和结束时,一定要进行加锁和解锁操作.由于多个用户可以共享Application对象,因此加锁是必要的, ...
- Elasticsearch 5.0 磁盘空间节省策略的认识
前言:本文是当时QQ群员讨论磁盘空间如何优化,我搜了下类似的文章,结合官方文档做了一些总结 参考文章1 参考文章2 如果你有疑问,可以联系我参与讨论,或者去原文查看. NOTE: 磁盘空间节省问题,是 ...
- Kattis - Fenwick Tree(树状数组区间更新单点求值)
Fenwick Tree Input The first line of input contains two integers NN, QQ, where 1≤N≤50000001≤N≤500000 ...
- angular1 实现页面切换及tag页面
tag页面实现<div class="b_gray" style="padding-left:24px;border-bottom:1px solid #ccc&q ...
- netty(4)高级篇-Websocket协议开发
一.HTTP协议的弊端 将HTTP协议的主要弊端总结如下: (1) 半双工协议:可以在客户端和服务端2个方向上传输,但是不能同时传输.同一时刻,只能在一个方向上传输. (2) HTTP消息冗长:相比于 ...
- button 自动刷新当前页面
button请始终为按钮规定 type 属性.Internet Explorer 的默认类型是 "button",而其他浏览器中(包括 W3C 规范)的默认值是 "sub ...
- 中文版的jqGrid实例大全
中文版的jqGrid实例大全 http://blog.mn886.net/jqGrid/
- ajax无法跳转页面的问题,
将return true去掉!