zoukankan      html  css  js  c++  java
  • HNOI2002营业额统计(平衡树)

    标准的平衡树。

    贴个splay吧

    var
     v,l,r,fa:array[0..100000] of longint;
     root,x,i,n,ans:longint;
    procedure zig(x:longint);
     var y,z:longint;
     begin
     y:=fa[x];z:=fa[y];
     if root=y then root:=x;
     l[y]:=r[x];
     if r[x]<>0 then fa[r[x]]:=y;
     r[x]:=y;
     fa[y]:=x;
     fa[x]:=z;
     if z=0 then exit;
     if l[z]=y then l[z]:=x else r[z]:=x;
     end;
    procedure zag(x:longint);
     var y,z:longint;
     begin
     y:=fa[x];z:=fa[y];
     if root=y then root:=x;
     r[y]:=l[x];
     if l[x]<>0 then fa[l[x]]:=y;
     l[x]:=y;
     fa[y]:=x;
     fa[x]:=z;
     if z=0 then exit;
     if l[z]=y then l[z]:=x else r[z]:=x;
     end;
    procedure splay(x:longint);
     var y,z:longint;
     begin
     while x<>root do
      begin
      y:=fa[x];
      if y=root then
       begin
       if x=l[y] then zig(x) else zag(x);
       exit;
       end;
      z:=fa[y];
      if l[y]=x then
       if l[z]=y then
        begin
         zig(y);zig(x)
        end
       else
        begin
         zig(x);zag(x);
        end
      else
       if r[z]=y then
        begin
         zag(y);zag(x);
        end
       else
        begin
         zag(x);zig(x);
        end;
      end;
     end;
    procedure insert(x,k:longint);
     var i,j:longint;
     begin
     if root=0 then
      begin
      root:=k;
      v[k]:=x;l[k]:=0;r[k]:=0;fa[k]:=0;
      exit;
      end;
     i:=root;
     v[k]:=x;
     while true do
      begin
      if x<=v[i] then j:=l[i] else j:=r[i];
      if j=0 then
       begin
        if x<=v[i] then l[i]:=k else r[i]:=k;
        fa[k]:=i;
        break;
       end;
      i:=j;
      end;
     splay(k);
     end;
    function min(x,k:longint):longint;
     var i,j,tmp:longint;
     begin
     tmp:=maxlongint;
     i:=l[k];
     while i<>0 do
      begin
       if abs(v[i]-x)<tmp then begin tmp:=abs(v[i]-x);j:=i;end;
       i:=r[i];
      end;
     i:=r[k];
     while i<>0 do
      begin
       if abs(v[i]-x)<tmp then begin tmp:=abs(v[i]-x);j:=i;end;
       i:=l[i];
      end;
     splay(j);
     exit(tmp);
     end;
    begin
     read(n);
     ans:=0;
     for i:=1 to n do
      begin
       read(x);
       insert(x,i);
       if i<>1 then ans:=ans+min(x,i) else ans:=x;
      end;
     writeln(ans);
    end.                      
  • 相关阅读:
    流量数据iftop命令
    DNS A记录和CNAME记录
    centos6.5安装mysql
    Python列表插入字典(转)
    列表转字典
    python 二分法O(logn)
    centos 6.5搭建Samba
    反爬虫-----看这一篇就够了
    windows常用命令
    requests中文页面乱码解决方案【转】
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/3763107.html
Copyright © 2011-2022 走看看