题目链接

给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 莫队的更多相关文章

  1. CodeForces - 617E XOR and Favorite Number 莫队算法

    https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry,  问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...

  2. Codeforces 617E XOR and Favorite Number莫队

    http://codeforces.com/contest/617/problem/E 题意:给出q个查询,每次询问区间内连续异或值为k的有几种情况. 思路:没有区间修改,而且扩展端点,减小端点在前缀 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. Codeforces 617E XOR and Favorite Number(莫队算法)

    题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减 ...

  7. XOR and Favorite Number CodeForces - 617E(前缀异或+莫队)

    题意原文地址:https://blog.csdn.net/chenzhenyu123456/article/details/50574169 题意:有n个数和m次查询,每次查询区间[l, r]问满足a ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. C# GridView弹出窗口新增行 删除行

    <%@ Page Language="C#" AutoEventWireup="true" EnableViewState="true" ...

  2. BST的删除

    #include<iostream> #include<math.h> #include<stdio.h> #include<stdlib.h> #in ...

  3. http keepalive and tcpkeepalive

    http keepalive 减少tcp 连接 (三次握手的消耗) tcp keepalive 检测死链接的 session 在tcp连接中, src_ip + src_port + dest_ip ...

  4. windows中copy命令详解

    功能:将一份文件或者多份文件复制到另一个位置 用法: copy [/D] [/V] [/N] [/Y|/-Y] [/Z] [/A | /B] source [/A | /B] [+ source [/ ...

  5. 几个DOM属性

    1,childNodes,包含这个元素全部子元素的数组: 2,nodeType, 元素节点的nodeType属性值是1:  属性节点............................2:  文本 ...

  6. MVC自学第一课

    了解传统的ASP.NET WebForm ASP.NET 在02年问世,给Web开发领域带来了巨大转变.下图描述了当时微软的技术堆栈. ASP.NET WebForm 技术堆栈 (注:此图的含义为,W ...

  7. FAQ:Python 断点调试

    Python程序调试:断点调试是必须有的功能,以Pycharm开发工具为例: 一.理论知识: 1. step into(F7)就是单步执行,遇到子函数就进入并且继续单步执行: 2  step over ...

  8. http://webhelp.esri.com/arcgisexplorer/2500/zh-CN/index.html#add_raster_data.htm

    http://webhelp.esri.com/arcgisexplorer/2500/zh-CN/index.html#add_raster_data.htm

  9. DELL服务器安装Windows server 2003---解决找不到安装在计算机上的硬盘驱动器 安装无法

    安装Windows server 2003系统,本以为改改BIOS配置“改为从光驱启动优先”很容易搞定的.没想到系统安装过程中碰到“找不到安装在计算机上的硬盘驱动器安装无法继续,要退出请按F3”问题, ...

  10. VC2008如何生成及使用DLL(完整版)

    生成.使用DLL看起来简单,但做起来才发现还是有一些地方需要注意的. 1. 打开VS2008,新建一个VC工程,选择Win32类型,Win32项目: 2. 应用程序类型选择DLL,附加选项选择到处符号 ...