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
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.
Input
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.
Output
Print m lines, answer the queries in the order they appear in the input.
Sample Input
6 2 3
1 2 1 1 0 3
1 6
3 5
Sample Output
7
0
Hint
题意
给你n个数,然后M次询问,问你l,r区间内有多少对数,使得a[i]^a[j] = k
题解:
无修改,而且可以知道[l,r]可以O(1)就出[l-1,r],[l,r+1],[l+1,r],[l,r-1]的数据的
所有很显然的莫队算法搞一搞就好了
直接大暴力,注意不能再带log,所以直接开数组存就好了
注意,数组得开大一点哦
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 120010;
int a[maxn],pos[maxn];
long long ans,flag[5000000];
long long Ans[maxn];
int k;
struct query
{
int l,r,id;
}Q[maxn];
bool cmp(query a,query b)
{
if(pos[a.l]==pos[b.l])
return a.r<b.r;
return pos[a.l]<pos[b.l];
}
void Updata(int x)
{
ans+=flag[a[x]^k];
flag[a[x]]++;
}
void Delete(int x)
{
flag[a[x]]--;
ans-=flag[a[x]^k];
}
int main()
{
int n,m;
scanf("%d%d%d",&n,&m,&k);
int sz =ceil(sqrt(1.0*n));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
pos[i]=(i-1)/sz;
}
for(int i=1;i<=n;i++)
a[i]^=a[i-1];
for(int i=1;i<=m;i++)
{
scanf("%d%d",&Q[i].l,&Q[i].r);
Q[i].id = i;
}
sort(Q+1,Q+1+m,cmp);
int l = 1,r = 0;
ans=0;
flag[0]=1;
for(int i=1;i<=m;i++)
{
int id = Q[i].id;
while(r<Q[i].r)
{
r++;
Updata(r);
}
while(l>Q[i].l)
{
l--;
Updata(l-1);
}
while(r>Q[i].r)
{
Delete(r);
r--;
}
while(l<Q[i].l)
{
Delete(l-1);
l++;
}
Ans[id]=ans;
}
for(int i=1;i<=m;i++)
printf("%lld\n",Ans[i]);
}
Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法的更多相关文章
- 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 莫队板子
#include<bits/stdc++.h> using namespace std; <<; struct node{ int l,r; int id; }q[N]; in ...
- 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/contest/617/problem/E 题目大意:有n个数和m次查询,每次查询区间[l, r]问满足ai ^ ai+1 ^ ... ^ aj ...
- Codeforces Round #340 (Div. 2) E. XOR and Favorite Number
time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standa ...
- 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 ...
- 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 XOR and Favorite Number 莫队算法
https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry, 问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...
- [Codeforces Round #340 (Div. 2)]
[Codeforces Round #340 (Div. 2)] vp了一场cf..(打不了深夜的场啊!!) A.Elephant 水题,直接贪心,能用5步走5步. B.Chocolate 乘法原理计 ...
随机推荐
- SQL语句构建器类
问题 Java程序员面对的最痛苦的事情之一就是在Java代码中嵌入SQL语句.这么来做通常是由于SQL语句需要动态来生成-否则可以将它们放到外部文件或者存储过程中.正如你已经看到的那样,MyBatis ...
- PHP 新建动态类的代码
$testObject=(object)array(); $testObject->first="firstValue"; var_dump($testObject); $t ...
- Cracking the Code Interview 4.3 Array to Binary Tree
Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal hei ...
- Application Cache
轉發處:http://www.cnblogs.com/blackbird/archive/2012/06/12/2546751.html HTML5提供了一系列的特性来支持离线应用: applicat ...
- 解读Cardinality Estimation<基数估计>算法(第一部分:基本概念)
基数计数(cardinality counting)是实际应用中一种常见的计算场景,在数据分析.网络监控及数据库优化等领域都有相关需求.精确的基数计数算法由于种种原因,在面对大数据场景时往往力不从心, ...
- 【开源项目之路】jquery的build问题
在刚开始clone了jquery到本地build的时候,就遇到了问题. “ENORESTARGET No tag found that was able to satisfy ...” 提示为bowe ...
- PHP代码格式化批量脚本
@echo off echo please input phpCB url: set /p input= cd /d "E:\tools\phpCB\" phpCB --space ...
- 使用Sunny-grok实现内网转发
Sunny-grok 申请地址:http://www.ngrok.cc ngrok.cfg配置: server_addr: "server.ngrok.cc:4443" auth_ ...
- Xcode升级后插件失效的原理与修复办法
转载:http://joeshang.github.io/2015/04/10/fix-xcode-upgrade-plugin-invalid/ Xcode 的插件大大丰富了 Xcode 的功能,而 ...
- windows平台下,快速删除所有.svn文件夹
新建一个注册表文件名为:DELSVN.reg编辑其内容如下: Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Cla ...