本文主要是介绍[BZOJ3224] Tyvj 1728 普通平衡树,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
传送门
http://www.lydsy.com/JudgeOnline/problem.php?id=3224
题目大意
支持以下操作
1. 插入x数
2. 删除x数(若有多个相同的数,因只删除一个)
3. 查询x数的排名(若有多个相同的数,因输出最小的排名)
4. 查询排名为x的数
5. 求x的前驱(前驱定义为小于x,且最大的数)
6. 求x的后继(后继定义为大于x,且最小的数)
题解
平衡树模板题
constmaxn=100005;
varw:array[-1..maxn,1..6]of longint;i,j,k:longint;n,sum,root,a,b:longint;
procedure print(a:longint);
var i:longint;
beginif 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;
beginb:=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]<>-1thenif w[w[a,3],1]=bthen w[w[a,3],1]:=aelse w[w[a,3],2]:=a;
end;procedure splay(a,goal:longint);
var b,kind,unkind:longint;
beginwhile w[a,3]<>goal dobeginb:=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)elseif w[w[b,3],kind]=bthen begin rotate(b,kind); rotate(a,kind); endelse 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;
begintt:=root;while tt<>-1 dobegininc(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]; endelse begin kind:=2; tt:=w[tt,2]; end;end;if w[tt,6]=athen 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;
begintt:=a;while w[tt,2]<>-1 dott:=w[tt,2];exit(tt);
end;function getmin(a:longint):longint;
var tt:longint;
begintt:=a;while w[tt,1]<>-1 dott:=w[tt,1];exit(tt);
end;procedure del(a:longint);
var tt:longint;
begintt:=root;while w[tt,6]<>a doif a<w[tt,6]then tt:=w[tt,1]else tt:=w[tt,2];splay(tt,-1);if w[root,5]=1then beginsplay(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;endelse begin dec(w[root,5]); dec(w[root,4]); end;
end;function getrank(a:longint):longint;
var tt:longint;
begintt:=root;while w[tt,6]<>a doif 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;
begintt:=root;while (a<=w[w[tt,1],4])or(a>w[w[tt,1],4]+w[tt,5]) doif 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;beginreadln(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 dobeginreadln(a,b);case a of1: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.
这篇关于[BZOJ3224] Tyvj 1728 普通平衡树的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!