题目链接:小Z的袜子

题意:$n$只袜子,$m$个询问,每次回答有多大概率在$[L,R]$区间内抽到两只颜色相同的袜子

思路:普通莫队,如果两个询问左端点在一个块内,则按询问右端点排序,否则按照所在块的序号排序,维护一个数组$num$,表示在区间$[L,R]$中,颜色为$c$的袜子有$num[c]$只,令变量$res=\sum_{c}num[c]*(num[c]-1)/2$,显然对于每个区间$[L,R]$抽到同色袜子的概率就是$\frac{res}{C_{R-L+1}^{2}}$,每次移动区间时修改$num[c]$,同时更新$res$即可,时间复杂度$O(n^{\frac{3}{2}})$

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath> using namespace std; typedef long long ll; const int N = ; struct node {
int l, r, id;
}; int n, m, a[N];
int block, belong[N], tot, l[N], r[N];
ll num[N], res, mol[N], den[N];
node q[N]; void build()
{
block = sqrt(n);
tot = n / block;
if (n % block) tot++;
for (int i = ; i <= tot; i++) {
l[i] = (i - ) * block + ;
r[i] = i * block;
}
r[tot] = n;
for (int i = ; i <= n; i++)
belong[i] = (i - ) / block + ;
} bool cmp(node a, node b)
{
if (belong[a.l] != belong[b.l])
return belong[a.l] < belong[b.l];
return a.r < b.r;
} void add(int x)
{
res = res - num[a[x]] * (num[a[x]] - ) / ;
num[a[x]]++;
res = res + num[a[x]] * (num[a[x]] - ) / ;
} void sub(int x)
{
res = res - num[a[x]] * (num[a[x]] - ) / ;
num[a[x]]--;
res = res + num[a[x]] * (num[a[x]] - ) / ;
} ll c(int n)
{
return (ll)n * (n - ) / ;
} ll gcd(ll a, ll b)
{
return == b ? a : gcd(b, a % b);
} int main()
{
scanf("%d%d", &n, &m);
build();
for (int i = ; i <= n; i++) scanf("%d", &a[i]);
for (int i = ; i <= m; i++) {
scanf("%d%d", &q[i].l, &q[i].r);
den[i] = c(q[i].r - q[i].l + );
q[i].id = i;
}
sort(q + , q + m + , cmp);
int L = , R = ;
for (int i = ; i <= m; i++) {
while (q[i].l < L) add(--L);
while (q[i].r > R) add(++R);
while (q[i].l > L) sub(L++);
while (q[i].r < R) sub(R--);
mol[q[i].id] = res;
}
for (int i = ; i <= m; i++) {
ll d = gcd(mol[i], den[i]);
if ( == mol[i]) printf("0/1\n");
else printf("%lld/%lld\n", mol[i] / d, den[i] / d);
}
return ;
}

BZOJ - 2038 小Z的袜子(普通莫队)的更多相关文章

  1. (原创)BZOJ 2038 小Z的袜子(hose) 莫队入门题+分块

    I - 小Z的袜子(hose) 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命…… 具体来说,小Z ...

  2. BZOJ 2038 小Z的袜子(hose) 莫队算法模板题

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2038 题目大意: 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中 ...

  3. BZOJ 2038 小z的袜子(莫队)

    Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜 ...

  4. [bzoj] 2038 小Z的袜子(hose) || 莫队

    原题 给出一个序列,求给定[l,r]内有任意取两个数,有多大概率是一样的 简单的莫队,每次+-当前区间里有的这个颜色的袜子的个数,最后除以(r-l+1)*(r-l)/2即可. 记得约分. #inclu ...

  5. BZOJ 2038 小Z的袜子(hose) (莫队算法)

    题意:中文题. 析:很著名的莫队算法,先把这个求概率的式子表达出来,应该是分子:C(x1, 2) + C(x2, 2) + C(x3, 2) + ... + C(xn, 2)  分母:C(n, 2), ...

  6. HYSBZ - 2038 小Z的袜子 (莫队算法)

    A1206. 小Z的袜子 时间限制:1.0s   内存限制:512.0MB   总提交次数:744   AC次数:210   平均分:44.44 将本题分享到:        查看未格式化的试题    ...

  7. 小Z的袜子(题解)(莫队)

    小Z的袜子(题解)(莫队) Junlier良心莫队 题目 luoguP1494 [国家集训队]小Z的袜子 code #include<bits/stdc++.h> #define lst ...

  8. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 7687  Solved: 3516[Subm ...

  9. Bzoj 2038: [2009国家集训队]小Z的袜子(hose) 莫队,分块,暴力

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 5763  Solved: 2660[Subm ...

随机推荐

  1. ORA-01830

    问题:varchar2类型转换成date类型 select to_date(INVOICE_DATE,'yyyy-mm-dd') from tab; 提示 ORA-01830: 日期格式图片在转换整个 ...

  2. Java中的IO、NIO、File、BIO、AIO详解

    java中有几种类型的流?JDK为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类?         Java中的流分为两种,一种是字节流,另一种是字符流,分别由四个抽象类来表示(每种流包 ...

  3. python 自动化实现定时发送html报告到邮箱

    # coding =utf-8 import os import unittest import time import datetime import smtplib from email.mime ...

  4. Linux - Deepin Linux,intel无线网卡下载慢、不能跑满宽带的解决方案

    解决方案 将 /etc/modprobe.d/iwlwifi.conf中的11n_disable=1删掉,重启. 参考 https://bbs.deepin.org/forum.php?mod=vie ...

  5. 使用fetch进行数据请求时报json错误

    使用fetch进行数据请求返回response对象,response.json报错.原因是response中含有特殊字符. fetch(url).then(response => respons ...

  6. js -- 正则表达式集合

    在做项目中,有时需要进行正则验证,但我又不太会正则表达式. 在一次又一次的寻找正则表达式的过程中,我最后总结了一个用于验证的函数,把我们常用的正则写在方法里,就不用每次都去网上找了. 可以根据需求进行 ...

  7. Codeforces Round #598 (Div. 3) B Minimize the Permutation

    B. Minimize the Permutation You are given a permutation of length nn. Recall that the permutation is ...

  8. CentOS 7下用firewall-cmd控制端口与端口转发

    # 将80端口的流量转发至192.168.0.1的8080端口 1.firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toad ...

  9. HTML学习(15)框架

    通过使用框架,你可以在同一个浏览器窗口中显示不止一个页面. <iframe src="//www.taobao.com" name="iframe_a" ...

  10. 在java中使用FFmpeg处理视频与音频

    FFmpeg是一个非常好用的视频处理工具,下面讲讲如何在java中使用该工具类. 一.首先,让我们来认识一下FFmpeg在Dos界面的常见操作 1.拷贝视频,并指定新的视频的名字以及格式 ffmpeg ...