题意:中文题。

析:很著名的莫队算法,先把这个求概率的式子表达出来,应该是分子: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. jdbc调用sparksql on yarn

    spark sql访问hive表 1.将hive-site.xml拷贝到spark目录下conf文件夹 2.(非必需)将mysql的jar包引入到spark的classpath,方式有如下两种: 方式 ...

  2. jQuery笔记——UI

    jQuery UI 的官网网站为:http://jqueryui.com/,我们下载最新版本的即可,使用JQueryUI中的样式比我们使用原生的HTML要好看,还会有一些封装好的特效,JQueryUI ...

  3. Java 中 wait, notify 和 notifyAll的正确使用 – 以生产者消费者模型为例

    如何使用Wait 尽管关于wait和notify的概念很基础,它们也都是Object类的函数,但用它们来写代码却并不简单.如果你在面试中让应聘者来手写代码,用wait和notify解决生产者消费者问题 ...

  4. zoj-3433-Gu Jian Qi Tan

    /* Gu Jian Qi Tan -------------------------------------------------------------------------------- T ...

  5. Change R source code

    If you'd like to simply test out the effect of that change in an interactive R session, you can do s ...

  6. mac下mysql5.7.18修改root密码

    参考:http://blog.csdn.net/lijilong_/article/details/70991809 第一步:苹果->系统偏好设置->最下面点MySQL,关闭mysql服务 ...

  7. 三、postman测试断言设置语法

    postman的基本介绍跟基本的用法,在此就不做过多赘述,主要是受限于时间,网上有很多大神总结的已经算是很详尽了,给出链接并感谢大神的辛苦与奉献: postman基本用法:http://www.jia ...

  8. 怎么分辨linux系统是虚拟机还是物理机

    用lspci -b|grep "VMware"抓取系列信息,即表明此系统的宿主机是基于VMware虚拟出来的,别的可以照葫芦画瓢来!

  9. C#中有关数组和string引用类型或值类型的判断

    直接来一段测试代码 class value_ref_type { public static void DEMO1() { ] { }; double[] location_new; string s ...

  10. C#接口的三种实现方式

    转自原文C#接口的三种实现方式 public interface MyInterface { /// 下面三个方法的签名都是 /// .method public hidebysig newslot ...