花了一上午大概复习了一下splay,treap

像这种裸的数据结构题在js应该会越来越少

不过练练手也好,

这就是平衡树+hash,如果这是单纯的BST应用,还是写treap吧,好调试

 const rd=;
      ran=; type link=^node;
     node=record
       st:string[];
       loc:longint;
       next:link;
     end; var hash:array[..rd] of link;
    son:array[..,..] of longint;
    score,fa,count,key:array[..] of longint;
    na:array[..] of string[];
    t,tot,root,x,y,i,n:longint;
    what,ch:char;
    s,ss:string; procedure update(x:longint);
  begin
    count[x]:=count[son[x,]]+count[son[x,]]+;
  end; procedure clear(x:longint);
  begin
    count[x]:=;
    fa[x]:=;
    score[x]:=;
    key[x]:=;
    son[x,]:=;
    son[x,]:=;
    fa[]:=;
    count[]:=;
  end; function tran(x:string):longint;
  var l,i:longint;
  begin
    l:=length(x);
    tran:=;
    for i:= to l do
      tran:=tran*+ord(x[i])-;
  end; function get(x:string):longint;
  var l,i,s:longint;
      p:link;
  begin
    l:=length(x);
    s:=;
    for i:= to l do
      s:=(s+(sqr(ord(x[i]))*i mod rd)) mod rd;
    p:=hash[s];
    while p<>nil do
    begin
      if p^.st=x then
      begin
        get:=p^.loc;
        break;
      end;
      p:=p^.next;
    end;
    if p=nil then
    begin
      get:=-;
      new(p);
      inc(t);
      p^.loc:=t;
      p^.st:=x;
      p^.next:=hash[s];
      hash[s]:=p;
    end
    else if what='+' then
    begin
      inc(t);
      p^.loc:=t;
    end;
  end; procedure rotate(x,w:longint);
  var y:longint;
  begin
    y:=fa[x];
    if fa[y]= then root:=x
    else begin
      if son[fa[y],]=y then son[fa[y],]:=x
      else son[fa[y],]:=x;
    end;
    fa[x]:=fa[y];
    son[y,-w]:=son[x,w];
    fa[son[x,w]]:=y;
    son[x,w]:=y;
    fa[y]:=x;
    update(y);
    update(x);
  end; procedure up(x:longint);
  var y:longint;
  begin
    y:=fa[x];
    while y<> do
    begin
      if key[y]>key[x] then
      begin
        if son[y,]=x then rotate(x,)
        else rotate(x,);
      end
      else break;
      y:=fa[x];
    end;
    if y= then root:=x;
  end; procedure sift(x:longint);
  var j1,j2:longint;
  begin
    repeat
      j1:=son[x,];
      j2:=son[x,];
      if (j1=) and (j2=) then exit;
      if j1= then rotate(j2,)
      else if j2= then rotate(j1,)
      else begin
        if (key[j1]>key[j2]) then rotate(j2,)
        else rotate(j1,);
      end;
    until false;
  end; function rank(x:longint):longint;
  var y,p:longint;
  begin
    y:=fa[x];
    p:=x;
    rank:=count[son[x,]];
    while y<> do
    begin
      if son[y,]=p then rank:=rank+count[son[y,]]+;
      p:=y;
      y:=fa[p];
    end;
    rank:=rank+;
  end; function kth(x:longint):longint;
  var p:longint;
  begin
    p:=root;
    while true do
    begin
      if count[son[p,]]+=x then exit(p);
      if count[son[p,]]+>x then p:=son[p,]
      else begin
        x:=x-count[son[p,]]-;
        p:=son[p,];
      end;
    end;
  end; procedure insert(s:string;x:longint);
  var p:longint;
  begin
    clear(t);
    score[t]:=x;
    key[t]:=trunc(random(ran));
    na[t]:=s;
    if root= then
    begin
      root:=t;
      fa[t]:=;
    end
    else begin
      p:=root;
      repeat
        inc(count[p]);
        if x>score[p] then
        begin
          if son[p,]= then break;
          p:=son[p,];
        end
        else begin   //注意后插入的相等分数应放在左子树
          if son[p,]= then break; 
          p:=son[p,];
        end;
      until false;
      if score[p]>=x then son[p,]:=t else son[p,]:=t;  
      fa[t]:=p;
      up(t);
    end;
  end; procedure delete(x:longint);
  var y:longint;
  begin
    sift(x);
    y:=fa[x];
    while y<> do
    begin
      dec(count[y]);
      y:=fa[y];
    end;
    y:=fa[x];
    if son[y,]=x then son[y,]:= else son[y,]:=;
    clear(x);
  end; procedure ask(x:longint);
  var i,y:longint;
  begin
    if x+>tot then y:=tot else y:=x+;
    for i:=x to y do
    begin
      write(na[kth(i)]);
      if i<>y then write(' ');
    end;
    writeln;
  end; begin
  randomize;
  readln(n);
  t:=;
  for i:= to n do
  begin
    read(what);
    if what='?' then
    begin
      readln(s);
      if (s[]>='') and (s[]<='') then
      begin
        x:=tran(s);
        ask(x);
      end
      else begin
        x:=get(s);
        writeln(rank(x));
      end;
    end
    else begin
      s:='';
      read(ch);
      while ch<>' ' do
      begin
        s:=s+ch;
        read(ch);
      end;
      x:=;
      readln(ss);
      x:=tran(ss);
      y:=get(s);
      if y=- then
      begin
        inc(tot);
        insert(s,x);
      end
      else begin
        insert(s,x);
        delete(y);
      end;
    end;
  end;
end.

bzoj1056的更多相关文章

  1. [BZOJ1056][BZOJ1862][HAOI2008][Zjoi2006]排名系统

    [BZOJ1056][BZOJ1862][HAOI2008][Zjoi2006]排名系统 试题描述 排名系统通常要应付三种请求:上传一条新的得分记录.查询某个玩家的当前排名以及返回某个区段内的排名记录 ...

  2. 【BZOJ1056】[HAOI2008]排名系统(Splay)

    [BZOJ1056][HAOI2008]排名系统(Splay) 题面 BZOJ 洛谷 题解 \(Splay\)随便维护一下就好了,至于名字什么的,我懒得手写哈希表了,直接哈希之后拿\(map\)压. ...

  3. 替罪羊树模板(BZOJ1056/1862)

    #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #defin ...

  4. bzoj1056: [HAOI2008]排名系统 && 1862: [Zjoi2006]GameZ游戏排名系统

    hash 加上 平衡树(名次树). 这道题麻烦的地方就在于输入的是一个名字,所以需要hash. 这个hash用的是向后探查避免冲突,如果用类似前向星的方式避免冲突,比较难写,容易挂掉,但也速度快些. ...

  5. [HAOI2008]排名系统 & [Zjoi2006]GameZ游戏排名系统 BZOJ1862&BZOJ1056

    分析: 平衡树裸题,(学完LCT感觉自己不会普通的Splay了...),维护每个节点的权值大小顺序,和时间戳顺序,之后map维护一下是否存在过,(懒得写字符串hash了). 附上代码: #includ ...

  6. bzoj1056/1862 [Zjoi2006]GameZ游戏排名系统

    题目链接:1,2 treap恶心题,不多说 #include<algorithm> #include<iostream> #include<cstdlib> #in ...

  7. 【bzoj1056】排名系统

    1056: [HAOI2008]排名系统 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2195  Solved: 623[Submit][Statu ...

  8. [bzoj1056] [HAOI2008]排名系统

    Description 排名系统通常要应付三种请求:上传一条新的得分记录.查询某个玩家的当前排名以及返回某个区段内的排名记录.当某个玩家上传自己最新的得分记录时,他原有的得分记录会被删除.为了减轻服务 ...

  9. 【pb_ds】bzoj1056 [HAOI2008]排名系统/bzoj1862 [Zjoi2006]GameZ游戏排名系统

    STL裸题,线下AC,bzoj无限RE ing…… #include<cstdio> #include<cctype> #include<iostream> #in ...

随机推荐

  1. XML, XPath, Xslt及解析/Parse

    XML及解析/Parse "Programming with libxml2 is like the thrilling embrace of an exotic stranger.&quo ...

  2. How to: Change icon in Inno Setup

    1. Change the installer executable icon or the icon showed in Start Menu folder Using SetupIconFile ...

  3. POJ 1170 Shopping Offers -- 动态规划(虐心的六重循环啊!!!)

    题目地址:http://poj.org/problem?id=1170 Description In a shop each kind of product has a price. For exam ...

  4. S.O.L.I.D

    S.O.L.I.D.是一组面对面向对象设计的最佳实践的设计原则.术语来自Robert C.Martin的著作Agile Principles, Patterns, and Practices in C ...

  5. linux中的sticky bit

    今天看到有个目录的权限是rwxrwxrwt 很惊讶这个t是什么,怎么不是x或者-呢?搜了下发现: 这个t代表是所谓的sticky bit. sticky bit: 该位可以理解为防删除位. 一个文件是 ...

  6. Yii 通过widget小物件生成添加表单

    通过widget小物件创建添加商品的表单 视图里,表单以endWidget();?>结束 最终效果: 把表单提交过来的信息保存到数据库中去. 补充要点: 密码表单: <?php echo ...

  7. Centos7 设置IPtables

    entOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop firewalld.service #停止fire ...

  8. jquery-easyui使用

    1,闲来无事用了一下jquery-easyui,首先下载下来easyui,文件夹如图 2,将themes里面的东西整体拷贝,我是整体的,省的麻烦了 3,在在自己的vs里面新建一个mvc的项目(项目自己 ...

  9. 图论(四)------非负权有向图的单源最短路径问题,Dijkstra算法

    Dijkstra算法解决了有向图G=(V,E)上带权的单源最短路径问题,但要求所有边的权值非负. Dijkstra算法是贪婪算法的一个很好的例子.设置一顶点集合S,从源点s到集合中的顶点的最终最短路径 ...

  10. WPF中Application.Current的使用

    WPF程序对应一个Application对象,当前的Application对象可以通过Application.Current获取,通过获取到的Application对象,我们可以做以下的事情: App ...