ural1028 using sbt...

http://hi.baidu.com/raulliubo/blog/item/db8b8a52731d030a0cf3e3a5.html

 

发现sbt退化版奇好无比。又快又短。OYE.

1949443 12:18:20
5 Feb 2008
raulliubo 1028 Pascal Accepted 0.031 575 KB

my ugly code:

program ural1028_sbt;
var
    level, key, size, lch, rch : array[0 .. 200000] of longint;
    x, y : array[0 .. 200000] of longint;
    n, i, temp, tot, now : longint;

procedure leftrotate(var now : longint);
begin
    temp := rch[now];
    rch[now] := lch[temp];
    lch[temp] := now;
    size[temp] := size[now];
    size[now] := size[lch[now]] + size[rch[now]] + 1;
    now := temp;
end;

procedure rightrotate(var now : longint);
begin
    temp := lch[now];
    lch[now] := rch[temp];
    rch[temp] := now;
    size[temp] := size[now];
    size[now] := size[lch[now]] + size[rch[now]] + 1;
    now := temp;
end;

procedure insert(var now, value : longint);
begin
    if now = 0 then begin
        inc(tot);
        now := tot;
        key[now] := value;
        size[now] := 1;
        lch[now] := 0;
        rch[now] := 0;
    end
    else begin
        inc(size[now]);
        if value <key[now] then begin
            insert(lch[now],value);
            if size[lch[lch[now]]] > size[rch[now]] then
                rightrotate(now);
        end
        else begin
            insert(rch[now], value);
            if size[rch[rch[now]]] > size[lch[now]] then
                leftrotate(now);
        end;
    end;
end;

function rank(var now, value : longint) : longint;
begin
    if now = 0 then exit(0);
    if value < key[now] then
        rank := rank(lch[now], value)
    else
        rank := size[lch[now]] + 1 + rank(rch[now], value);
end;

begin
    readln(n);
    for i := 1 to n do readln(x[i], y[i]);
    tot := 0;
    now := 0;
    for i := 1 to n do begin
        insert(now, x[i]);
        inc(level[rank(now, x[i])-1]);
    end;
    for i := 0 to n-1 do writeln(level[i]);
end.

posted @ 2009-01-04 12:28  jesonpeng  阅读(184)  评论(0编辑  收藏  举报