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. C# 成员变量和属性的区别

    之前一直在C#中使用这两者, 却一直不知道成员变量和属性还是不一样的两种概念. 不过说不一样, 也不是完全对. 简单举个例子: public class myclass { public string ...

  2. sqlServer 查询表中31到40的记录,考虑id不连续的情况

    SQL   查询表中31到40的记录,考虑id不连续的情况 写出一条sql语句输出users表中31到40记录(数据库为SQL Server,以自动增长的ID作为主键,注意ID可能不是连续的)? -- ...

  3. web请求的状态码

    摘录于  https://www.cnblogs.com/lovychen/p/6256343.html 1xx消息 这一类型的状态码,代表请求已被接受,需要继续处理.这类响应是临时响应,只包含状态行 ...

  4. 蒲公英(bzoj2724)(分块+区间众数)

    Input Output Sample Input 6 3 1 2 3 2 1 2 1 5 3 6 1 5 Sample Output 1 2 1 HINT \(n <= 40000\),$ m ...

  5. Django 使用getattr() 方法获取配置文件的变量值

    在django项目的开发过程中,有时需要获取配置文件里的变量值,可以通过下面这样的方式去进行获取 from django.conf import settings item = getattr(set ...

  6. Dynamic Programming-650. 2 Keys Keyboard

    Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...

  7. python 爬图 helloworld

    最近发现 吾志 上用户的头像都很个性,另外,对于没有把日记设为私密的用户,最后一天的日记是公开的,谁都可以查看. 所以,如果每天把所有可查看的日记爬一遍,那么-- 哈哈 以前对爬虫只是了解一点点,没有 ...

  8. Python(多进程multiprocessing模块)

    day31 http://www.cnblogs.com/yuanchenqi/articles/5745958.html 由于GIL的存在,python中的多线程其实并不是真正的多线程,如果想要充分 ...

  9. 二,PHP会话机制---session的基本使用

    1,思考:登录网站后,在每个网页都能拿到用户信息 (1) 使用超链接传递用户名,这样太繁琐了,不建议使用 . (2) 使用数据库,每打开一个页面都查询一次用户信息表,这样网页加载速度变慢,用户体验变差 ...

  10. Spring+Mybatis整合时 Failed to read candidate component class,Caused by:IllegalArgumentException

    Spring+Mybatis整合时Caused by: java.lang.IllegalArgumentException错误 org.springframework.beans.factory.B ...