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. 解压安装的tomcat, 使用chkconfig命令让tomcat 随机启动,tomcat 变为系统服务

    使用解压安装的tomcat包,命令行输入 service tomcat start 会报 tomcat: unrecognized service 错误提示,意思是说系统没有找到该服务. 好了,我们现 ...

  2. Mysql Bypass小结

    总结一些Bypass的方法, 1. 绕过空格过滤,使用注释/**/来替换 绕过安全狗简单的两个方法: /*'+'*/ /**a*/ 2.使用大小写绕过某些关键字的过滤 SeLeCT * From te ...

  3. Python时间戳与时间字符串互相转换实例代码

    #设a为字符串import timea = "2011-09-28 10:00:00" #中间过程,一般都需要将字符串转化为时间数组time.strptime(a,'%Y-%m-% ...

  4. C++ template —— 函数对象和回调(十四)

    本篇是本系列博文最后一篇,主要讲解函数对象和回调的相关内容.函数对象(也称为仿函数)是指:可以使用函数调用语法进行调用的任何对象.在C程序设计语言中,有3种类似于函数调用语法的实体:函数.类似于函数的 ...

  5. 使用dom4j解析xml为json对象

    import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j. ...

  6. TCP三次握手和四次挥手原理

  7. 【Java并发编程三】闭锁

    1.什么是闭锁? 闭锁(latch)是一种Synchronizer(Synchronizer:是一个对象,它根据本身的状态调节线程的控制流.常见类型的Synchronizer包括信号量.关卡和闭锁). ...

  8. 向Windows内核驱动传递用户层定义的事件Event,并响应内核层的通知

    完整的程序在下载:http://download.csdn.net/detail/dijkstar/7913249 用户层创建的事件Event是一个Handle句柄,和内核中的创建的内核模式下的KEV ...

  9. <转>ML 相关算法参考

    转自 国内外网站如果你想搜索比较新颖的机器学习资料或是文章,可以到以下网站中搜索,里面不仅包括了机器学习的内容,还有许多其它相关领域内容,如数据科学和云计算等.InfoWord:http://www. ...

  10. CSS改变插入光标颜色caret-color简介及其它变色方法(转)

    一.CSS改变输入框光标颜色的原生属性caret-color CSS caret-color属性可以改变输入框插入光标的颜色,同时又不改变输入框里面的内容的颜色. 例如: input { color: ...