• 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.
  • 相关阅读:
    day06作业
    day04_ATM项目说明书
    ATM+购物车基本思路流程
    装饰器、迭代器、生成器、递归、匿名函数、面向过程编程、三元表达式6
    day05函数部分
    自制七段数码管源码
    字符串格式化
    字符串表示
    格式化输出
    python入门——列表类型、元组、字典类型
  • 原文地址:https://www.cnblogs.com/BLADEVIL/p/3496079.html
Copyright © 2020-2023  润新知