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> ...
随机推荐
- Vhost Architecture
在前面的文章中在介绍virtio机制中,能够看到在通常的应用中一般使用QEMU用户态程序来模拟I/O訪问,而Guest中的数据要通过Guest到Host Userspace的第一次拷贝,再经过Host ...
- Goutte 获取http response
$client = new Goutte\Client(); $crawler = $client->request('GET', 'http://symfony.com'); 获取http 响 ...
- Golang的演化历程
本文来自Google的Golang语言设计者之一Rob Pike大神在GopherCon2014大会上的开幕主题演讲资料“Hello, Gophers!”.Rob大神在这次分 享中用了两个生动的例子讲 ...
- VisualStateManager
管理控件状态和管理控件状态的转换逻辑 <Window.Resources> <Style TargetType="Button" x:Key="Anim ...
- windows IIS发布.net core网站的环境配置
1.安装对应的.net core的runtime2.安装Windows Server Hosting下载地址:https://www.microsoft.com/net/download/core#/ ...
- .net core使用redis
本地启动redis控制台 && 安装redis服务(用于调试) 1.下载最新版redis,选择.zip则是免安装的版本下载地址:https://github.com/Microsoft ...
- JS 输入框为空的使用
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- 通通玩blend美工(6)上——仿iPhone滚动选择器的ListBox(UI设计)
原文:通通玩blend美工(6)上--仿iPhone滚动选择器的ListBox(UI设计) 好久没更新博客了,由于项目比较紧,期间收到不少园友的短消息,感谢大家对我的支持~~. 相信各位都在自己的神机 ...
- Delphi 7下最小化到系统托盘(主要是WM_TRAYMSG和WM_SYSCOMMAND消息)
在Delphi 7下要制作系统托盘,只能制作一个比较简单的系统托盘,因为ShellAPI文件定义的TNotifyIconData结构体是比较早的版本.定义如下: 123456789 _NOTIFY ...
- apache本地服务器的配置流程
安装Apache 一.目的: 1. 能够有一个测试的服务器,不是所有的特殊网络服务都能找到免费的! 二.为什么是 "Apache" 1. 使用最广的 Web 服务器 2. Mac自 ...