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 ...
随机推荐
- bzoj 4823 & 洛谷 P3756 老C的方块 —— 最小割
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4823 https://www.luogu.org/problemnew/show/P3756 ...
- for循环中的条件执行循序
问题: public class Main { public static void main(String[] args) { int i,n,length = 0; for(i=1;length& ...
- JVM体系结构之六:堆Heap之1
一.简介 对于大多数应用来说,Java 堆(Java Heap)是Java 虚拟机所管理的内存中最大的一块.Java 堆是被所有线程共享的一块内存区域,在虚拟机启动时创建.此内存区域的唯一目的就是存放 ...
- zk 02之 Windows安装和使用zookeeper
本文介绍的 Zookeeper 是以 3.4.5 这个稳定版本为基础,最新的版本可以通过官网 http://hadoop.apache.org/zookeeper/来获取,Zookeeper 的安装非 ...
- Velocity的layout功能
一.从VelocityViewServlet到VelocityLayoutServlet 使用Velocity开发web应用时,需要在web.xml中配置一个Velocity提供的VelocityVi ...
- javascript的DI
学习AngularJS的原因之一就是觉得他的DI很牛叉,为了更好的学习,在研究源码之前,想自己按照自己的思路先实现个DI,希望这个思路能够对学习源码有帮助. (function(){ var conf ...
- 聪明的kk (南洋理工—171)
#include<iostream> using namespace std; ][]; ][]; int N, M; int dp(int i, int j) { ) return d[ ...
- UT源码116
2)NextDate函数问题 NextDate函数说明一种复杂的关系,即输入变量之间逻辑关系的复杂性 NextDate函数包含三个变量month.day和year,函数的输出为输入日期后一天的日期. ...
- Android性能优化系列---管理你的app内存
文章出处:http://developer.android.com/training/articles/memory.html#YourApp Random-access memory(RAM)在任 ...
- c# 窗体启动后自动执行 Form_Load事件注册及调用
很多时候我们需要在程序一开始后立即触发执行一些程序.这时候需要调用Form_Load. 首先编写事件程序块,编写完后即可再里面添加需要执行的代码. 在结构体之后写就行.添加之前的代码如下: using ...