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 ...
随机推荐
- Python-连接Redis并操作
首先开启redis的外连 sch01ar@ubuntu:~$ sudo vim /etc/redis/redis.conf 把bind 127.0.0.1这行注释掉 然后重启redis sudo /e ...
- linux日常管理-sar工具
查看网卡瓶颈 查看网卡流量 默认10分钟一次 查看实时流量 每秒钟显示一次 显示5次 网卡有 lo eth0 主要看eth0外网 rxbyt/s 进网口和 txbyt/s出网口 带宽看txby ...
- 阿里巴巴Druid数据库连接池的使用
准备: 创建一个基于SpringBoot的web项目 1 引入相关依赖 jpa.mysql.druid <?xml version="1.0" encoding=" ...
- 杭电acm 1022题
Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...
- web特点
1.图形化和易于导航的 Web是非常易于导航的,只需要从一个连接跳到另一个连接,就可以在各页各站点之间进行浏览了. 2.与平台无关 这里所说的平台是指软件的运行环境,可以是Windows.Linux等 ...
- Win7环境下Sublime Text 3下安装NodeJS插件
1.首先下载安装Node.JS,配置好环境变量(安装好Node.JS默认是配置好了环境变量的). 2.Sublime Text 3下安装NodeJS插件. 参考的两篇文章:http://www.cnb ...
- java File基本操作,以及递归遍历文件夹
java 的文件操作,相对来说是比较重要的,无论是编写CS还是BS程序,都避免不了要与文件打交道,例如读写配置文件等.虽然现在很多框架都直接帮你做好了这一步! java.io.File 底层是调用与c ...
- Codeforces Round #279 (Div. 2) C. Hacking Cypher (大数取余)
题目链接 C. Hacking Cyphertime limit per test1 secondmemory limit per test256 megabytesinputstandard inp ...
- [CentOS7] ssh免密登录 scp免密传输
我们采用RSA非对称加密算法,原理: 如果,A要和B通讯,则: (1). A通过RSA算法生成公钥(.pub)和私钥(公钥用于加密,私钥用于解密) (2). B将A的公钥文件(.pub)内容加入到au ...
- java模拟简易按键精灵
很多小伙伴们都有过抢课的经历,有时候抢不到自己想上的课,只能盼望有人退选,可是很多时候别人退选了,但是很快又被别人抢走了,我们不可能时刻盯着电脑, 这时候如果有一个抢课的程序岂不是很棒.. 出于这个目 ...