bzoj 2308 小Z的袜子(莫队算法)
小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的袜子(莫队算法)的更多相关文章
- bzoj 2038 小Z的袜子 莫队算法
题意 给你一个长度序列,有多组询问,每次询问(l,r)任选两个数相同的概率.n <= 50000,数小于等于n. 莫队算法裸题. 莫队算法:将序列分为根号n段,将询问排序,以L所在的块为第一关键 ...
- BZOJ 2038 小z的袜子 & 莫队算法(不就是个暴力么..)
题意: 给一段序列,询问一个区间,求出区间中.....woc! 贴原题! 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过 ...
- bzoj 2038 小z的袜子 莫队例题
莫队,利用可以快速地通过一个问题的答案得到另一问题的答案这一特性,合理地组织问题的求解顺序,将已解决的问题帮助解决当前问题,来优化时间复杂度. 典型用法:处理静态(无修改)离线区间查询问题. 线段树也 ...
- 【国家集训队2010】小Z的袜子[莫队算法]
[莫队算法][国家集训队2010]小Z的袜子 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程, ...
- [日常摸鱼]bzoj2038[2009国家集训队]小Z的袜子-莫队算法
今天来学了下莫队-这题应该就是这个算法的出处了 一篇别人的blog:https://www.cnblogs.com/Paul-Guderian/p/6933799.html 题意:一个序列,$m$次询 ...
- bzoj 2038 小z的袜子 莫队
莫队大法好,入坑保平安 只要能O(1)或O(log)转移,离线莫队貌似真的无敌. #include<cstdio> #include<iostream> #include< ...
- Luogu 1494 - 小Z的袜子 - [莫队算法模板题][分块]
题目链接:https://www.luogu.org/problemnew/show/P1494 题目描述 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天 ...
- 小Z的袜子 & 莫队
莫队学习 & 小Z的袜子 引入 莫队 由莫涛巨佬提出,是一种离线算法 运用广泛 可以解决广大的离线区间询问题 莫队的历史 早在mt巨佬提出莫队之前 类似莫队的算法和莫队的思想已在Codefor ...
- BZOJ 2038 [2009国家集训队]小Z的袜子 莫队
2038: [2009国家集训队]小Z的袜子(hose) 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Descriptionw ...
随机推荐
- shell监控之列出1小时内cpu占用最多的10个进程
脚本内容如下: -------------------------------------------------------------------------------------------- ...
- [DPI][suricata] suricata 配置使用
前文: [DPI] suricata-4.0.3 安装部署 至此, 我们已经拥有了suricata可以运行的环境了. 接下来,我们来研究一下它的功能, 首先,分析一下配置文件: /suricata/e ...
- 转:环绕通知返回值 object 类型
遇到 AOP 环绕通知报错 “return value from advice does not match primitive return type for: public boolean” 百 ...
- LeetCode 104 Maximum Depth of Binary Tree 解题报告
题目要求 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- 第三方python 加密库 --- cryptography
1,安装依赖 pip install cryptography 2,生成秘钥 from cryptography.fernet import Fernet #秘钥#随机生成秘钥 cipher_key ...
- TensorFlow环境
vps cenots7自带的python2.7各种毛病,浪费了不少时间,装了pyhton3一下就搞定了 mac上有些依赖库需要sudo安装 vps上是基于Anaconda搭建的,感谢极客学院的教程ht ...
- CentOS 7.2编译安装nginx1.10.3+MySQL5.5.38+PHP5.5.38
1.关闭firewallad 关闭防火墙 systemctl stop firewalld.service 禁止firewall开机启动 systemctl disable firewalld.ser ...
- MySQL之——崩溃-修复损坏的innodb:innodb_force_recovery
转: https://blog.csdn.net/l1028386804/article/details/77199194 一.问题描述 今天在线运行的一个mysql崩溃了. 查看错误日志,如下: - ...
- 下载pywinauto
1.下载pywinauto 链接:http://pywinauto.github.io/ 2.将安装包放到D:python36\script,进入pywinauto目录,按shift+右键,进入命令窗 ...
- Windows 10正式版的历史版本
1.Windows 10 1507 初版Windows 10,代号TH1,版本号10240,发布于2015年7月. 2015年7月29日,微软正式发布了Windows 10操作系统.Windows 1 ...