[BZOJ3224] Tyvj 1728 普通平衡树
2016-01-10 19:21
344 查看
传送门
http://www.lydsy.com/JudgeOnline/problem.php?id=3224题目大意
支持以下操作1. 插入x数
2. 删除x数(若有多个相同的数,因只删除一个)
3. 查询x数的排名(若有多个相同的数,因输出最小的排名)
4. 查询排名为x的数
5. 求x的前驱(前驱定义为小于x,且最大的数)
6. 求x的后继(后继定义为大于x,且最小的数)
题解
平衡树模板题const maxn=100005; var w:array[-1..maxn,1..6]of longint; i,j,k:longint; n,sum,root,a,b:longint; procedure print(a:longint); var i:longint; begin if w[a,1]<>-1 then print(w[a,1]); for i:=1 to w[a,5] do write(w[a,6],' '); if w[a,2]<>-1 then print(w[a,2]); if a=root then writeln; end; procedure rotate(a,kind:longint); var b,unkind:longint; begin b:=w[a,3]; unkind:=kind xor 3; w[a,4]:=w[b,4]; dec(w[b,4],w[a,5]+w[w[a,kind],4]); w[b,kind]:=w[a,unkind]; w[w[a,unkind],3]:=b; w[a,unkind]:=b; w[a,3]:=w[b,3]; w[b,3]:=a; if w[a,3]<>-1 then if w[w[a,3],1]=b then w[w[a,3],1]:=a else w[w[a,3],2]:=a; end; procedure splay(a,goal:longint); var b,kind,unkind:longint; begin while w[a,3]<>goal do begin b:=w[a,3]; if w[b,1]=a then kind:=1 else kind:=2; unkind:=kind xor 3; if w[b,3]=goal then rotate(a,kind) else if w[w[b,3],kind]=b then begin rotate(b,kind); rotate(a,kind); end else begin rotate(a,kind); rotate(a,unkind); end; end; if goal=-1 then root:=a; end; procedure init(a:longint); var tt,fa,kind:longint; begin tt:=root; while tt<>-1 do begin inc(w[tt,4]); if w[tt,6]=a then break; fa:=tt; if a<w[tt,6] then begin kind:=1; tt:=w[tt,1]; end else begin kind:=2; tt:=w[tt,2]; end; end; if w[tt,6]=a then inc(w[tt,5]) else begin inc(sum); w[sum,1]:=-1; w[sum,2]:=-1; w[sum,3]:=fa; w[sum,4]:=1; w[sum,5]:=1; w[sum,6]:=a; w[fa,kind]:=sum; tt:=sum; end; splay(tt,-1); end; function getmax(a:longint):longint; var tt:longint; begin tt:=a; while w[tt,2]<>-1 do tt:=w[tt,2]; exit(tt); end; function getmin(a:longint):longint; var tt:longint; begin tt:=a; while w[tt,1]<>-1 do tt:=w[tt,1]; exit(tt); end; procedure del(a:longint); var tt:longint; begin tt:=root; while w[tt,6]<>a do if a<w[tt,6] then tt:=w[tt,1] else tt:=w[tt,2]; splay(tt,-1); if w[root,5]=1 then begin splay(getmax(w[root,1]),root); w[w[root,1],2]:=w[root,2]; w[w[root,1],4]:=w[root,4]-1; root:=w[root,1]; w[w[root,2],3]:=root; w[root,3]:=-1; end else begin dec(w[root,5]); dec(w[root,4]); end; end; function getrank(a:longint):longint; var tt:longint; begin tt:=root; while w[tt,6]<>a do if a<w[tt,6] then tt:=w[tt,1] else tt:=w[tt,2]; splay(tt,-1); exit(w[w[tt,1],4]); end; function getkth(a:longint):longint; var tt:longint; begin tt:=root; while (a<=w[w[tt,1],4])or(a>w[w[tt,1],4]+w[tt,5]) do if a<=w[w[tt,1],4] then tt:=w[tt,1] else begin dec(a,w[w[tt,1],4]+w[tt,5]); tt:=w[tt,2]; end; exit(w[tt,6]); end; begin readln(n); sum:=2; root:=2; w[1,1]:=-1; w[1,2]:=-1; w[1,3]:=2; w[1,4]:=1; w[1,5]:=1; w[1,6]:=-1000000005; w[2,1]:=1; w[2,2]:=-1; w[2,3]:=-1; w[2,4]:=2; w[2,5]:=1; w[2,6]:=1000000005; for i:=1 to n do begin readln(a,b); case a of 1:begin init(b); end; 2:begin del(b); end; 3:begin writeln(getrank(b)); end; 4:begin writeln(getkth(b+1)); end; 5:begin init(b); writeln(w[getmax(w[root,1]),6]); del(b); end; 6:begin init(b); writeln(w[getmin(w[root,2]),6]); del(b); end; end; end; end.
相关文章推荐
- 196,UITableView之cell增删移
- android OpenGL(二) 定义顶点和着色器
- 徐宥
- 使用DML语句更改数据
- MyBatis_查询缓存
- 第一篇blog
- 浅谈HTTP中Get与Post的区别
- Access-简易进销存管理系统
- 对矩阵进行转置运算
- 心情小记
- ExtJS学习-----------Ext.Array,ExtJS对javascript中的Array的扩展
- C# 委托和事件
- 常用数据结构Stack
- hdoj2042不容易系列之二
- android之JSON 进行网络数据交换
- HTML+CSS+Asp.net 打造高大上UI
- [BZOJ1503] [NOI2004]郁闷的出纳员
- BZOJ 2600: [Ioi2011]ricehub|暴力|中位数
- Redis 配置
- 图书数据库以及操作界面