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. RedHat6.5-Linux安装telnet服务

    1 下载以下三个包 telnet-0.17-47.el6.x86_64.rpm(telnet客户端) telnet-server-0.17-47.el6.x86_64.rpm(telnet服务端) x ...

  2. 5 -- Hibernate的基本用法 --4 9 其他常用的配置属性

    Hibernate其他常用的配置属性: ⊙ hibernate.show_sql : 是否在控制台输出Hibernate持久化操作底层所使用的SQL语句.只能为true和false两个值. ⊙ hib ...

  3. bat批处理设置静态、动态、ping、查看网络配置

    @echo off :startIP set /p source=S or D or C or P or E: echo source:%source% if /i "%source%&qu ...

  4. 【VirtualBox】设置NAT端口映射-SSH登录

    参考地址: http://www.cnblogs.com/sky20081816/p/3951889.html http://blog.csdn.net/jiangsyace/article/deta ...

  5. 数据库iops的理解

    想购买阿里云的RDS mysql,想请教下最大连接数是请求数吗?如下图,600最大可支持连接数,那一个页面查询30次,20个人同时请求,数据库不就超载了么?(内存2400MB,专用数据服务器,只能支持 ...

  6. C语言从零开始(十四)-字符串处理

    在软件开发过程中,字符串的操作相当频繁.在标准C语言库中提供了很多字符串处理的函数.今天我们来介绍一些常用的字符串处理函数.1. 字符串输入输出1.1 printf() scanf() 之前我们学习过 ...

  7. tts文本转语音服务

    1. 谷歌tts http://translate.google.cn/translate_tts?ie=UTF-8&tl=zh-CN&q=谷歌 2. 百度tts http://tts ...

  8. codeforces水题100道 第二十六题 Codeforces Beta Round #95 (Div. 2) A. cAPS lOCK (strings)

    题目链接:http://www.codeforces.com/problemset/problem/131/A题意:字符串大小写转换.C++代码: #include <cstdio> #i ...

  9. web_qianduan

    <!DOCTYPE html><html lang="zh"><head><meta charset="UTF-8"& ...

  10. Foxmail邮箱最新应用指南二

    Foxmail邮箱最新应用指南二 1.打开Foxmail主界面—工具—账号管理,或者鼠标右击任何已有账号—属性,弹出账号管理窗口,点击左下角的“新建”按钮: 2.输入邮箱地址,下一步→选择邮箱类型(I ...