题目链接: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. Maven项目Eclipse启动时报错: java.lang.ClassNotFoundException: org.springframework.web.util.IntrospectorCleanupListener

    Eclipse中启动Maven项目时报如下错误: 严重: Error configuring application listener of class org.springframework.web ...

  2. ASP.NET网站伪静态下使用中文URL

    首先解释一下,什么是中文URL呢?它并不是我们常见的把汉字编码为 %CF%EC 这种形式,而是在URL中直接使用汉字 这种形式目前还不是很多见.因为不同的浏览器处理起来可能会有所不同,不过据我测试,I ...

  3. Global.asax文件—ASP.NET细枝末节(1)

    说明 Global的解释是全局的.全球的. Global.asax 文件,有时候叫做 ASP.NET 应用程序文件,提供了一种在一个中心位置响应应用程序级或模块级事件的方法.你可以使用这个文件实现应用 ...

  4. Linux Deploy 使用 Repository部署Linux系统

    Linux Deploy 使用 Repository部署Linux系统 为了解决镜像不稳定导致包下载错误,能得到一个稳定环境,可以使用linux deploy导出功能. 这里提供两个制作好的包 用户名 ...

  5. CTSC 2017 游记

    惨啊,弱菜选手只报上了CTSC,去不了APIO. day -1 晚上的时候,坐上了去帝都的卧铺. 由于第二天就是luogu月赛round1,还得在火车上赶工出题... 颓了好长时间,把题面写出来了,用 ...

  6. linux nginx php-fpm被攻击

    1.nginx错误日志:报错 2018/05/30 16:30:55 [error] 8765#0: *1485 connect() to unix:/tmp/php-70-cgi.sock fail ...

  7. 端口扫描———nmap

    nmap教程之nmap命令使用示例(nmap使用方法) 浏览:8268 | 更新:2014-03-29 17:23 Nmap是一款网络扫描和主机检测的非常有用的工具.Nmap是不局限于仅仅收集信息和枚 ...

  8. LightOJ 1074 Extended Traffic(spfa+dfs标记负环上的点)

    题目链接:https://cn.vjudge.net/contest/189021#problem/O 题目大意:有n个站点,每个站点都有一个busyness,从站点A到站点B的花费为(busynes ...

  9. HDU 4614 Vases and Flowers(线段树+记录区间始末点或乱搞)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 题目大意:有n个空花瓶,有两种操作: 操作①:给出两个数字A,B,表示从第A个花瓶开始插花,插B ...

  10. PHP–图像XX因其本身有错无法显示

    1.你输出的图像格式 PHP不支持,检查GD库有没有开启 对应的图像格式是不是支持2.文件里面的格式是否一致 如果是UTF-8的检查文件格式是不是UTF-8 [UTF-8+BOM很多IDE默认是这个格 ...