题意:在[l, r]之中任选两个数,求它们相同的概率。

解:

莫队入门。

概率这个很好搞,就是cnt * (cnt - 1) / 2。

然后发现每次挪指针的时候,某一个cnt会+1或-1。这时候差值就是2 * cntsmall

 #include <cstdio>
#include <algorithm>
#include <cmath> typedef long long LL;
const int N = ; int a[N], bin[N], fr[N];
LL ans; struct ASK {
int id, l, r;
LL ans;
inline bool operator <(const ASK &w) const {
if(fr[l] != fr[w.l]) {
return fr[l] < fr[w.l];
}
return r < w.r;
}
}ask[N]; inline bool cmp(const ASK &a, const ASK &b) {
return a.id < b.id;
} inline LL gcd(LL a, LL b) {
if(!b) {
return a;
}
return gcd(b, a % b);
} inline void add(int x) {
ans += bin[a[x]] << ;
bin[a[x]]++;
return;
} inline void del(int x) {
bin[a[x]]--;
ans -= bin[a[x]] << ;
} int main() {
int n, m;
scanf("%d%d", &n, &m);
int T = sqrt(n);
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
fr[i] = (i - ) / T + ;
}
for(int i = ; i <= m; i++) {
ask[i].id = i;
scanf("%d%d", &ask[i].l, &ask[i].r);
}
std::sort(ask + , ask + m + ); int L = , R = ;
bin[a[]]++;
for(int i = ; i <= m; i++) {
if(ask[i].l == ask[i].r) {
continue;
}
while(ask[i].l < L) {
add(--L);
}
while(R < ask[i].r) {
add(++R);
}
while(L < ask[i].l) {
del(L++);
}
while(ask[i].r < R) {
del(R--);
}
ask[i].ans = ans;
} std::sort(ask + , ask + m + , cmp);
for(int i = ; i <= m; i++) {
if(!ask[i].ans || ask[i].l == ask[i].r) {
puts("0/1");
continue;
}
int g = gcd(ask[i].ans, 1ll * (ask[i].r - ask[i].l + ) * (ask[i].r - ask[i].l));
printf("%lld/%lld\n", ask[i].ans / g, 1ll * (ask[i].r - ask[i].l + ) * (ask[i].r - ask[i].l) / g);
}
return ;
}

AC代码

洛谷P1494 小Z的袜子的更多相关文章

  1. Bzoj2038/洛谷P1494 小Z的袜子(莫队)

    题面 Bzoj 洛谷 题解 考虑莫队算法,首先对询问进行分块(分块大小为\(sqrt(n)\)),对于同一个块内的询问,按照左端点为第一关键字,右端点为第二关键字排序.我们统计这个区间内相同的颜色有多 ...

  2. 洛谷P1494小Z的袜子 [国家集训队] 莫队

    正解:莫队 解题报告: 这是,传送门qwq 昂大概是莫队板子题? 首先可以推出来答案是(∑C(2,color[i]))/C(2,r-l+1)趴?挺显然的不解释了qwq 然后显然除数直接做就成,考虑怎么 ...

  3. P1494 小Z的袜子

    P1494 小Z的袜子 莫队板子题,对询问进行排序+分块,从而得到巧妙的复杂度 对于L,R的询问. 设其中颜色为x,y,z的袜子的个数为a,b,c... 那么答案即为 (a*(a-1)/2+b*(b- ...

  4. 洛谷 2186 小Z的栈函数

    https://www.luogu.org/problem/show?pid=2186 题目描述 小Z最近发现了一个神奇的机器,这个机器的所有操作都是通过维护一个栈来完成的,它支持如下11个操作: N ...

  5. 洛谷——P2117 小Z的矩阵

    P2117 小Z的矩阵 题目描述 小Z最近迷上了矩阵,他定义了一个对于一种特殊矩阵的特征函数G.对于N*N的矩阵A,A的所有元素均为0或1,则G(A)等于所有A[i][j]*A[j][i]的和对2取余 ...

  6. 洛谷P2188 小Z的 k 紧凑数

    P2188 小Z的 k 紧凑数 题目描述 小 Z 在草稿纸上列出了很多数,他觉得相邻两位数字差的绝对值不超过 k 的整数特别奇特,称其为 k 紧凑数. 现在小 Z 想知道 [l,r] 内有多少个 k ...

  7. 洛谷—— P2117 小Z的矩阵

    https://www.luogu.org/problemnew/show/2117 题目描述 小Z最近迷上了矩阵,他定义了一个对于一种特殊矩阵的特征函数G.对于N*N的矩阵A,A的所有元素均为0或1 ...

  8. 洛谷 P2117 小Z的矩阵

    P2117 小Z的矩阵 题目描述 小Z最近迷上了矩阵,他定义了一个对于一种特殊矩阵的特征函数G.对于N*N的矩阵A,A的所有元素均为0或1,则G(A)等于所有A[i][j]*A[j][i]的和对2取余 ...

  9. [洛谷P2186] 小Z的栈函数

    题目链接: 传送门 题目分析: 大模拟,先得存操作,然后再处理每个数-- 有一个小优化,在处理操作的时候顺便判一下最后栈里是不是有且仅有一个数,但A完了才想起来,所以就算了-- 总之就是个模拟题--没 ...

随机推荐

  1. Day 3-3 内置方法

    常用内置函数方法: min,max li = [1, 2, 3, 6, 9, 5, 10, 26] print('li的最小值是:', min(li)) # 取最小值 print('li的最大值是:' ...

  2. gitlab+jenkins

    一.安装好gitlab.jenkins yum install -y java wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat-sta ...

  3. SpringBoot标签之@ConfigurationProperties、@PropertySource注解的使用

    当获取主配置文件中属性值时,只需@ConfigurationProperties(prefix = "person")注解来修饰某类,其作用是告诉springBoot,此类中的属性 ...

  4. 四、Mysql主从同步

    一.MySQL Replication介绍 MySQL Replication 官方文档 Replication可以实现将数据从一台数据库服务器(master)复制到一或多台数据库服务器(slave) ...

  5. 二、启用Docker支持

    一.使用

  6. Python 第三方库 cp27、cp35 等文件名的含义(转)

    转自 https://blog.csdn.net/lanchunhui/article/details/62417519 转自 https://stackoverflow.com/questions/ ...

  7. Python——进程队列

    队列 先进先出 from multiprocessing import Queue q = Queue(5) #队列的大小 q.put(1) #放入内容 q.put(2) #放入内容 q.put(3) ...

  8. Lodop打印项对象类型属性区分

    Lodop提供了一些打印项类型,默认是普通项,通过设置打印对象的类型,可以实现一些普通项不能实现的效果.例如:该博客另一篇博文 标题是Lodop打印控件 打印‘接下一页’‘以下空白’,就用了眉脚项. ...

  9. SQL Server中获取指定时间段内的所有月份

    例如查询 2012-1-5 到 2012-11-3 之间所有的月份 declare @begin datetime,@end datetime set @begin='2012-1-5' set @e ...

  10. Maven问题:Failure to transfer org.apache.maven

    Maven报错:Failure to transfer org.apache.maven 在创建Maven项目时,经常会在pom.xml的第一行处报错,提示信息如下: Failure to trans ...