https://vjudge.net/problem/CodeForces-617E

题意,给你n个数ax,m个询问Ly,Ry,  问LR内有几对i,j,使得ai^...^ aj =k.

题解:第一道莫队题。

技巧:前缀亦或。flag数组:利用XOR的性质。

   莫队的区间排序及处理。

   id的处理:因为排序打乱了询问顺序,所以用id记录原来的顺序。

四个月后的补充: 关于XOR的性质: x^k=y <=> x^y=k. 所以每次读入一个a[i],Ans +=flag[a[i]^k]. It would be better if we call flag a 'cnt' array!

坑点:打错了一个字母,结果tle了好久

//#define _CRT_SECURE_NO_WARNINGS

#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
struct node {
int l, r, id; }Q[maxn];
long long ans[maxn];
long long flag[];
int a[maxn],pos[maxn];
int n, m, k;
int L = , R=;
long long Ans=;
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]; }
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 (R < Q[i].r) {
R++;
add(R);
}
while (L > Q[i].l) {
L--;
add(L - );
}
while (R > Q[i].r) {
del(R);
R--; }
while (L < Q[i].l) {
del(L - );
L++;
}
ans[Q[i].id] = Ans;
//
}
for (int i = ; i <= m; i++)
cout << ans[i] << endl; }

CodeForces - 617E XOR and Favorite Number 莫队算法的更多相关文章

  1. Codeforces 617E XOR and Favorite Number莫队

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

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

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

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

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

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

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

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

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

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

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

  9. CodeForces 617E XOR and Favorite Number

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

随机推荐

  1. mysql中concat 和 group_concat()的用法

    一.CONCAT()函数CONCAT()函数用于将多个字符串连接成一个字符串.使用数据表Info作为示例,其中SELECT id,name FROM info LIMIT 1;的返回结果为+----+ ...

  2. 【代码审计】CLTPHP_v5.5.3后台目录遍历漏洞分析

      0x00 环境准备 CLTPHP官网:http://www.cltphp.com 网站源码版本:CLTPHP内容管理系统5.5.3版本 程序源码下载:https://gitee.com/chich ...

  3. Ansible Playbook 使用变量

    如何在 Playbook 中定义并使用变量: vars: - user: "test" # 定义变量 tasks: - name: create user user: name=& ...

  4. [转]redis的三种启动方式

    来源:https://www.cnblogs.com/pqchao/p/6549510.html redis的启动方式1.直接启动  进入redis根目录,执行命令:  #加上‘&’号使red ...

  5. IDEA 单元测试 导入JUnit4到项目

    一.IDEA自带JUnit4的jar包,现在让我们来导入. Step 1. IDEA最上面一栏的菜单栏中,选File->Project Structure(从上往下第11个),弹出窗口左边有一个 ...

  6. IOS 怎么用UIScrollView来滚动和缩放他的内容第一篇

    本篇文章来自于互联网资料翻译 UIScrollView是在IOS最有用的控件之一.他是一个来展现超过一个屏幕的内容的很好的方式.下面有很多的技巧来使用他. 这篇文章就是关于UIScrollView的, ...

  7. Python 编码规范(Google)

    Python 编码规范(Google) https://blog.csdn.net/q469587851/article/details/54096093 Python 风格规范(Google) 本项 ...

  8. Matlab当中size() length()等函数讲解

    在Matlab中: size:获取数组的行数和列数 length:数组长度(即行数或列数中的较大值) numel:元素总数. s=size(A): 当只有一个输出参数时,返回一个行向量,该行向量的第一 ...

  9. nginx(一)----ubuntu14.04下安装nginx

    /** * lihaibo * 文章内容都是根据自己工作情况实践得出. *如有错误,请指正 *转载请注明出处 */ 此文章中用到的软件下载地址: 链接: http://pan.baidu.com/s/ ...

  10. 记录一次OOM排查经历(一)

    一.经历概要 程序里有个跑数据的job,这个job的主要功能是往数据库写假数据. 既需要跑历史数据(传给job的日期是过去的时间),也需要能够上线后,实时跑(十秒钟触发一次,传入触发时的当前时间). ...