bzoj 2761 平衡树

裸地平衡树,只需要用到find操作

/**************************************************************
    Problem: 2761
    User: BLADEVIL
    Language: Pascal
    Result: Accepted
    Time:1884 ms
    Memory:1788 kb
****************************************************************/
 
//By BLADEVIL
var
    tt, n                   :longint;
    tot, t                  :longint;
    left, right, size, key  :array[0..100010] of longint;
    i                       :longint;
     
procedure left_rotate(var t:longint);
var
    k                       :longint;
begin
    k:=right[t];
    right[t]:=left[k];
    left[k]:=t;
    size[k]:=size[t];
    size[t]:=size[left[t]]+size[right[t]]+1;
    t:=k;
end;
 
procedure right_rotate(var t:longint);
var
    k                       :longint;
begin
    k:=left[t];
    left[t]:=right[k];
    right[k]:=t;
    size[k]:=size[t];
    size[t]:=size[left[t]]+size[right[t]]+1;    
    t:=k;
end;
     
procedure maintain(var t:longint; flag:boolean);
begin
    if not flag then
    begin
        if size[left[left[t]]]>size[right[t]] then
            right_rotate(t) else
        if size[right[left[t]]]>size[left[t]] then
        begin
            left_rotate(left[t]);
            right_rotate(t);
        end else exit;
    end else
    begin
        if size[right[right[t]]]>size[left[t]] then
            left_rotate(t) else
        if size[left[right[t]]]>size[left[t]] then
        begin
            right_rotate(right[t]);
            left_rotate(t);
        end else exit;
    end;
    maintain(left[t],false);
    maintain(right[t],true);
    maintain(t,true);
    maintain(t,false);
end;
     
     
procedure insert(var t:longint;v:longint);
begin
    if t=0 then
    begin
        inc(tot);
        t:=tot;
        left[t]:=0;
        right[t]:=0;
        size[t]:=1;
        key[t]:=v;
    end else
    begin
        inc(size[t]);
        if v<key[t] then insert(left[t],v) else insert(right[t],v);
        maintain(t,v>=key[t]);
    end;
end;
 
function find(var t:longint; v:longint):boolean;
begin
    if t=0 then exit(false);
    if key[t]=v then exit(true);
    if v>key[t] then exit(find(right[t],v)) else exit(find(left[t],v));
end;
 
procedure main;
var
    i                       :longint;
    x                       :longint;
begin
    tot:=0; t:=0;
    read(n);
    read(x);
    write(x);
    insert(t,x);
    for i:=2 to n do
    begin
        read(x);
        if not find(t,x) then
        begin
            write(' ',x); 
            insert(t,x);
        end;
    end;
    writeln;
end;
 
begin
    read(tt);
    for i:=1 to tt do main;
end.

 

posted on 2013-12-29 02:31  BLADEVIL  阅读(260)  评论(0编辑  收藏  举报