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> ...
随机推荐
- WPF 4 日期选择器(DatePicker)
原文:WPF 4 日期选择器(DatePicker) 前一篇<WPF 4 日历控件(Calendar)> 中我们对日历控件的使用方式有了基本了解,本篇将继续介绍WPF 4 中另一 ...
- jquery属性过滤器
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- C#原子性运算 interlocked.compareExchanged
缘起: 假设有一个类myClass, myclass里有一个count属性. 在多线程的环境下 每个线程中 直接使用count++, 如果两个线程并行执行时, 两个线程中的一个的结果会被覆掉, 非线 ...
- Cordova页面加载外网图片失败,Refused to load the image
原文:Cordova页面加载外网图片失败,Refused to load the image 1.使用Cordova页面加载外网图片失败,抛出异常 Refused to load the image ...
- iOS判断当前时间是否处于某个时间段内
/** * 判断当前时间是否处于某个时间段内 * * @param startTime 开始时间 * @param expireTime 结束时间 */ - (BOOL)validateWithSta ...
- QString转换为LPTSTR(使用了reinterpret_cast,真是叹为观止,但是也开阔了思路),三篇文章合起来的各种转换方法
醉了,windows下宏定义了很多char类型 LPTSTR .今天,直接使用,qt报错,真TM费事. 将“CPU”转化为wcha_t * QString str = "CPU"; ...
- Windows 10预装应用太多?一个命令删除!
Windows 10预装了很多应用软件,虽然有些其实也不难用,但是使用率可能比较低,很多人也不喜欢预装的东西,而且还占空间,那么这些预装的如何彻底清除呢? 其实微软是自带了命令可以执行这样的操作,用它 ...
- How to setup Assigned Access in Windows 10 (Kiosk Mode) 设置分配的访问权限(Kiosk模式)
Let’s say you’re building some sort of ingenious mechanical contraption to be displayed in public th ...
- C++ 标准库概览(一分钟就看完了)
C++ 标准库以若干头文件的方式提供. 下面简单介绍一个各头文件的内容. 第一部分 容器 Containers <array> C++11 新增.提供了容器类模板 std::array,固 ...
- C函数实现返回多个值的方法
C语言中,一个函数最多只能实现一个返回值. int func (int b) { int a=5; if (a>b) return a; else return b; return 0; } ...