题目链接

给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. [转]Visual Studio 2008中如何比较二个数据库的架构【Schema】和数据【Data】并同步

    使用场景: 在团队开发中,每一个人都有可能随时更新数据库,这时候数据库中数据和架构等信息都会发生变化.如果更新不及时,就会发生数据错误或数据丢失的风险,影响团队的开发效率和 项目进度,这时候我们该怎么 ...

  2. Nodejs随笔(一):Hello World!

    声明:本人用的是Ubuntu 14.04 LTS 系统. 一.Nodejs安装: <1>直接apt-get安装,最简单:sudo apt-get install nodejs <2& ...

  3. oracle form 触发器执行顺序及键定义[Z]

    1当打开FORM时: (1)PRE-FORM (2)PRE-BLOCK(BLOCK级) (3)WHEN-NEW-FORM-INSTANCE (4)WHEN-NEW-BLOCK-INSTANCE (5) ...

  4. php curl详解用法[真的详解]

    目前为目最全的CURL中文说明了,学PHP的要好好掌握.有很多的参数.大部份都很有用.真正掌握了它和正 则,一定就是个采集高手了. 通用函数: function curl_file_get_conte ...

  5. leetcode Maximum Depth of Binary Tree python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  6. threadid=1: thread exiting with uncaught exception (group=0x40db8930)

    异常信息如下: 07-26 17:23:49.521: W/dalvikvm(29229): threadid=1: thread exiting with uncaught exception (g ...

  7. Windows2008RT搭建VPN服务器

    总结一下2008系统搭建VPN的步骤和过程,自己有个人网站和服务要通过互联网发布出来.服务器放在自己家里,宽带是民用的.也就产生了服务发布的一些问题.用无法映射出真实的公网IP,或是一些其他内部的问题 ...

  8. MYSQL 引擎的情况

    方法 1: show engine innodb status; ------------------------------------------------------------------- ...

  9. (15)Visual Studio中使用PCL项目加入WCF WebService参考

    原文 Visual Studio中使用PCL项目加入WCF WebService参考 Visual Studio中使用PCL项目加入WCF WebService参考 作者:Steven Chang 2 ...

  10. c++链接数据库测试,中文有问题

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <Windows.h& ...