题意:中文题。

析:很著名的莫队算法,先把这个求概率的式子表达出来,应该是分子:C(x1, 2) + C(x2, 2) + C(x3, 2) + ... + C(xn, 2)  分母:C(n, 2),然后化成分数的表达形式,[x1(x1-1)+x2(x2-1)+...+xn(xn-1)] / (n*(n-1))  然后再化简得到 (sigma(xi*xi)  - n) / (n*(n-1)) ,然后就是对每个区间进行运算,离线,把所以的序列分成sqrt(n)块,然后用两个指针,进行对数据的计算。

注意不要用I64d,用的话WA到死。。。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <assert.h>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 0xffffffffffLL;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int maxn = 50000 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} LL ansx[maxn], ansy[maxn];
int pos[maxn], val[maxn];
int cnt[maxn]; struct Node{
int l, r, id;
bool operator < (const Node &p) const{
return pos[l] < pos[p.l] || pos[l] == pos[p.l] && r < p.r;
}
}; Node a[maxn]; LL x, y; void update(int l, int ok){
x -= cnt[val[l]] * (LL)cnt[val[l]];
cnt[val[l]] += ok;
x += cnt[val[l]] * (LL)cnt[val[l]] - ok;
y += ok;
} void update(int i){
ansx[i] = x;
ansy[i] = y * (y - 1);
} int main(){
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; ++i) scanf("%d", val+i);
for(int i = 0; i < m; ++i){
scanf("%d %d", &a[i].l, &a[i].r);
a[i].id = i;
}
int t = sqrt(n + 0.5);
for(int i = 1; i <= n; ++i)
pos[i] = i / t;
sort(a, a + m);
for(int l = 1, i = 0 ,r = 0; i < m; ++i){
int L = a[i].l, R = a[i].r;
while(l < L) update(l++, -1);
while(l > L) update(--l, 1);
while(r < R) update(++r, 1);
while(r > R) update(r--, -1);
update(a[i].id);
}
for(int i = 0; i < m; ++i){
LL g = gcd(ansx[i], ansy[i]);
printf("%lld/%lld\n", ansx[i]/g, ansy[i]/g);
}
return 0;
}

  

BZOJ 2038 小Z的袜子(hose) (莫队算法)的更多相关文章

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

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

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

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

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

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

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

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

  5. BZOJ2038: [2009国家集训队]小Z的袜子(hose) -- 莫队算法 ,,分块

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

  6. kyeremal-bzoj2038-[2009国家集训队]-小z的袜子(hose)-莫队算法

    id=2038">bzoj2038-[2009国家集训队]-小z的袜子(hose) F.A.Qs Home Discuss ProblemSet Status Ranklist Con ...

  7. [BZOJ2038] [2009国家集训队]小Z的袜子(hose) 莫队算法练习

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

  8. Bzoj 2038---[2009国家集训队]小Z的袜子(hose) 莫队算法

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

  9. BZOJ - 2038 小Z的袜子(普通莫队)

    题目链接:小Z的袜子 题意:$n$只袜子,$m$个询问,每次回答有多大概率在$[L,R]$区间内抽到两只颜色相同的袜子 思路:普通莫队,如果两个询问左端点在一个块内,则按询问右端点排序,否则按照所在块 ...

随机推荐

  1. bash中常用的快捷键

    常用快捷键 ctrl+c 强制终止当前命令 ctrl+l 清屏(clear) ctrl+a 光标移动到命令行行首 ctrl+e 光标移动到命令行行尾 ctrl+u 从光标所在位置删除到行首 ctrl+ ...

  2. spring aop实现拦截接口请求打印日志

    在spring配置 1编写自己的注解类 2.编写注解解析类 3.配置spring aop代理 (下面我使用注解 如使用配置 配置切点即可,有两种代理默认jdk代理 设置true 为cglib代理) / ...

  3. VirtualBox从USB设备(PE)启动图文教程

    很多朋友都为VBox无法进入U盘启动而苦恼,别懊恼,魔笛这就为你讲解 方法/步骤   1 大家先把U盘插入实体电脑. 单击“确定”打开“磁盘管理”, 键盘上的Win+R组合键,输入diskmgnt.. ...

  4. linux网络监视器

    vnstat iftop nethogs dig ipcalc

  5. Linux系统SVN安装指导配置说明

    Linux 系统 SVN 安装指导配置说明   1 安装 SVN 前检查: 检查是否安装 SVN : rpm -qa subversion 查看安装 SVN 版本信息: svnserve --vers ...

  6. Netty面试题

    1.BIO.NIO和AIO的区别? BIO:一个连接一个线程,客户端有连接请求时服务器端就需要启动一个线程进行处理.线程开销大. 伪异步IO:将请求连接放入线程池,一对多,但线程还是很宝贵的资源. N ...

  7. spring-boot-actuator健康监控

    #健康监控 management.security.enabled=false health.mail.enabled =false http://localhost:54001/autoconfig ...

  8. sitemap和sitemapindex

    介绍: https://support.google.com/webmasters/answer/75712?hl=zh-Hans 实例: http://www.thepaper.cn/sitemap ...

  9. 4.2 最邻近规则分类(K-Nearest Neighbor)KNN算法应用

    1 数据集介绍:   虹膜     150个实例   萼片长度,萼片宽度,花瓣长度,花瓣宽度 (sepal length, sepal width, petal length and petal wi ...

  10. springboot 的定时任务使用

    定时任务在Spring Boot中的集成 在启动类中加入开启定时任务的注解: 在SpringBoot中使用定时任务相当的简单.首先,我们在启动类中加入@EnableScheduling来开启定时任务. ...