题目链接: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. 引用类型 ( 对象定义 )——Array 类型

    本文地址:http://www.cnblogs.com/veinyin/p/7607293.html  一个数组中可以存储不同类型的值,可以混合存储数字.字符串.对象等 1 创建数组 1.1 构造函数 ...

  2. 手机网页的头部meta的相关配置~~

    今天使用sublime写手机端网页的时候,发现木有meta的自动生成手机网页的快捷键·~ 然后就去网上巴拉,准备存储一份~~哈哈 一.天猫 <title>天猫触屏版</title&g ...

  3. CentOS7防火墙fiewall用法

    CentOS7与以前常用的CentOS6还是有一些不同之处的,比如在设置开放端口的时候稍许有些不同,常用的iptables命令已经被 firewalld代替.这几天正好有在CentOS7系统中玩Sea ...

  4. stegsolve使用探究

    应该也不是工具的问题吧,更多的是图片.但是不知道咋取就写工具了. 比如:http://ctf5.shiyanbar.com/stega/chromatophoria/steg.png 我在想为毛要选择 ...

  5. flask基础之jijia2模板使用基础(二)

    前言 在以前前后端不分离的时代,后台程序员往往又当爹又当妈,需要将前端程序员写的h5页面填充模板语言.而jijia2是一门十分强大的python的模板语言,是flask框架的核心模块之一.先简单介绍一 ...

  6. 实现UE添加自定义按钮之添加菜单

    1.ueditor.config.js配置文件中配置 2.在ueditor.all.js配置文件中配置点开的的弹框位置 3.在ueditor1_4_3-utf8-jsp\themes\default\ ...

  7. Python中如何Debug

    debug是编码是非常重要的调试技巧,通过在运行过程中设置断点,帮助开发人员更好的理解运行过程. Python中debug不像JAVA或者C++那样在IDE中设置断点那么直观. Python的debu ...

  8. NLP基础 成分句法分析和依存句法分析

    正则匹配: .除换行符所有的 ?表示0次或者1次 *表示0次或者n次 a(bc)+表示bc至少出现1次 ^x.*g$表示字符串以x开头,g结束 |或者 http://regexr.com/ 依存句法分 ...

  9. C# 下载文件的四种方法

    C# 文件下载四方法 - CSDN论坛 - CSDN.NET using System; using System.Data; using System.Configuration; using Sy ...

  10. C# Winform频繁刷新导致界面闪烁解决方法

    C#Winform频繁刷新导致界面闪烁解决方法 一.通过对窗体和控件使用双缓冲来减少图形闪烁(当绘制图片时出现闪烁时,使用双缓冲) 对于大多数应用程序,.NET Framework 提供的默认双缓冲将 ...