HNOI宠物收养所
来源:互联网 发布:网络暴力的社会危害 编辑:程序博客网 时间:2024/04/28 20:12
很水的平衡树,看到C++们直接STL,我很是郁闷!还好,虽然咱pas党代码长,但我一次Ac还是很开心地~
program h8oj1208;type link=^node; node=record value,size:longint; pre:link; ch:array[0..1] of link; end;var root:link; now,n,a,b,ans:longint; pl,pr:link;procedure updata(x:link);begin x^.size:=1; if x^.ch[0]<>nil then inc(x^.size,x^.ch[0]^.size); if x^.ch[1]<>nil then inc(x^.size,x^.ch[1]^.size);end;procedure rotate(x:link;k:longint);var f:link;begin f:=x^.pre; f^.ch[1-k]:=x^.ch[k];if x^.ch[k]<>nil then x^.ch[k]^.pre:=f; x^.pre:=f^.pre; if f^.pre<>nil then if f^.pre^.ch[0]=f then f^.pre^.ch[0]:=x else f^.pre^.ch[1]:=x; x^.ch[k]:=f; f^.pre:=x; if f=root then root:=x; updata(f); updata(x);end;procedure splay(x,goal:link);var y,z:link;begin while x^.pre<>goal do if x^.pre^.pre=goal then if x^.pre^.ch[0]=x then rotate(x,1) else rotate(x,0) else begin y:=x^.pre; z:=y^.pre; if z^.ch[0]=y then if y^.ch[0]=x then begin rotate(y,1); rotate(x,1); end else begin rotate(x,0); rotate(x,1); end else if y^.ch[1]=x then begin rotate(y,0); rotate(x,0); end else begin rotate(x,1); rotate(x,0); end; end;end;procedure insert(x:longint);var p:link; procedure find(var r:link); begin if r=nil then begin new(r); with r^ do begin size:=1; value:=x; ch[0]:=nil; ch[1]:=nil; end; p:=r; end else begin inc(r^.size); if x>r^.value then begin find(r^.ch[1]); r^.ch[1]^.pre:=r; end else begin find(r^.ch[0]); r^.ch[0]^.pre:=r; end; end; end;begin find(root); root^.pre:=nil; splay(p,nil);end;function pre(x:longint):link; procedure find(r:link); begin if r=nil then exit; if r^.value<=x then begin pre:=r; find(r^.ch[1]); end else find(r^.ch[0]); end;begin pre:=nil;find(root);Splay(pre,nil);end;function succ(x:longint):link; procedure find(r:link); begin if r=nil then exit; if r^.value>=x then begin succ:=r; find(r^.ch[0]); end else find(r^.ch[1]); end;begin succ:=nil; find(root); splay(succ,nil);end;procedure delete(r:link);var p:link;begin splay(r,nil); if root^.ch[0]=nil then begin root:=root^.ch[1]; root^.pre:=nil; end else begin p:=root^.ch[0]; while p^.ch[1]<>nil do p:=p^.ch[1]; splay(p,root); p^.pre:=nil; p^.ch[1]:=root^.ch[1]; root^.ch[1]^.pre:=p; root:=p; updata(root); end;end;begin assign(input,'h8oj1208.in'); reset(input); assign(output,'h8oj1208.out'); rewrite(output); ans:=0; root:=nil;insert(-(1 shl 29));insert(1 shl 29); readln(n); while n>0 do begin dec(n); readln(a,b); //writeln(root^.size); if (root^.size=2)or(a=now) then begin insert(b); now:=a; end else begin pl:=pre(b); pr:=succ(b); writeln(pl^.value,'<=',b,'<=',pr^.value); if pr^.value-b<b-pl^.value then begin inc(ans,pr^.value-b); delete(pr) end else begin inc(ans,b-pl^.value); delete(pl); end; ans:=ans mod 1000000; end; end; write(ans); close(input); close(output);end.
- HNOI宠物收养所
- [HNOI 2004]宠物收养所
- bzoj 1208[HNOI 2004] 宠物收养所
- 【平衡树】宠物收养所 HNOI 2004
- BZOJ 1208 [HNOI 2004] 宠物收养所 (splay)
- BZOJ 1208 [HNOI 2004] 宠物收养所 Splay
- 【BZOJ 1208】【HNOI 2004】宠物收养所【treap】|【STL】
- 数据结构----AVL平衡树----[HNOI]宠物收养所
- HNOI 2004 宠物收养所(Splay树)
- [HNOI2004]宠物收养所
- HNOI2004宠物收养所
- HNOI2004-宠物收养所
- BZOJ1208 宠物收养所
- 宠物收养所
- wikioi1285宠物收养所
- 【HNOI2004】宠物收养所
- [HNOI2004]宠物收养所
- HNOI2004 宠物收养所
- struts2基于注解的拦截器配置
- 程序员真的很懒
- 算法系列之十二:多边形区域填充算法--几种边标志填充算法
- Windows 8 Metro 游戏开发技术初窥
- 求一个矩阵中最大的二维子矩阵(元素和最大)
- HNOI宠物收养所
- 看每日微软面试题之若个题的思路总结
- 计算机:语言篇
- SRP:单一职责原则
- 电子商务数据仓库架构
- .NET高级特性总结之“委托”
- dom4j 节选
- 沟通之难
- JDK动态代理