码迷,mamicode.com
首页 > 其他好文 > 详细

bzoj1588 [HNOI2002]营业额统计 (平衡树)

时间:2015-02-24 18:37:18      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

平衡树裸题

只需要求前驱后驱

 

treap写法

技术分享
const
  mm=1<<30;
  maxnumber=500000;
  maxn=500000;

var
  left,right,fix,key:array[0..maxn]of longint;
  t,n,ans,i,j,k,l,tot:longint;

function min(x,y:longint):longint;
begin
  if x<y then exit(x);
  exit(y);
end;

function max(x,y:longint):longint;
begin
  if x<y then exit(y);
  exit(x);
end;

procedure lt(var t:longint);
var
  k:longint;
begin
  k:=right[t];
  right[t]:=left[k];
  left[k]:=t;
  t:=k;
end;

procedure rt(var t:longint);
var
  k:longint;
begin
  k:=left[t];
  left[t]:=right[k];
  right[k]:=t;
  t:=k;
end;

procedure insert(var t:longint;v:longint);
begin
  if t=0 then begin
    inc(tot);
    t:=tot;
    key[t]:=v;
    fix[t]:=random(maxnumber)+1;
    left[t]:=0;
    right[t]:=0;
    exit;
  end;
  if v<=key[t] then begin
    insert(left[t],v);
    if fix[left[t]]>fix[t] then rt(t);
  end
  else begin
    insert(right[t],v);
    if fix[right[t]]>fix[t] then lt(t);
  end;
end;

function pred(t,v:longint):longint;
begin
  if t=0 then exit(-mm);
  if v=key[t] then exit(v);
  if v<key[t] then exit(pred(left[t],v))
  else
    exit(max(key[t],pred(right[t],v)));
end;

function succ(t,v:longint):longint;
begin
  if t=0 then exit(mm);
  if v=key[t] then exit(v);
  if v<key[t] then exit(min(key[t],succ(left[t],v)))
  else
    exit(succ(right[t],v));
end;

begin
  t:=0;
  readln(n);
  read(j);
  insert(t,j);
  ans:=j;
  for i:=2 to n do begin
    read(j);
    k:=pred(t,j);
    l:=succ(t,j);
   // writeln(j, ,k, ,l);
    if j-k<l-j then ans:=ans+j-k
      else ans:=ans+l-j;
    insert(t,j);
  end;
  writeln(ans);
  readln;
  readln;
end.
View Code

 

bzoj1588 [HNOI2002]营业额统计 (平衡树)

标签:

原文地址:http://www.cnblogs.com/Macaulish/p/4298817.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!