题目链接:http://codeforces.com/contest/617/problem/E

题意:给出一个长度为n(1e5)的序列,有m(1e5)次操作,每次操作选择一个L-R区间,然后输出符合条件的i,j的个数:L<i<j<R,ai^a------^aj=k,k是一个给定的值

题解:莫队可做,首先,求一个前缀异或和,如果求ai^……^a[j]的值,那么sum[j]^sum[i-1]。莫队的思想是已知道[L,R]的值,可以在O(1)的时间内算出[L-1,R],[L,R-1]等等的值,

好,现在如果我们维护[L-R]区间,所有{sum[L-1],sum[R-1]}和{sum[L,sum[R]},去除中间重复的部分,然后维护{sum[L-1],sum[R]},这样就方便维护所有的值了。

另外呢,优于是异或,所以呢,中间异或的值很可能会大于1e6,所以标记数组要开的大一点。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ( << ) - ;
int A[maxn], pos[maxn], sz = ;
LL num[maxn], Ans[maxn], ans = ;
struct Edge
{
int l, r, id;
} E[maxn];
bool cmp (Edge a, Edge b)
{
if(pos[a.l] == pos[b.l]) return a.r < b.r;
return pos[a.l] < pos[b.l];
}
int L = , R = ;
int N, M, k;
void add(int x)
{
ans += num[A[x] ^ k];
num[A[x]]++;
}
void det(int x)
{
num[A[x]]--;
ans -= num[A[x] ^ k];
}
int main ()
{
scanf("%d %d %d", &N, &M, &k);
for(int i = ; i <= N; i++)
{
scanf("%lld", &A[i]);
A[i] ^= A[i - ];
pos[i] = i / sz;
}
for(int i = ; i <= M; i++)
{
scanf("%d %d", &E[i].l, &E[i].r);
E[i].id = i;
}
num[] = ;
sort(E + , E + + M, cmp);
for(int i = ; i <= M; i++)
{
while(L < E[i].l)
{
det(L - );
L++;
}
while(L > E[i].l)
{
L--;
add(L - );
}
while(R < E[i].r)
{
R++;
add(R);
}
while(R > E[i].r)
{
det(R);
R--;
}
Ans[E[i].id] = ans;
}
for(int i = ; i <= M; i++) printf("%lld\n", Ans[i]);
return ;
}

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

随机推荐

  1. css3中新增的样式使用方法

    在PC版开发中由于IE原因,我们很少用到css3,但随着平板和智能手机进入我们的生活,以及现在越来越流行,在手机版和平板版开发中我们就可以大胆的使用了,下面我们探讨常用几个css3属性: 1.css3 ...

  2. 16级第二周寒假作业E题

    Home_W的位运算4 TimeLimit:2000MS  MemoryLimit:128MB 64-bit integer IO format:%I64d Problem Description 给 ...

  3. PHP对象1: 创建对象与 $this

    <?php class perl{ public $name; function __construct($name){ echo '一个对象造好了<br/>'; $this-> ...

  4. 搭建linux+nginx+mysql+php环境

    yum install -y gcc gcc-c++  make zlib zlib-devel pcre pcre-devel  libjpeg libjpeg-devel libpng libpn ...

  5. js实现图片上传预览

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  6. 《深入理解Java虚拟机》笔记--第十二章、Java内存模型与线程

    主要内容:虚拟机如何实现多线程.多线程之间由于共享和竞争数据而导致的一系列问题及解决方案. Java内存模型:     Java内存模型的主要目标是定义程序中各个变量的访问规则,即在虚拟机中将变量存储 ...

  7. C++学习笔记--从虚函数说开去

    虚函数与纯虚函数: 虚函数:在某基类中声明为virtual并在一个或多个派生类中被重新定义的成员函数,virtual  函数返回类型  函数名(参数表){函数体;} ,实现多态性,通过指向派生类的基类 ...

  8. mysql 配置数据库主从同步

    参考:https://www.cnblogs.com/kevingrace/p/6256603.html http://www.51testing.com/html/00/130600-243651. ...

  9. 对cgic的理解——name选项

    #include <stdio.h>#include <stdlib.h>#include <string.h>#include "cgic.h" ...

  10. Delphi根据字符串实例化对象

    我们可以通过ClassRegistry单元的TClassRegistry类很轻松的根据字符串创建出对象. 下面是该类几个主要函数的说明: // 获取TClassRegistry自身的单例引用class ...