bz上为数不多的模板题,致敬一下
代码长度应该可以缩短,就不多说了,贴一个先
var i,j,k,l,m,n,p,q,t,tt:longint; key,s,left,right:array[0..100005] of longint; procedure lro(var t:longint); var k:longint; begin k:=right[t]; right[t]:=left[k]; left[k]:=t; s[k]:=s[t]; s[t]:=s[left[t]]+s[right[t]]+1; t:=k; end; procedure rro(var t:longint); var k:longint; begin k:=left[t]; left[t]:=right[k]; right[k]:=t; s[k]:=s[t]; s[t]:=s[left[t]]+s[right[t]]+1; t:=k; end; procedure maintain(var t:longint;flag:boolean); begin if not flag then begin if s[left[left[t]]]>s[right[t]] then rro(t) else if s[right[left[t]]]>s[right[t]] then begin lro(left[t]); rro(t); end else exit; end else begin if s[right[right[t]]]>s[left[t]] then lro(t) else if s[left[right[t]]]>s[left[t]] then begin rro(right[t]); lro(t); end else exit; end; maintain(left[t],false); maintain(right[t],true); maintain(t,false); maintain(t,true); end; procedure insert(var t:longint;v:longint); begin if t=0 then begin inc(tt); t:=tt; s[t]:=1; key[t]:=v; end else begin inc(s[t]); if v=key[t]); end; end; function delete(var t:longint;v:longint):longint; begin dec(s[t]); if (v=key[t])or(v key[t])and(right[t]=0) then begin delete:=key[t]; if (left[t]=0)or(right[t]=0) then t:=left[t]+right[t] else key[t]:=delete(left[t],key[t]+1); end else if v v then exit(select(left[t],v)) else exit(select(right[t],v-s[left[t]]-1)); end; function pred(t,v:longint):longint; begin if t=0 then exit(-1); if key[t]>=v then exit(pred(left[t],v)) else begin pred:=pred(right[t],v); if pred=-1 then exit(key[t]); end; end; function succ(t,v:longint):longint; begin if t=0 then exit(-1); if key[t]<=v then exit(succ(right[t],v)) else begin succ:=succ(left[t],v); if succ=-1 then exit(key[t]); end; end; begin readln(n); for i:=1 to n do begin readln(p,q); case p of 1:insert(t,q); 2:delete(t,q); 3:writeln(rank(t,q)); 4:writeln(select(t,q)); 5:writeln(pred(t,q)); 6:writeln(succ(t,q)); end; end; end.