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 ...
随机推荐
- Poj 1504 Adding Reversed Numbers(用字符串反转数字)
一.题目大意 反转两个数字并相加,所得结果崽反转.反转规则:如果数字后面有0则反转后前面不留0. 二.题解 反转操作利用new StringBuffer(s).reverse().toString() ...
- HDOJ2553(2N皇后问题)
N皇后问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 【转】 Pro Android学习笔记(五二):ActionBar(5):list模式
可以在action bar中加入spinner的下来菜单,有关spinner,可以参考Pro Android学习笔记(二十):用户界面和控制(8):GridView和Spinner. list的样式和 ...
- u-boot向linux内核传递启动参数
U-BOOT 在启动内核时,会向内核传递一些参数.BootLoader 可以通过两种方法传递参数给内核,一种是旧的参数结构方式(parameter_struct),主要是 2.6 之前的内核使用的方式 ...
- OpenCV创建轨迹条,图片像素的访问
.OpenCV创建进度条以及图像对比度,亮度调整 1.创建轨迹条createTrackbar() 函数原型C++: intcreateTrackbar(conststring& trackba ...
- React组件详细介绍及其生命周期函数
组件的详细说明 通过Reac.createClass({...})创建组件的时候,应该有一个render()方法,也可以在其中添加生命周期函数. render方法 当调用该方法的时候,会检测this. ...
- 【总结整理】webstorm插件使用
<ul> <li><a href="#">1F 男装</a></li> <li><a href=&qu ...
- 【总结整理】overflow: auto/hidden;清除自己
.top-nav{ font-size: 12px; font-weight: bold; list-style-type: none; border-bottom: 8px solid #DC4E1 ...
- Learning Python 008 正则表达式-003 sub()方法
Python 正则表达式 - sub()方法 sub()方法 sub()方法:替换符合规律的内容,返回替换的值 # -?- coding: utf-8 -?- import re secret_cod ...
- 6.【应急响应】Linux入侵排查思路
0x01 入侵排查思路 一.账号安全 基本使用: 1.用户信息文件/etc/passwd root:x:0:0:root:/root:/bin/bash account:password:UID:GI ...