花了一上午大概复习了一下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. 常用终端及git命令

    终端常用命令 1,打开终端,git version 查看版本 2,pwd 打印工作目录 3,ls(list简写)查看当前目录的所有文件 4,clear 清掉屏幕 5,cd (change direct ...

  2. Java 7 新的 try-with-resources 语句,自动资源释放

    Java 7 的编译器和运行环境支持新的 try-with-resources 语句,称为 ARM 块(Automatic Resource Management) ,自动资源管理. 新的语句支持包括 ...

  3. Mac或Linux中对Android抓包

    转载说明 本篇文章可能已经更新,最新文章请转:http://www.sollyu.com/mac-or-linux-android-caught/ 说明 首先要到http://www.charlesp ...

  4. OpenJudge 2979 陪审团的人选 / Poj 1015 Jury Compromise

    1.链接地址: http://bailian.openjudge.cn/practice/2979 http://poj.org/problem?id=1015 2.题目: 总Time Limit: ...

  5. OpenJudge 2680 化验诊断 C++

    链接地址:http://bailian.openjudge.cn/practice/2680 题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 下表是进行血常规检验的正常值参考范围 ...

  6. linux 终端快捷键

    1. 移动光标快捷键 ctrl+f 向前移动一个字符 ctrl+b 向后移动一个字符 alt+f 向前移动一个单词 alt+b 向后移动一个单词 ctrl+a 移动到当前行首 ctrl+e 移动到当前 ...

  7. python计算两个日期时间差

    经常在python中因为日期而google,在此做个小笔记. >>>import datetime >>> a = datetime.date.today() &g ...

  8. lsof作用

    lsof 卸载移动存储时经常提示device busy,也可能误删了一个正在打开的文件....  这时候可以试试lsof  lsof简介 lsof(list open files)是一个列出当前系统打 ...

  9. WF4的数据库 表

    WF4的数据库 表 SQL 建表 SqlPersistenceProviderSchema.sql InstanceData 实例数据表 SqlPersistenceService_Schema.sq ...

  10. git 使用事项

    基本安装可查看 http://help.github.com 如果删除了本地的文件,要恢复相关文件,在github存在(别人增加的),则:git pull <远程主机名> <远程分支 ...