Codeforces 617E:XOR and Favorite Number(莫队算法)
http://codeforces.com/problemset/problem/617/E
题意:给出n个数,q个询问区间,问这个区间里面有多少个区间[i,j]可以使得ai^ai+1^...^aj = k。
思路:根据xor的性质,可以处理出前缀异或和sum[],那么要使结果等于k,就是sum[i-1]^sum[j] == k,即sum[j]^k == sum[i-1],那么用一个数组cnt[]存储对于每一个sum[]的数量。然后用莫队算法做。莫队算法是一种离线处理区间问题的算法,在我看来是将询问通过分块排序成一个可以暴力处理的序列,让暴力的复杂度尽量的低。只有O(n*sqrt(n))的复杂度。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <iostream>
#include <stack>
#include <map>
#include <queue>
#include <set>
using namespace std;
typedef long long LL;
#define N 100010
#define INF 0x3f3f3f3f
struct node {
int l, r, id;
LL ans;
} q[N*];
int sum[N], kuai, k;
LL cnt[N*]; bool cmp1(const node &a, const node &b) {
if(a.l / kuai != b.l / kuai) return a.l / kuai < b.l / kuai; // 根据块的编号排序
return a.r < b.r; // 块内按照r值排序
} bool cmp2(const node &a, const node &b) {
return a.id < b.id;
} int main()
{
int n, m, num, L, R;
LL s = ;
scanf("%d%d%d", &n, &m, &k);
for(int i = ; i <= n; i++) {
scanf("%d", &num); sum[i] = sum[i-] ^ num;
}
for(int i = ; i <= m; i++) {
scanf("%d%d", &q[i].l, &q[i].r);
q[i].id = i; q[i].l--;
}
kuai = sqrt(n);
sort(q + , q + + m, cmp1);
L = ; R = ;
for(int i = ; i <= m; i++) {
int l = q[i].l, r = q[i].r;
while(L < l) {
cnt[sum[L]]--;
s -= cnt[sum[L]^k];
L++;
}
while(L > l) {
L--;
s += cnt[sum[L]^k];
cnt[sum[L]]++;
}
while(R < r) {
R++;
s += cnt[sum[R]^k];
cnt[sum[R]]++;
}
while(R > r) {
cnt[sum[R]]--;
s -= cnt[sum[R]^k];
R--;
}
q[i].ans = s;
}
sort(q + , q + + m, cmp2);
for(int i = ; i <= m; i++) printf("%lld\n", q[i].ans);
return ;
}
Codeforces 617E:XOR and Favorite Number(莫队算法)的更多相关文章
- CodeForces - 617E XOR and Favorite Number 莫队算法
https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry, 问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...
- Codeforces 617E XOR and Favorite Number莫队
http://codeforces.com/contest/617/problem/E 题意:给出q个查询,每次询问区间内连续异或值为k的有几种情况. 思路:没有区间修改,而且扩展端点,减小端点在前缀 ...
- codeforces 617E. XOR and Favorite Number 莫队
题目链接 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k. 维护一个前缀异或值就可以了. 要注意的是 区间[l ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces617 E . XOR and Favorite Number(莫队算法)
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...
- 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 ...
- Codeforces 617E XOR and Favorite Number(莫队算法)
题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减 ...
- CodeForces 617E XOR and Favorite Number
莫队算法. #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> ...
随机推荐
- Entity States
Added. The entity does not yet exist in the database. The SaveChanges method must issue an INSERT st ...
- WPF MVVM+EF增删改查 简单示例(二) 1对1 映射
WPF MVVM+EF增删改查 简单示例(一)实现了对学生信息的管理. 现在需求发生变更,在录入学生资料的时候同时需要录入学生的图片信息,并且一名学生只能有一张图片资料.并可对学生的图片资料进行更新. ...
- 高手问答精选:Go 语言 —— 云计算时代的 C 语言(类似于一个FAQ)
Go 语言被称为云计算时代的 C 语言,它在软件开发效率和运行效率之间做出了绝佳的权衡.这使得它既适应于互联网应用的极速开发,又能在高并发.高性能的开发场景中如鱼得水.正因如此,许多互联网公司,尤其是 ...
- WPF之MahApps.Metro下载和WPF学习经验
这几天一直在学习WPF的东西.刚开始以为和Winform一样.拖拽控件来进行布局.结果远远没有那么简单.很多东西都需要自己写.包括样式.今天给大家分享一个 MahApps.Metro. 首先在NuGe ...
- Socket进阶篇
Socket简介 1,socket是什么? 2,socket的作用 3,socket怎么用 4,socket的扩展 ——————————————————- socket是什么? Socket这个名词现 ...
- SQL Server 可更新订阅中有行筛选的同步复制移除项目而不重新初始化所有订阅!
原文:SQL Server 可更新订阅中有行筛选的同步复制移除项目而不重新初始化所有订阅! 在可更新订阅的同步复制中,有行筛选的项目表,移除的时候会提示重新初始化所有的快照并且应用此快照,这将导致所有 ...
- 中国自主X86处理器工艺跃进:国产28nm升级16nm(上海兆芯)
提到X86处理器,世人皆知Intel.AMD,殊不知还有个VIA(威盛),在Intel反垄断世纪大战中VIA公司作为Intel霸权的受害者也最终确认了X86授权,不过VIA与前面两家的实力相差太远,X ...
- ASP.NET MVC视图
前言 视图即是用户与Web应用程序的接口,用户通常会看到视图,然后在视图上进行交互,Web应用程序的视图通常是HTML格式. 首先了解控制器选择返回哪个视图的问题.新建一个项目,浏览到/Home/Ab ...
- “流”的5个例子(TStream是抽象类,写到文件里和内存里,都是一样的)
http://www.cnblogs.com/keyvip/category/270215.html
- Cookieless.js —— 无需 Cookie 实现访客跟踪
直击现场 https://github.com/Colex/node-cookieless Cookieless.js 是一个轻量级的使用 Etag 进行访客跟踪的 Node.js 扩展库.使用该库无 ...