E. XOR and Favorite Number 莫队 2038: [2009国家集训队]小Z的袜子(hose)
一直都说学莫队,直到现在才学,训练的时候就跪了 T_T,其实挺简单的感觉。其实训练的时候也看懂了,一知半解,就想着先敲。(其实这样是不好的,应该弄懂再敲,以后要养成这个习惯)
前缀异或也很快想出来,结果没弄好边界,也是对前缀异或和莫队的不熟练。
CF 的E题,给定区间中有多少子区间个数异或等于k
容易想到的是预处理前缀异或值,求解区间[L, R]的贡献,相当于在前缀异或值[L - 1, R]中任取两个数,异或值等于k
知道区间[L, R]的贡献,可以O(1)知道[L - 1, R]和[L, R + 1]的贡献,就可以用莫队了
把询问分块,每块大小sqrtn,然后块内按右端点排序,然后two pointer维护即可。
因为块内的大小是sqrtn,然后每次移动只会移动sqrtn的大小。复杂度是nsqrtn
两题都是莫队的一个应用,离线查询区间
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
const int maxn = + ;
struct Query {
int L, R, id;
}node[maxn];
int a[maxn];
int n, m, k, magic;
bool cmp(struct Query a, struct Query b) {
if (a.L/magic != b.L/magic) return a.L/magic < b.L/magic;
else return a.R < b.R;
}
LL ans[maxn];
LL num[maxn];
void calc() {
LL temp = ;
int L = , R = ;
num[] = ;
for (int i = ; i <= m; ++i) {
while (R < node[i].R) {
++R;
temp += num[a[R] ^ k];
num[a[R]]++;
}
while (R > node[i].R) { // differ sqrt
num[a[R]]--;
temp -= num[a[R] ^ k];
--R;
}
while (L < node[i].L) {
num[a[L - ]]--;
temp -= num[a[L - ] ^ k];
++L;
}
while (L > node[i].L) {
--L;
temp += num[a[L - ] ^ k];
num[a[L - ]]++;
}
ans[node[i].id] = temp;
}
}
void work() {
scanf("%d%d%d", &n, &m, &k);
for (int i = ; i <= n; ++i) {
scanf("%d", a + i);
a[i] ^= a[i - ];
// printf("%d ", a[i]);
}
magic = (int)sqrt(n);
for (int i = ; i <= m; ++i) {
scanf("%d%d", &node[i].L, &node[i].R);
node[i].id = i;
}
sort(node + , node + + m, cmp);
calc();
for (int i = ; i <= m; ++i) {
cout << ans[i] << endl;
}
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
const int maxn = 5e5 + ;
struct Query {
int L, R, id;
LL a, b;
void init() {
if (a != ) {
LL t = __gcd(a, b);
a /= t, b /= t;
} else b = ;
}
}node[maxn], ans[maxn];
int n, m, magic;
int a[maxn];
LL num[maxn];
bool cmp(struct Query a, struct Query b) {
if (a.L/magic != b.L/magic) return a.L/magic < b.L/magic;
else return a.R < b.R;
}
void work() {
scanf("%d%d", &n, &m);
for (int i = ; i <= n; ++i) {
scanf("%d", a + i);
}
for (int i = ; i <= m; ++i) {
scanf("%d%d", &node[i].L, &node[i].R);
node[i].id = i;
}
magic = sqrt(n);
sort(node + , node + + m, cmp);
int L = , R = ;
LL res = ;
for (int i = ; i <= m; ++i) {
while (R < node[i].R) {
++R;
res -= num[a[R]] * num[a[R]] - num[a[R]];
num[a[R]]++;
res += num[a[R]] * num[a[R]] - num[a[R]];
}
while (R > node[i].R) { //不同块之间才会出现
res -= num[a[R]] * num[a[R]] - num[a[R]];
num[a[R]]--;
res += num[a[R]] * num[a[R]] - num[a[R]];
R--;
}
while (L < node[i].L) { //每个块之间只是按照R排序的
res -= num[a[L]] * num[a[L]] - num[a[L]];
num[a[L]]--;
res += num[a[L]] * num[a[L]] - num[a[L]];
L++;
}
while (L > node[i].L) {
--L;
res -= num[a[L]] * num[a[L]] - num[a[L]];
num[a[L]]++;
res += num[a[L]] * num[a[L]] - num[a[L]];
}
ans[node[i].id].a = res, ans[node[i].id].b = 1LL * (node[i].R - node[i].L + ) * (node[i].R - node[i].L);
}
for (int i = ; i <= m; ++i) {
ans[i].init();
printf("%lld/%lld\n", ans[i].a, ans[i].b);
}
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}
E. XOR and Favorite Number 莫队 2038: [2009国家集训队]小Z的袜子(hose)的更多相关文章
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 7687 Solved: 3516[Subm ...
- 莫队算法 2038: [2009国家集训队]小Z的袜子(hose)
链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 ...
- Bzoj 2038: [2009国家集训队]小Z的袜子(hose) 莫队,分块,暴力
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 5763 Solved: 2660[Subm ...
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose) ( 莫队 )
莫队..先按sqrt(n)分块, 然后按块的顺序对询问排序, 同块就按右端点排序. 然后就按排序后的顺序暴力求解即可. 时间复杂度O(n1.5) --------------------------- ...
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose)【莫队算法裸题&&学习笔记】
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 9894 Solved: 4561[Subm ...
- 2038: [2009国家集训队]小Z的袜子(hose) (莫队算法)
题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=2038 专题练习: http://acm.hust.edu.cn/vjudge/conte ...
- 莫队算法 BOJ 2038 [2009国家集训队]小Z的袜子(hose)
题目传送门 /* 莫队算法:求出[l, r]上取出两只相同袜子的个数. 莫队算法是离线处理一类区间不修改查询类问题的算法.如果你知道了[L,R]的答案,可以在O(1)的时间下得到 [L,R-1]和[L ...
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose)
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 7676 Solved: 3509[Subm ...
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose) 分块
分块大法好 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MB Submit: 2938 Solved: 13 ...
随机推荐
- Centos下Yum安装PHP5.5
默认的版本太低了,手动安装有一些麻烦,想采用Yum安装的可以使用下面的方案: 1.检查当前安装的PHP包 yum list installed | grep php 如果有安装的PHP包,先删除他们 ...
- error: converting to execution character set: Invalid or incomplete multibyte or wide character
交叉编译.c文件,遇到如下问题 arm-linux-gcc -o show_lines show_lines.c -lfreetype -lm show_lines.c:199:19: error: ...
- tomcat安装与运行
实验环境:CentOS7 使用系统yum仓库安装: #安装基本包和开发工具包 [root@~ localhost]#yum install -y java-1.8.0-openjdk java-1.8 ...
- 大数据处理之道(十分钟学会Python)
一:python 简介 (1)Python的由来 Python(英语发音:/ˈpaɪθən/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个 ...
- 进程中t.start(), t.daemon() t.jion()的使用
#!/usr/bin/env python import multiprocessing import time def f1(a1): time.sleep(2) print(a1) if __na ...
- 【总结整理】overflow: auto/hidden;清除自己
.top-nav{ font-size: 12px; font-weight: bold; list-style-type: none; border-bottom: 8px solid #DC4E1 ...
- MQTT,XMPP,STOMP,AMQP,WAMP适用范围优缺点比较
想要向服务器发送请求并获得响应?直接使用 HTTP 吧!非常简单.但是当需要通过持久的双向连接来通信时,如 WebSockets,当然你也有其它的选择. 这篇文章会简单扼要的解释 MQTT,XMPP, ...
- Mahout0.9 – Clustering (聚类篇)
Mahout – Clustering (聚类篇) Leave a reply 什么是Mahout? " Apache Mahout™ project's goal is to build ...
- 2、misa统计SRR结果
参考: https://www.sogou.com/link?url=hedJjaC291NYNxVe4xgB4c3bUxXRMqZrT93cntTAgYfyBbRAdP9kIA.. https:// ...
- hdu1076
#include<iostream> using namespace std; int main() { int cases; int k; cin>>cases; while ...