Description

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

输入文件第一行包含两个正整数N和M。N为袜子的数量,M为小Z所提的询问的数量。接下来一行包含N个正整数Ci,其中Ci表示第i只袜子的颜色,相同的颜色用相同的数字表示。再接下来M行,每行两个正整数L,R表示一个询问。
Output

包含M行,对于每个询问在一行中输出分数A/B表示从该询问的区间[L,R]中随机抽出两只袜子颜色相同的概率。若该概率为0则输出0/1,否则输出的A/B必须为最简分数。(详见样例)
Sample Input
6 4
1 2 3 3 3 2
2 6
1 3
3 5
1 6
Sample Output
2/5
0/1
1/1
4/15
【样例解释】
询问1:共C(5,2)=10种可能,其中抽出两个2有1种可能,抽出两个3有3种可能,概率为(1+3)/10=4/10=2/5。
询问2:共C(3,2)=3种可能,无法抽到颜色相同的袜子,概率为0/3=0/1。
询问3:共C(3,2)=3种可能,均为抽出两个3,概率为3/3=1/1。
注:上述C(a, b)表示组合数,组合数C(a, b)等价于在a个不同的物品中选取b个的选取方案数。
【数据规模和约定】
30%的数据中 N,M ≤ 5000;
60%的数据中 N,M ≤ 25000;
100%的数据中 N,M ≤ 50000,1 ≤ L < R ≤ N,Ci ≤ N。

莫队算法基础题

莫队算法是一种优化的思想

给很多组询问,每次询问一个区间的答案

我们要用一个神奇的数据结构,维护区间信息,可以由(l,r)推出(l+1,r)和(l,r+1)和(l-1,r)和(l,r-1)

这时我们把询问看成点(l,r),两个询问的转移的代价就是曼哈顿距离

然后我们求出曼哈顿最小生成树,再dfs一遍就行了,但是这样很麻烦,所以就有了一个替代品,很容易写

具体操作是先分块,分成根号n块,对询问的区间排序,第一关键字是l所在的块的编号,第二关键字是r

然后就是暴力了,用那个神奇的数据结构从上一次的询问推到这一次的询问

可以证明,这样复杂度是O(n^1.5*那个神奇的数据结构操作一次的时间)

1.对于l,因为我们按块排序,所以每一次变化不超过n^0.5,一共n次,所以是n^1.5

2.对于r,有两种情况

1>l在同一个块,因为第二关键字是r,所以这时r递增,一个块最多O(n),有n^0.5个块,所以是O(n^1.5)

2>l跨块,r最多变化n,最多有n^0.5次跨块,所以为O(n^1.5)

 const
maxn=;
var
ll,rr,a,c,s,kuai:array[..maxn]of longint;
ans:array[..maxn,..]of int64;
n,m,w:longint; procedure swap(var x,y:longint);
var
t:longint;
begin
t:=x;x:=y;y:=t;
end; procedure sort(l,r:longint);
var
i,j,y,z:longint;
begin
i:=l;
j:=r;
y:=kuai[ll[(l+r)>>]];
z:=rr[(l+r)>>];
repeat
while (kuai[ll[i]]<y)or((kuai[ll[i]]=y)and(rr[i]<z)) do
inc(i);
while (kuai[ll[j]]>y)or((kuai[ll[j]]=y)and(rr[j]>z)) do
dec(j);
if i<=j then
begin
swap(ll[i],ll[j]);
swap(rr[i],rr[j]);
swap(a[i],a[j]);
inc(i);
dec(j);
end;
until i>j;
if i<r then sort(i,r);
if j>l then sort(l,j);
end; procedure init;
var
i:longint;
begin
read(n,m);
w:=trunc(sqrt(n));
for i:= to n do
kuai[i]:=i div w;
for i:= to n do
read(c[i]);
for i:= to m do
read(ll[i],rr[i]);
for i:= to m do
a[i]:=i;
sort(,m);
end; function f(x:int64):int64;
begin
exit((x*(x-))>>);
end; function gcd(a,b:int64):int64;
begin
if b= then exit(a);
exit(gcd(b,a mod b));
end; procedure insert(x:longint;a,b:int64);
var
t:longint;
begin
if a= then
begin
ans[x,]:=;
ans[x,]:=;
end;
t:=gcd(a,b);
a:=a div t;
b:=b div t;
ans[x,]:=a;
ans[x,]:=b;
end; procedure work;
var
lastl,lastr,i,j:longint;
sum:int64;
begin
lastl:=;
lastr:=;
sum:=;
for i:= to m do
begin
for j:=lastr+ to rr[i] do
begin
inc(s[c[j]]);
sum:=sum+f(s[c[j]])-f(s[c[j]]-);
end;
for j:=ll[i] to lastl- do
begin
inc(s[c[j]]);
sum:=sum+f(s[c[j]])-f(s[c[j]]-);
end;
for j:=rr[i]+ to lastr do
begin
dec(s[c[j]]);
sum:=sum+f(s[c[j]])-f(s[c[j]]+);
end;
for j:=lastl to ll[i]- do
begin
dec(s[c[j]]);
sum:=sum+f(s[c[j]])-f(s[c[j]]+);
end;
lastl:=ll[i];
lastr:=rr[i];
insert(a[i],sum,f(rr[i]-ll[i]+));
end;
for i:= to m do
writeln(ans[i,],'/',ans[i,]);
end; begin
init;
work;
end.

2038: [2009国家集训队]小Z的袜子(hose) - BZOJ的更多相关文章

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

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

  2. BZOJ 2038: [2009国家集训队]小Z的袜子(hose)

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

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

    链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 ...

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

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

  5. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) ( 莫队 )

    莫队..先按sqrt(n)分块, 然后按块的顺序对询问排序, 同块就按右端点排序. 然后就按排序后的顺序暴力求解即可. 时间复杂度O(n1.5) --------------------------- ...

  6. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) 分块

    分块大法好 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MB Submit: 2938  Solved: 13 ...

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

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

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

    题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=2038 专题练习: http://acm.hust.edu.cn/vjudge/conte ...

  9. 2038: [2009国家集训队]小Z的袜子(hose)

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

随机推荐

  1. Swift 性能探索和优化分析

    本文首发在 CSDN<程序员>杂志,订阅地址 http://dingyue.programmer.com.cn/. Apple 在推出 Swift 时就将其冠以先进,安全和高效的新一代编程 ...

  2. over Oracle

    QL code: sql over的作用及用法RANK ( ) OVER ( [query_partition_clause] order_by_clause )DENSE_RANK ( ) OVER ...

  3. SQLSERVER2012数据库还原

    (1)还原已存在数据库 还原时提示失败,因为数据库正在使用,无法获得独占访问权.解决办法,先分离已存在的数据库,再执行还原操作. 确定后居然成功了,因为我是先使用(2)还原数据库后直接尝试(1)方法还 ...

  4. UIViewSubviews多个views之间的关系

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  5. linux下进入root

    baoyu@ubuntu:~$ sudo password root sudo: password: command not found baoyu@ubuntu:~$ sudo passwd roo ...

  6. Axure RP 各个版本中文版 汉化包 破解版 下载地址及注册码

    导读:Axure RP Pro是一个产品经理必备的交互原型设计工具,能够高效率制作产品原型,快速绘制线框图.流程图.网站架构图.示意图.HTML模版等.Axure RP已被一些大公司采用.Axure ...

  7. 使用ckplayer搭建rtmp视频直播应用

    视频直播才有的是RTMP协议进行视频实时流传输,在这里我们用到的软件都是 adobe 公司的一个是:Flash Media Server4 另一个是flash media live encoder 这 ...

  8. 锋利的jquery-事件和动画

    1 注册事件的触发时机 在jquery中,$(window).load(function(){}) 注册在window下的事件等待页面所有资源加载完成(包括dome树的加载和图片视频的资源的加载) $ ...

  9. SQL Server数据库连接类SQLHelper.cs

    using System; using System.Collections.Generic; using System.Text; using System.Configuration; using ...

  10. coreseek安装使用

    本文引自:http://www.phperz.com/article/14/0615/95.html sphinx的安装使用,其实都是大同小异,以下以 coreseek安装为例Coreseek 是一款 ...