ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1028. Stars

why wa on #12
Posted by zhuojingwei 3 Mar 2008 18:38
my code:


program lt;
var n,i,x,head,num:longint;
    key,r,l,s,f:array[-1..15000] of longint;
procedure leftrotate(var t:longint);
var k:longint;
begin
  k:=r[t];
  r[t]:=l[k];
  l[k]:=t;
  s[k]:=s[t];
  s[t]:=s[l[t]]+s[r[t]]+1;
  t:=k;
end;
procedure rightrotate(var t:longint);
var k:longint;
begin
  k:=l[t];
  l[t]:=r[k];
  r[k]:=t;
  s[k]:=s[t];
  s[t]:=s[l[t]]+s[r[t]]+1;
  t:=k;
end;
procedure maintain(var t:longint;flag:boolean);
begin
  if flag then
    if s[r[r[t]]]>s[l[t]] then
    leftrotate(t)
    else
    if s[l[r[t]]]>s[l[t]] then
    begin
      rightrotate(r[t]);
      leftrotate(t);
    end
    else exit
  else
    if s[l[l[t]]]>s[r[t]] then
    rightrotate(t)
    else if s[r[l[t]]]>s[r[t]] then
    begin
      leftrotate(l[t]);
      rightrotate(t);
    end
    else exit;
  maintain(l[t],false);
  maintain(r[t],true);
  maintain(t,false);
  maintain(t,true);
end;
procedure insert(var t:longint);
begin
  if t<=0 then
  begin
    inc(num);
    key[num]:=x;
    t:=num;
    l[t]:=0;r[t]:=0;
    s[t]:=1;
  end
  else
  begin
    inc(s[t]);
    if x<=key[t] then insert(l[t])
    else insert(r[t]);
    maintain(t,x>key[t]);
  end;
end;
function rank(t,x:longint):longint;
begin
  if t=0 then rank:=1
  else if x=key[t] then rank:=s[l[t]]+1
  else if x<=key[t] then rank:=rank(l[t],x)
  else rank:=s[t]-s[r[t]]+rank(r[t],x);
end;
procedure init;
begin
  readln(n);
  head:=-1;
  for i:=1 to n do
  begin
    readln(x);
    insert(head);
    inc(f[rank(head,key[i])-1]);
  end;
end;
begin
  init;
  for i:=0 to n-1 do writeln(f[i]);
end.