莫队算法?

感觉没什么优越性啊?难道就是因为在排序的时候cmp函数的不同?这样做为什么减少时限啊?

我带着疑惑敲了代码,却一直有bug……

代码:

 type node=record
l,r,id,x,y:int64;
end;
var a,ans:array[..] of node;
s,c,p:array[..] of longint;
i,n,m,block,l,r,k:longint;
anss:int64;
function gcd(x,y:longint):longint;
begin
if y= then exit(x) else exit(gcd(y,x mod y));
end;
function cmp(x,y:node):boolean;
begin
if p[x.l]=p[y.l] then exit(x.r<y.r);
exit(x.l<y.l);
end;
procedure sort(h,l:longint);
var i,j:longint;
tmp,mm:node;
begin
i:=h;j:=l;mm:=a[(i+j)>>];
repeat
while cmp(a[i],mm) do inc(i);
while cmp(mm,a[j]) do dec(j);
if i<=j then
begin
tmp:=a[i];a[i]:=a[j];a[j]:=tmp;
inc(i);dec(j);
end;
until i>j ;
if i<l then sort(i,l);
if j>h then sort(h,j);
end;
procedure init;
begin
readln(n,m);
for i:= to n do read(c[i]);
block:=trunc(sqrt(n));
for i:= to n do p[i]:=(i-) div block+;
for i:= to m do
begin
readln(a[i].l,a[i].r);
a[i].id:=i;
end;
end;
procedure update(p,add:longint);
begin
dec(anss,sqr(s[c[p]]));
inc(s[c[p]],add);
inc(anss,sqr(s[c[p]]));
end;
procedure main;
begin
sort(,m);
l:=;r:=;
for i:= to m do
begin
while r<a[i].r do
begin
update(r+,);inc(r);
end;
while r>a[i].r do
begin
update(r,-);dec(r);
end;
while l<a[i].l do
begin
update(l,-);inc(l);
end;
while l>a[i].l do
begin
update(l-,);dec(l);
end;
if a[i].l=a[i].r then
begin
a[i].x:=;a[i].y:=;
continue;
end;
with a[i] do
begin
x:=anss-(r-l+);
y:=(r-l+)*(r-l);
k:=gcd(x,y);
x:=x div k;y:=y div k;
end;
end;
for i:= to m do ans[a[i].id]:=a[i];
for i:= to m do with ans[i] do writeln(x,'/',y);
end;
begin
init;
main;
end.

2009国家集训队小Z的袜子的更多相关文章

  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的袜子 莫队

    2038: [2009国家集训队]小Z的袜子(hose) 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Descriptionw ...

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

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

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

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

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

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

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

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

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

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

  10. BZOJ_2038_[2009国家集训队]小Z的袜子(hose)_莫队

    BZOJ_2038_[2009国家集训队]小Z的袜子(hose)_莫队 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无 ...

随机推荐

  1. Kakfa揭秘 Day5 SocketServer下的NIO

    Kakfa揭秘 Day5 SocketServer下的NIO 整个Kafka底层都是基于NIO来进行开发的,这种消息机制可以达到弱耦合的效果,同时在磁盘有很多数据时,会非常的高效,在gc方面有非常大的 ...

  2. 关于rsync的密码问题

    今天在做关于rsync的实验时,发现关于rsync的密码问题还是有几点需要注意的,因此特意归纳了一下(rsync的版本是3.1.0): 1.服务器端的密码文件的格式必须为“用户名:密码”,所属者必须为 ...

  3. String Subtraction

    Given two strings S1 and S2, S = S1 - S2 is defined to be the remaining string after taking all the ...

  4. Web Api 控制器

    Web Api 控制器 文档目录 本节内容: 简介 AbpApiController 基类 本地化 其它 过滤 审计日志 授权 防伪造过滤 工作单元 结果包装和异常处理 结果缓存 验证 模块绑定器 简 ...

  5. vs中的主题配置

    比较讨厌vs的默认字体颜色及背景,代码看久了就受不了.不过自己手动配置的话还是比较麻烦的,这里有一个网站(http://studiostyl.es/)提供了很多主题,一般都是背景比较深色的,对眼睛比较 ...

  6. Codeforces Round #260 (Div. 1) C. Civilization 树的中心+并查集

    题目链接: 题目 C. Civilization time limit per test1 second memory limit per test256 megabytes inputstandar ...

  7. 如何循环遍历document.querySelectorAll()方法返回的结果

    使用JavaScript的forEach方法,我们可以轻松的循环一个数组,但如果你认为document.querySelectorAll()方法返回的应该是个数组,而使用forEach循环它: /* ...

  8. 图片流滚动效果html代码(复制)

    <!doctype html> <html> <head>     <meta charset="utf-8" />     < ...

  9. 【leetcode】4Sum(middle)

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  10. JAVA面试题:Spring中bean的生命周期

    Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...