bzoj1056
花了一上午大概复习了一下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的更多相关文章
- [BZOJ1056][BZOJ1862][HAOI2008][Zjoi2006]排名系统
[BZOJ1056][BZOJ1862][HAOI2008][Zjoi2006]排名系统 试题描述 排名系统通常要应付三种请求:上传一条新的得分记录.查询某个玩家的当前排名以及返回某个区段内的排名记录 ...
- 【BZOJ1056】[HAOI2008]排名系统(Splay)
[BZOJ1056][HAOI2008]排名系统(Splay) 题面 BZOJ 洛谷 题解 \(Splay\)随便维护一下就好了,至于名字什么的,我懒得手写哈希表了,直接哈希之后拿\(map\)压. ...
- 替罪羊树模板(BZOJ1056/1862)
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #defin ...
- bzoj1056: [HAOI2008]排名系统 && 1862: [Zjoi2006]GameZ游戏排名系统
hash 加上 平衡树(名次树). 这道题麻烦的地方就在于输入的是一个名字,所以需要hash. 这个hash用的是向后探查避免冲突,如果用类似前向星的方式避免冲突,比较难写,容易挂掉,但也速度快些. ...
- [HAOI2008]排名系统 & [Zjoi2006]GameZ游戏排名系统 BZOJ1862&BZOJ1056
分析: 平衡树裸题,(学完LCT感觉自己不会普通的Splay了...),维护每个节点的权值大小顺序,和时间戳顺序,之后map维护一下是否存在过,(懒得写字符串hash了). 附上代码: #includ ...
- bzoj1056/1862 [Zjoi2006]GameZ游戏排名系统
题目链接:1,2 treap恶心题,不多说 #include<algorithm> #include<iostream> #include<cstdlib> #in ...
- 【bzoj1056】排名系统
1056: [HAOI2008]排名系统 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2195 Solved: 623[Submit][Statu ...
- [bzoj1056] [HAOI2008]排名系统
Description 排名系统通常要应付三种请求:上传一条新的得分记录.查询某个玩家的当前排名以及返回某个区段内的排名记录.当某个玩家上传自己最新的得分记录时,他原有的得分记录会被删除.为了减轻服务 ...
- 【pb_ds】bzoj1056 [HAOI2008]排名系统/bzoj1862 [Zjoi2006]GameZ游戏排名系统
STL裸题,线下AC,bzoj无限RE ing…… #include<cstdio> #include<cctype> #include<iostream> #in ...
随机推荐
- 02线性表链式存储_LinkList--(线性表)
#include "stdio.h" #include "string.h" #include "ctype.h" #include &qu ...
- JavaScript中的apply与call与arguments对象
(一) call方法 语法:presentObj.call(thisObj,arg1,arg2,arg3...) 参数thisObj :将被用作当前对象presentObj的对象. 当thisObj无 ...
- C++对象模型与内存位对齐的简单分析(GNU GCC&VS2015编译器)
以Fruit和Apple为例进行分析: Fruit和Apple的定义如下: 通过在两种编译环境下的测试(GNU GCC & VS2015),可以发现这两种编译器的对象模型是一样的,如下图所示: ...
- bzoj2287:[POJ Challenge]消失之物
思路:首先先背包预处理出f[x]表示所有物品背出体积为x的方案数.然后统计答案,利用dp. C[i][j]表示不用物品i,组成体积j的方案数. 转移公式:C[i][j]=f[j]-C[i][j-w[i ...
- linux下查看端口的连接数
linux下,可以通过natstat命令来查看端口的连接状况,比如连接数 例如,查看9090端口的连接状况: 查看某个端口的连接数netstat -nat | grep -iw "9090& ...
- 建站服务器的最优选择之Windows Or Linux
转载于:http://www.0553114.com/news/detail-702287.html 不管是个人建站,还是中小型企业建站,选择一款合适的主机是站长朋友们共同的心愿.主机是选择Windo ...
- 今日吐槽20151208.net服务器控件
正文 今天有个小任务是给页面添加一个搜索条件,复选框多选.因为页面都是服务器控件,我也只能用服务器控件配合了.然后给页面加了一个 CheckBoxList 控件.后台通过数据表加载数据. fore ...
- c语言知识(找出大于2门成绩不及格的学生)
1.首先定义一个学生结构体(结构体中包含一个Score结构体): typedef struct score{ float chinese;//语文成绩 float english;//英语成绩 flo ...
- linux shell命令的常用快捷键
一些shell的常用快捷键. Ctrl + a 切换到命令行开始 Ctrl + e 切换到命令行末尾 Ctrl + l 清除屏幕内容 Ctrl + u 清除剪切光标之前的内容 Ctrl + ...
- oracle,wamp,FZ突然出现问题,求解决方案(未解决,最终系统还原)
-----背景------- 系统:win7 64位oracle 11g(11.1)每天都用oracle.用toad for oracle .电脑固定IP.未更改任何配置信息.用了几个月,突然出现了 ...