codeforces 617E. XOR and Favorite Number 莫队
给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k。
维护一个前缀异或值就可以了。
要注意的是 区间[l, r], 我们需要将pre[l-1]......pre[r]都加进去, pre[l-1]不能少。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 2e5+;
long long n, m, k, sum[maxn], ans[maxn], tmp, num[maxn*];
struct node
{
int l, r, block, id;
bool operator < (node a) const
{
if(block == a.block)
return r<a.r;
return block < a.block;
}
}q[maxn];
void add(int x) {
tmp += num[x^k];
num[x]++;
}
void del(int x) {
num[x]--;
tmp -= num[x^k];
}
int main()
{
cin>>n>>m>>k;
int BLOCK = sqrt(n*1.0);
for(int i = ; i<=n; i++) {
scanf("%I64d", &sum[i]);
sum[i] ^= sum[i-];
}
for(int i = ; i<m; i++) {
scanf("%d%d", &q[i].l, &q[i].r);
q[i].block = q[i].l/BLOCK;
q[i].id = i;
}
num[] = ;
sort(q, q+m);
int l = , r = ;
for(int i = ; i<m; i++) {
while(r<q[i].r) {
add(sum[++r]);
}
while(r>q[i].r) {
del(sum[r--]);
}
while(l<q[i].l) {
del(sum[l-]);
l++;
}
while(l>q[i].l) {
l--;
add(sum[l-]);
}
ans[q[i].id] = tmp;
}
for(int i = ; i<m; i++)
cout<<ans[i]<<endl;
return ;
}
codeforces 617E. XOR and Favorite Number 莫队的更多相关文章
- 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 ...
- 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 - 617E XOR and Favorite Number (莫队+前缀和)
Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is g ...
- Codeforces 617E XOR and Favorite Number(莫队算法)
题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减 ...
- XOR and Favorite Number CodeForces - 617E(前缀异或+莫队)
题意原文地址:https://blog.csdn.net/chenzhenyu123456/article/details/50574169 题意:有n个数和m次查询,每次查询区间[l, r]问满足a ...
- 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 ...
随机推荐
- sql2008中时间类型问题
DATEDIFF (DD ,@sdate ,getdate() ) eg30 计算从开始日期到今天的天数 datename(weekday,@sdate) eg星期三 查询那一天是星期几 SQL Se ...
- JavaScript引用类型之RegExp类型(正则表达式)
ECMAScript中使用RegExp来支持正则表达式.使用下面类似Perl的语法,就可以创建一个正则表达式. var expression=/pattern/flags; 如上代码: pattern ...
- 蓝桥杯算法训练<一>
一.图形显示 此题虽然简单,但是需啊哟注意的是,每个“*”后边有一个空格] 问题描述 编写一个程序,首先输入一个整数,例如5,然后在屏幕上显示如下的图形(5表示行数): * * * * * * * * ...
- 第9课_2_dbsoft安装
三 安装Oracle Database 数据库软件 1.上传数据库安装包到虚拟机上,unzip命令解压database软件,赋予正确的权限和属主,在以oracle身份登录进行图形界面安装 unzip ...
- EC读书笔记系列之5:条款9、条款10
条款9 绝不在构造和析构过程中调用virtual函数 记住: ★在构造和析构期间不要调用virtual函数,∵这类调用从不下降至derived class ---------------------- ...
- linux可执行文件目录的区别
/bin./sbin./usr/bin./usr/sbin目录的区别 在linux下我们经常用到的四个应用程序的目录是/bin./sbin./usr/bin./usr/sbin .而四者存放的文件一般 ...
- 在VS工程中,添加c/c++工程中外部头文件及库
在VS工程中,添加c/c++工程中外部头文件及库的基本步骤: 1.添加工程的头文件目录:工程---属性---配置属性---c/c++---常规---附加包含目录:加上头文件存放目录. 2.添加文件引用 ...
- Jexus 配置多个站点
一:jexus配置站点的文件在 siteconf文件夹中,里面有多少个配置文件,就可以配置多少个站点 如我的里面有3个配置文件,其中default是原始文件,site1和siteconf就是我网站的配 ...
- php 按列值合并数据
/* * PHP按值合并数组 * */ function my_array_merge(&$array1, &$array2) { $result = Array(); foreach ...
- 不可综合的verilog语句分析
前半部分转自http://www.cnblogs.com/Mrseven/articles/2247657.html,后半部分为自己测试结果. 基础知识:verilog 不可综合语句 (1)所有综合工 ...