CoderForces-617B
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, ..., ajis 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.
Example
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
Note
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.
题意:这道题意思为:给你一行数,让你查找在这行数中有多少子区间满足其内的数亦或为K;
题解:可以让sum[i]表示从1亦或到i的值;sum[i]^=sum[i-1];
sum[r]=k^sum[l-1];(l,r分别表示左右端点)
AC代码为:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 4;
struct node
{
friend bool operator< (node x, node y)
{
if (x.pos == y.pos)return x.r<y.r;
return x.l<y.l;
}
int l, r, id;
int pos;
};
node qu[N];
int n, m, k;
int b[N], num[2 * N];
long long ans[N];
void solve()
{
memset(num, 0, sizeof(num));
int le = 1, ri = 0;
long long temp = 0;
for (int i = 1; i <= m; i++)
{
while (ri<qu[i].r)
{
ri++;
temp += num[b[ri] ^ k];
num[b[ri]]++;
}
while (ri>qu[i].r)
{
num[b[ri]]--;
temp -= num[b[ri] ^ k];
ri--;
}
while (le>qu[i].l - 1)
{
le--;
temp += num[b[le] ^ k];
num[b[le]]++;
}
while (le<qu[i].l - 1)
{
num[b[le]]--;
temp -= num[b[le] ^ k];
le++;
}
ans[qu[i].id] = temp;
}
}
int main()
{
b[0] = 0;
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++)
{
scanf("%d", &b[i]);
b[i] ^= b[i - 1];
}
int s = sqrt(n);
for (int i = 1; i <= m; i++)
{
scanf("%d%d", &qu[i].l, &qu[i].r);
qu[i].id = i;
qu[i].pos = qu[i].l / s;
}
sort(qu + 1, qu + m + 1);
solve();
for (int i = 1; i <= m; i++)
{
cout << ans[i] << "\n";
}
return 0;
}
CoderForces-617B的更多相关文章
- coderforces #387 Servers(模拟)
Servers time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- coderforces #384 D Chloe and pleasant prizes(DP)
Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- coderforces 731c
题目大意:给出m组数据,每组数据包括两个数Li与Ri,分别表示左右袜子的索引(下标),表示这一天要穿的袜子:而我们要使得每天穿的这两只袜子的颜色相同,所以可以改变袜子的颜色,每次只能改变一只袜子的颜色 ...
- coderforces 721b
题目描述: B. Passwords time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CoderForces 280B(记忆化搜索)
题目大意:一个纸牌游戏,52张纸牌排成一列,每张纸牌有面值和花色两种属性.每次操作可以用最后一张纸牌将倒数第二张或者倒数第四张替换,但前提是两张牌的花色或者面值相同.问最终能否只剩一张牌. 题目分析: ...
- codeforces 617B Chocolate
题意: 在给定01串中,问能分割成多少个子串?每个子串只有一个1. dp #include<iostream> #include<string> #include<alg ...
- Codeforces 617B:Chocolate(思维)
题目链接http://codeforces.com/problemset/problem/617/B 题意 有一个数组,数组中的元素均为0或1 .要求将这个数组分成一些区间,每个区间中的1的个数均为1 ...
- CoderForces 689A Mike and Cellphone (水题)
题意:给定一个手机键盘数字九宫格,然后让你判断某种操作是不是唯一的,也就是说是不是可以通过平移也能实现. 析:我的想法是那就平移一下,看看能实现,就四种平移,上,下,左,右,上是-3,要注意0变成8, ...
- CoderForces 518C Anya and Smartphone (模拟)
题意:给定一个手机,然后一共有 n 个app,告诉你每个屏幕最多放 k 个,现在要你运行 m 个app,每次都从第一个屏幕开始滑动,每运行一个,它就和前一个交换位置,第一个就不换了,现在问你要滑动多少 ...
- CoderForces 518D Ilya and Escalator (期望DP)
题意:给定 n 个人,在每一时刻一个人进入地铁的概率是 p,站着不动的概率是 1-p,然后问你 t 时间地铁里有多少人. 析:很明显这是一个期望DP,用d[i][j]表示 i 时刻 j 个人进入地铁的 ...
随机推荐
- python函数的基本语法<一>
函数: 一次定义,多次调用,函数可以变相看成变量函数的阶段: 1.定义阶段 2调用阶段 形参和实参: 定义阶段的参数叫形参,调用阶段的参数叫实参 函数的几种基本用法: #多变量 def test(na ...
- win7/win10系列的office安装与激活
Windows系列电脑安装office傻瓜式教程 一. 下载与安装 下载 (1).所需工具:迅雷 下载链接:http://xl9.xunlei.com/ 显示界面如下,点击“立即下载”即可,然后 ...
- linux中dd相关命令骚操作
一.dd如何快速将磁盘写满 方法一: dd if=/dev/zero of=/tmp/file bs=1G count=10 # 参数解释 1. if=文件名:输入文件名,缺省为标准输入.即指定源文件 ...
- CSS:CSS弹性盒子布局 Flexible Box
一.简介 flexbox:全称Flexible Box, 弹性盒子布局.可以简单实现各种伸缩性的设计,它是由伸缩容器和伸缩项目组成.任何一个元素都可以指定为flexbox布局.这种新的布局方案在200 ...
- 关键路径法(Critical Path Method, CPM)
1.活动节点描述及计算公式 通过分析项目过程中哪个活动序列进度安排的总时差最少来预测项目工期的网络分析. 产生目的:为了解决,在庞大而复杂的项目中,如何合理而有效地组织人力.物力和财力,使之在有限资源 ...
- ZeroC ICE中的对象
在ZeroC Ice中定义了三种基本对象类型. 它们分别是IceProxy::Ice::Object(于Ice/Proxy.h),Ice::Object(于Ice/Object.h)和Ice::Loc ...
- Unittest框架的从零到壹(一)
前言 Python中有非常多的单元测试框架,如unittest.pytest.nose.doctest等,Python2.1及其以后的版本已经将unittest作为一个标准模块放入Python开发包中 ...
- 扛把子组Scrum立会报告+燃尽图 07
此作业要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/8684 一.小组情况组长:迟俊文组员:宋晓丽 梁梦瑶 韩昊 刘信鹏队名:扛把 ...
- python3 之 文件read方法(read、readline、readlines)
目录 一.read方法 二.readline方法 三.readlines方法 正文 python3中,读取文件有三种方法:read().readline().readlines(). 此三种方法,均支 ...
- 2019-11-7:练习上传getshell,通过菜刀连接
一.使用phtstudy搭建环境 1,将emlog源码解压到phpstudy的www目录,复制emlog源码中的src所有文件到需要搭建emlog的目录中,修改emlog配置文件,输入好本机mysql ...