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. 敏捷项目管理架构(APMF)

    研读许秀影博士的<敏捷项目管理:基础知识与应用实务>一书,其中提到传统项目管理与敏捷项目管理的混合管理模式—敏捷项目管理架构(Agile Project Management Framew ...

  2. N个不同球取出M个的组合个数求解

    Technorati 标签: 组合,概率 从N个不同的球中取出M个,一共有多少种取法? 这个问题是组合数据的基本问题,考虑拿出球是否放回,拿出去的球是否有序,它有4种变体: 不放回,有序: 不放回,无 ...

  3. PHP RBAC权限管理 基于角色的访问控制演示

    RBAC rbac:Role Based Access Controll,基于角色的访问控制. 今天理一理RBAC,话不多说,直接切入主题 功能需求: 权限管理(无限极) 角色管理(可以分配权限) 管 ...

  4. C# 获取Url 请求方式 域名 端口 路径

    Example there's an given url: http://localhost:4800/account/login 获取整个url地址: 在页面(cstml)中 Microsoft.A ...

  5. 微信小程序支付C#后端源码

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  6. Eclipse (JavaEE版)中修改web项目的访问路径

    访问路径,也就是指在浏览器中访问该web系统时的根路径,比如http://localhost:8080/xxxx/index.jsp  这里的xxxx. 也就是request.getContextPa ...

  7. Kafka运行环境优化实践

    Kafka高性能的特点及条件 Kafka是一个高吞吐量分布式消息中间件,并且提供了消息的持久化功能.其高可行有两个重要的特点: 利用了磁盘连续读写性能显著高于随机读写性能的特点 并发,将一个topic ...

  8. java中常见的异常

    空指针异常类:NullPointerException 类型强制转换异常:ClassCastException 数组负下标异常:NegativeArrayException 数组下标越界异常:Arra ...

  9. Android自定义View创建流程

    Android的framework提供了很多高质量的view,有时业务需求需要自定义View,其实现流程大致如下: 1.在values/attrs.xml中定义支持的自定义属性,示例如下:

  10. Binder AIDL中自定义类型传递的源码分析

    binder机制实现的IPC和共享内存的方式不同,它采取的是值拷贝的方式,即进程间传递的实体遵循Parcelable协议, Bp端负责向Parcel里写东西,Bn端负责从Parcel里读取还原,顺序是 ...