题意:很多询问,求每个询问下,有多少个区间,异或=k。

分析:异或也有前缀和。[L,R] = pre[R] ^ pre[L-1];

莫队算法:是莫涛队长发明的,一种改良版的暴力离线算法。

首先将问题重新排序,有生成树的,有简单版的分块,然后通过一个区间去递推另个一区间的值。

这里需要记录一下flag[ pre[i] ] 的个数。

#include <bits/stdc++.h>

using namespace std;

const int maxn = <<;
int a[maxn]; struct Node {
int l,r,id;
}Q[maxn]; int pos[maxn];
long long ans[maxn];
long long flag[maxn]; bool cmp(Node a,Node b) {
if(pos[a.l]==pos[b.l])
return a.r < b.r;
return pos[a.l] < pos[b.l];
} int n,m,k;
int L = ,R = ;
long long Ans; void add(int x) {
Ans+=flag[a[x]^k];
flag[a[x]]++;
} void del(int x) {
flag[a[x]]--;
Ans-=flag[a[x]^k];
} int main()
{
scanf("%d%d%d",&n,&m,&k);
int sz = sqrt(n);
for(int i=; i <= n; i++) {
scanf("%d",&a[i]);
a[i] = a[i]^a[i-];
pos[i] = i/sz;
} for(int i=; i<=m ;i++) {
scanf("%d%d",&Q[i].l,&Q[i].r);
Q[i].id = i;
} sort(Q+,Q+m+,cmp);
flag[] = ; for(int i=; i <=m; i++) { while(L<Q[i].l) {
del(L-);
L++;
} while(L>Q[i].l) {
L--;
add(L-);
} while(R<Q[i].r) {
R++;
add(R);
} while(R>Q[i].r) {
del(R);
R--;
} ans[Q[i].id] = Ans;
} for(int i=; i <= m; i++)
printf("%I64d\n", ans[i]); return ;
}

E. XOR and Favorite Number的更多相关文章

  1. Codeforces617 E . XOR and Favorite Number(莫队算法)

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

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

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

  4. XOR and Favorite Number(莫队算法+分块)

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

  5. CF617E XOR and Favorite Number

    CF617E XOR and Favorite Number 已知一个序列 \(a_1,\ a_2,\ \cdots,\ a_n\) 和 \(k\) ,\(m\) 次询问给出 \(l,\ r\) ,求 ...

  6. XOR and Favorite Number (莫对算法)

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

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

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

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

  10. 题解-CF617E XOR and Favorite Number

    题面 CF617E XOR and Favorite Number 给定 \(n,m,k\) 和 \(n\) 个数的序列 \(a_i\),\(m\) 次求区间 \([l,r]\) 中异或值为 \(k\ ...

随机推荐

  1. superobject 设定排序方式

    (* * Super Object Toolkit * * Usage allowed under the restrictions of the Lesser GNU General Public ...

  2. Java 继承初探

    Java继承的基础 Java中,被继承的类叫做超类,继承超类的类叫子类.(一个子类亦可以是另一个类的超类) 继承一个类,只需要用关键字 extends 把一个类的定义合并到另一个类中就可以了. 例子中 ...

  3. sed命令——批量修改文件内容

    批量替换单个文件内容 命令格式:sed -i 's/旧内容/新内容/g' 文件路径 sed -i 's/oldString/newString/g' file 例如:我想替换cwx.txt文件中的 j ...

  4. 基于memcache的缓存机制的6个指令

    Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached ...

  5. TOJ 2814 Light Bulb

    Description Compared to wildleopard's wealthiness, his brother mildleopard is rather poor. His house ...

  6. [转]微信小程序安全浅析

    本文转自:http://blog.csdn.net/baize_security/article/details/54582854 引言 近期微信小程序重磅发布,在互联网界掀起不小的波澜,已有许多公司 ...

  7. hashlib库的使用

    这个模块实现了许多不同的安全哈希和消息摘要算法的通用接口.包括FIPS安全哈希算法SHA1.SHA224.SHA256.SHA384和SHA512(在FIPS 180-2中定义)以及RSA的MD5算法 ...

  8. 优化代码CPU层面

    今天在看<支撑处理器的技术>,其中,讲到了CPU流水线.在指令之间,如果下一条指令,需要用到上一条指令的结果,会影响到流水线的执行.书上给出了几种解决方案,一个是在指令中间插入一下无关的指 ...

  9. lua继承

    lua中其实是没有类的,有的只是表(table) lua查找一个表元素时的规则,其实就是如下3个步骤: 1.在表中查找,如果找到,返回该元素,找不到则往下看: 2.判断该表是否有元表,如果没有元表,返 ...

  10. nodejs进阶(7)—async异步流程控制

    Async介绍 Async是一个流程控制工具包,提供了直接而强大的异步功能.基于Javascript为Node.js设计,同时也可以直接在浏览器中使用. Async提供了大约20个函数,包括常用的 m ...