小Z的袜子

【题目链接】小Z的袜子

【题目类型】莫队算法

&题解:

莫队算法第一题吧,建议先看这个理解算法,之后在参考这个就可以写出简洁的代码

我的比第2个少了一次sort,他的跑了1600ms,我的跑了800ms.

【时间复杂度】\(O(n^{1.5})\)

&代码:

#include <cstdio>
#include <bitset>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
const int maxn = 5e4 + 7;
int n, m, col[maxn], pos[maxn], ans, num[maxn];
ll fz[maxn], fm[maxn];
struct data { int l, r, id; } a[maxn];
bool cmp(data a, data b) {
if(pos[a.l] == pos[b.l]) return a.r < b.r;
return a.l < b.l;
}
void update(int x, int d) {
int c = col[x];
ans -= num[c] * num[c];
num[c] += d;
ans += num[c] * num[c];
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int main() {
//("E:1.in", "r", stdin);
while(~scanf("%d%d", &n, &m)) {
int bk = sqrt(n);
for(int i = 1; i <= n; i++) {
scanf("%d", col + i);
pos[i] = (i - 1) / bk;
}
for(int i = 1; i <= m; i++) {
scanf("%d%d", &a[i].l, &a[i].r);
a[i].id = i;
}
sort(a + 1, a + 1 + m, cmp);
memset(num, 0, sizeof(num));
ans = 0;
for(int i = 1, l = 1, r = 0; i <= m; i++) {
for(; r < a[i].r; r++)
update(r + 1, 1);
for(; r > a[i].r; r--)
update(r, -1);
for(; l < a[i].l; l++)
update(l, -1);
for(; l > a[i].l; l--)
update(l - 1, 1);
if(a[i].l == a[i].r) {
fz[a[i].id] = 0, fm[a[i].id] = 1;
continue;
}
//引用& 是跟着变量的,不是跟着类型的
ll &tz = fz[a[i].id], &tm = fm[a[i].id];
tz = ans - (a[i].r - a[i].l + 1);
tm = (ll)(a[i].r - a[i].l + 1) * (a[i].r - a[i].l);
ll k = gcd(tz, tm);
tz /= k, tm /= k;
}
for(int i = 1; i <= m; i++) {
printf("%lld/%lld\n", fz[i], fm[i]);
}
}
return 0;
}

bzoj 2308 小Z的袜子(莫队算法)的更多相关文章

  1. bzoj 2038 小Z的袜子 莫队算法

    题意 给你一个长度序列,有多组询问,每次询问(l,r)任选两个数相同的概率.n <= 50000,数小于等于n. 莫队算法裸题. 莫队算法:将序列分为根号n段,将询问排序,以L所在的块为第一关键 ...

  2. BZOJ 2038 小z的袜子 & 莫队算法(不就是个暴力么..)

    题意: 给一段序列,询问一个区间,求出区间中.....woc! 贴原题! 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过 ...

  3. bzoj 2038 小z的袜子 莫队例题

    莫队,利用可以快速地通过一个问题的答案得到另一问题的答案这一特性,合理地组织问题的求解顺序,将已解决的问题帮助解决当前问题,来优化时间复杂度. 典型用法:处理静态(无修改)离线区间查询问题. 线段树也 ...

  4. 【国家集训队2010】小Z的袜子[莫队算法]

    [莫队算法][国家集训队2010]小Z的袜子 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程, ...

  5. [日常摸鱼]bzoj2038[2009国家集训队]小Z的袜子-莫队算法

    今天来学了下莫队-这题应该就是这个算法的出处了 一篇别人的blog:https://www.cnblogs.com/Paul-Guderian/p/6933799.html 题意:一个序列,$m$次询 ...

  6. bzoj 2038 小z的袜子 莫队

    莫队大法好,入坑保平安 只要能O(1)或O(log)转移,离线莫队貌似真的无敌. #include<cstdio> #include<iostream> #include< ...

  7. Luogu 1494 - 小Z的袜子 - [莫队算法模板题][分块]

    题目链接:https://www.luogu.org/problemnew/show/P1494 题目描述 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天 ...

  8. 小Z的袜子 & 莫队

    莫队学习 & 小Z的袜子 引入 莫队 由莫涛巨佬提出,是一种离线算法 运用广泛 可以解决广大的离线区间询问题 莫队的历史 早在mt巨佬提出莫队之前 类似莫队的算法和莫队的思想已在Codefor ...

  9. BZOJ 2038 [2009国家集训队]小Z的袜子 莫队

    2038: [2009国家集训队]小Z的袜子(hose) 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Descriptionw ...

随机推荐

  1. [No000011F]Python教程2/9-安装Python 及其解释器介绍

    因为Python是跨平台的,它可以运行在Windows.Mac和各种Linux/Unix系统上.在Windows上写Python程序,放到Linux上也是能够运行的. 要开始学习Python编程,首先 ...

  2. Spring <context:annotation-config> 与<context-component-scan> 的作用

    <context:annotation-config> 与<context-component-scan> 的作用 <context:annotation-config& ...

  3. 垃圾回收基本算法 内存管理 GC大统一理论

    <垃圾收集> (豆瓣) https://book.douban.com/subject/1157908/ 第1章 简介1.1 内存分配的历史1.1.1 静态分配1.1.2 栈分配1.1.3 ...

  4. dbclient python ---influxdb -install -relay--http write--read.[create db]

    1s=1000ms 1ms=1000 microseconds 1microsecond=1000 nanoseconds+01:00 from influxdb import InfluxDBCli ...

  5. flash cs4 如何新增自定义控件

    1. 新增控件脚本* import gfx.controls.CoreList; import gfx.core.UIComponent; import gfx.controls.CheckBox; ...

  6. C语音读写文件

    1.fopen() fopen的原型是:FILE *fopen(const char *filename,const char *mode),fopen实现三个功能:为使用而打开一个流,把一个文件和此 ...

  7. 页面调用qq

    tencent://message/?uin=516999605&Site=&Menu=yes

  8. Eclipse EE下载安装与配置

    Eclipse EE下载安装与配置 一.下载 下载链接:http://www.eclipse.org/downloads/eclipse-packages/ 1.进入Eclipse官网进行下载选择Ec ...

  9. BeanFactoryPostProcessor vs BeanPostProcessor

    BeanFactoryPostProcessors affect BeanDefinition objects because they are run right after your config ...

  10. FFmpeg 裁剪——音频解码

    配置ffmpeg,只留下某些音频的配置: ./configure --enable-shared --disable-yasm --enable-memalign-hack --enable-gpl ...