Description

题库链接1 题库链接2

已知一个长度为 \(n\) 的整数数列 \(a_1,a_2,\cdots,a_n\) ,给定查询参数 \(l,r\) ,问在 \([l,r]\) 区间内,有多少连续子序列满足异或和等于 \(k\) 。

CQOI 数据范围: \(1\leq n\leq 10^5, a_i,k\leq 10^5\)

CF 数据范围: \(1\leq n\leq 10^5, a_i,k\leq 10^6\)

Solution

撞题也是醉了...

莫队傻逼题,乱搞即可。

Code

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+5, SIZE = 1<<20; int n, m, k, lim, a[N], cnt[SIZE]; ll ans[N];
struct tt {
int l, r, id;
bool operator < (const tt &b) const {
return l/lim == b.l/lim ? r < b.r : l < b.l;
}
}b[N]; void work() {
scanf("%d%d%d", &n, &m, &k); lim = sqrt(n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), a[i] ^= a[i-1];
for (int i = 1; i <= m; i++) scanf("%d%d", &b[i].l, &b[i].r), --b[i].l, b[i].id = i;
sort(b+1, b+m+1);
int curl = 1, curr = 0; ll sum = 0;
for (int i = 1; i <= m; i++) {
int l = b[i].l, r = b[i].r;
while (curr < r) sum += cnt[a[++curr]^k], ++cnt[a[curr]];
while (curl > l) sum += cnt[a[--curl]^k], ++cnt[a[curl]];
while (curr > r) --cnt[a[curr]], sum -= cnt[a[curr--]^k];
while (curl < l) --cnt[a[curl]], sum -= cnt[a[curl++]^k];
ans[b[i].id] = sum;
}
for (int i = 1; i <= m; i++) printf("%lld\n", ans[i]);
}
int main() {work(); return 0; }

[CQOI 2018]异或序列&[Codeforces 617E]XOR and Favorite Number的更多相关文章

  1. [ CQOI 2018 ] 异或序列

    \(\\\) Description 给出一个长为 \(n\) 的数列 \(A\) 和 \(k\),多次询问: 对于一个区间 \([L_i,R_i]\),问区间内有多少个不为空的子段异或和为 \(k\ ...

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

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

  3. Codeforces 617E XOR and Favorite Number莫队

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

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

  5. codeforces 617E. XOR and Favorite Number 莫队

    题目链接 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k. 维护一个前缀异或值就可以了. 要注意的是 区间[l ...

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

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

  7. CodeForces 617E XOR and Favorite Number

    莫队算法. #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> ...

  8. Codeforeces 617E XOR and Favorite Number(莫队+小技巧)

    E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...

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

随机推荐

  1. 轻松转移github项目步骤

    之前有一些项目是托管在github上的,无奈github速度太慢,而且空间有限,还不能有私有项目.后来发现开源中国的git托管(git.oschina.net)还不错,可以托管1000个项目,而且可以 ...

  2. Java异常处理机制的秘密

    一.结论 这些结论你可能从未听说过,但其正确性是毋庸置疑的,不妨先看看: 1.catch中throw不一定能抛回到上一层,因为finally中的return会抑制这个throw 2.finally中t ...

  3. 【加密算法】DES

    一.简介 DES对称加密,是一种比较传统的加密方式,其加密运算.解密运算使用的是同样的密钥,信息的发送者和信息的接收者在进行信息的传输与处理时,必须共同持有该密码(称为对称密码),是一种对称加密算法. ...

  4. (C#)字符串反转

    方法一: public static string Reverse(string name) {     if (String.IsNullOrEmpty(name))       {        ...

  5. Monotone and Sorted Matrix Search ( Arithmetic and Algebra) CGAL 4.13 -User Manual

    monotone_matrix_search() and sorted_matrix_search() are techniques that deal with the problem of eff ...

  6. 移植Max中的控制器到Unity - 前言

    一切源自于一个ppt http://pan.baidu.com/s/1kT8x2V9 看到这个ppt之前,其实自己也想过将Max当中的一些约束移植到游戏中,一方面是那时候能力不足,另一方面还是自己不那 ...

  7. Windows 环境下使用强大的wget工具

    安装 下载[http://www.interlog.com/~tcharron/wgetwin.html] 解压到目录 比如我解压到D:\Tool\wget 添加wget环境变量,这样使用就更方便了, ...

  8. Android------------fragment数据传递

    一.activity向fragment的数值之间的传递 关键点:fragment.setArguments(bundle);---->activity发出的信息      Bundle bund ...

  9. falcon nodata 小坑一枚

    按照官方文档配置完一切正常,唯独 nodata, 明明有正常的数据,但是为什么 nodata 会认为是没收到呢 困扰许久,直到看了数据库中的数据才恍然大悟 falcon_portal库中的 hosts ...

  10. 对Spring 及SpringMVC的理解

    spring是一个轻型容器(light-weight Container),其核心是Bean工厂(Bean Factory),用以构造我们所需要的M(Model).在此基础之上,Spring提供了AO ...