1056: [HAOI2008]排名系统 - BZOJ
Description
排名系统通常要应付三种请求:上传一条新的得分记录、查询某个玩家的当前排名以及返回某个区段内的排名记录。当某个玩家上传自己最新的得分记录时,他原有的得分记录会被删除。为了减轻服务器负担,在返回某个区段内的排名记录时,最多返回10条记录。
Input
第一行是一个整数n(n>=10)表示请求总数目。接下来n行,每行包含了一个请求。请求的具体格式如下: +Name Score 上传最新得分记录。Name表示玩家名字,由大写英文字母组成,不超过10个字符。Score为最多8位的正整数。 ?Name 查询玩家排名。该玩家的得分记录必定已经在前面上传。 ?Index 返回自第Index名开始的最多10名玩家名字。Index必定合法,即不小于1,也不大于当前有记录的玩家总数。
Output
对于?Name格式的请求,应输出一个整数表示该玩家当前的排名。对于?Index格式的请求,应在一行中依次输出从第Index名开始的最多10名玩家姓名,用一个空格分隔。
Sample Input
20
+ADAM 1000000 加入ADAM的得分记录
+BOB 1000000 加入BOB的得分记录
+TOM 2000000 加入TOM的得分记录
+CATHY 10000000 加入CATHY的得分记录
?TOM 输出TOM目前排名
?1 目前有记录的玩家总数为4,因此应输出第1名到第4名。
+DAM 100000 加入DAM的得分记录
+BOB 1200000 更新BOB的得分记录
+ADAM 900000 更新ADAM的得分记录(即使比原来的差)
+FRANK 12340000 加入FRANK的得分记录
+LEO 9000000 加入LEO的得分记录
+KAINE 9000000 加入KAINE的得分记录
+GRACE 8000000 加入GRACE的得分记录
+WALT 9000000 加入WALT的得分记录
+SANDY 8000000 加入SANDY的得分记录
+MICK 9000000 加入MICK的得分记录
+JACK 7320000 加入JACK的得分记录
?2 目前有记录的玩家总数为12,因此应输出第2名到第11名。
?5 输出第5名到第13名。
?KAINE 输出KAINE的排名
Sample Output
2
CATHY TOM ADAM BOB
CATHY LEO KAINE WALT MICK GRACE SANDY JACK TOM BOB
WALT MICK GRACE SANDY JACK TOM BOB ADAM DAM
4
HINT
20%数据满足N<=100 100%数据满足N<=250000
唉,splay真的不是很熟练啊
又调了一上午,不过还是有一点收获的,每次发现超时都是因为找答案的方法不够好(当你要l到r的答案时,1.把l到r全部旋到一起 2.把l旋到根然后往右边遍历)
const
maxn=;
type
node=record
son:array[..]of longint;
size,fa,f,t:longint;
str:string;
end;
var
tree:array[..maxn*]of node;
hash,next:array[..maxn*]of longint;
n,tot,time,root,k,num:longint; function hashx(s:string):longint;
var
i:longint;
begin
hashx:=;
for i:= to length(s) do
hashx:=(hashx*+ord(s[i])-ord('A'))mod maxn;
inc(hashx);
end; function get(s:string):longint;
begin
get:=hashx(s);
while (tree[get].str<>s) and (get<>) do
get:=next[get];
end; procedure rotate(x,w:longint);
var
y:longint;
begin
y:=tree[x].fa;
tree[y].son[w]:=tree[x].son[w xor ];
if tree[x].son[w xor ]<> then tree[tree[x].son[w xor ]].fa:=y;
tree[x].son[w xor ]:=y;
if root=y then root:=x
else
if tree[tree[y].fa].son[]=y then tree[tree[y].fa].son[]:=x
else tree[tree[y].fa].son[]:=x;
tree[x].fa:=tree[y].fa;
tree[y].fa:=x;
with tree[y] do
size:=tree[son[]].size++tree[son[]].size;
end; procedure splay(x,z:longint);
var
y:longint;
begin
while tree[x].fa<>z do
begin
y:=tree[x].fa;
if tree[y].fa=z then
if tree[y].son[]=x then rotate(x,)
else rotate(x,)
else
if tree[tree[y].fa].son[]=y then
if tree[y].son[]=x then
begin
rotate(y,);
rotate(x,);
end
else
begin
rotate(x,);
rotate(x,);
end
else
if tree[y].son[]=x then
begin
rotate(x,);
rotate(x,);
end
else
begin
rotate(y,);
rotate(x,);
end;
end;
with tree[x] do
size:=tree[son[]].size++tree[son[]].size;
end; procedure delete(x:longint);
var
l,r:longint;
begin
splay(x,);
l:=tree[x].son[];
r:=tree[x].son[];
if (l=) or (r=) then
begin
root:=l+r;
tree[l+r].fa:=;
tree[x].son[]:=;
tree[x].son[]:=;
end
else
begin
while tree[l].son[]<> do
l:=tree[l].son[];
while tree[r].son[]<> do
r:=tree[r].son[];
splay(l,);
splay(r,l);
tree[r].son[]:=;
dec(tree[r].size);
dec(tree[l].size);
tree[x].fa:=;
end;
end; procedure add(x:longint);
var
now:longint;
begin
if root= then
begin
root:=x;
tree[x].fa:=;
tree[x].size:=;
end
else
begin
now:=root;
while true do
begin
inc(tree[now].size);
if (tree[x].f>tree[now].f) or ((tree[x].f=tree[now].f) and (tree[x].t<tree[now].t)) then
if tree[now].son[]= then break
else now:=tree[now].son[]
else
if tree[now].son[]= then break
else now:=tree[now].son[];
end;
if (tree[x].f>tree[now].f) or ((tree[x].f=tree[now].f) and (tree[x].t<tree[now].t)) then tree[now].son[]:=x
else tree[now].son[]:=x;
tree[x].size:=;
tree[x].fa:=now;
end;
splay(x,);
end; procedure insert(s:string;x:longint);
var
z:longint;
begin
z:=get(s);
if z> then
begin
delete(z);
tree[z].t:=time;
tree[z].f:=x;
add(z);
end
else
begin
z:=hashx(s);
inc(num);
if tree[z].str='' then
begin
tree[z].str:=s;
tree[z].t:=time;
tree[z].f:=x;
add(z);
end
else
begin
while next[z]<> do
z:=next[z];
inc(tot);
next[z]:=tot;
tree[tot].str:=s;
tree[tot].t:=time;
tree[tot].f:=x;
add(tot);
end;
end;
end; function find(x:longint):longint;
var
now:longint;
begin
now:=root;
while true do
begin
if x<=tree[tree[now].son[]].size then now:=tree[now].son[]
else
if x=tree[tree[now].son[]].size+ then exit(now)
else
begin
dec(x,tree[tree[now].son[]].size+);
now:=tree[now].son[];
end;
end;
end; procedure dfs(now:longint);
begin
if k= then exit;
if tree[now].son[]<> then dfs(tree[now].son[]);
if k= then exit;
inc(k);
write(' ',tree[now].str);
if k= then exit;
if tree[now].son[]<> then dfs(tree[now].son[]);
end; function min(x,y:longint):longint;
begin
if x<y then exit(x);
exit(y);
end; procedure main;
var
x:longint;
c:char;
s:string;
begin
readln(n);
tot:=maxn;
for time:= to n do
begin
read(c);
if c='+' then
begin
read(c);
s:='';
while c<>' ' do
begin
s:=s+c;
read(c);
end;
readln(x);
insert(s,x);
end
else
if c='?' then
begin
readln(s);
if (ord(s[])>=ord('A')) and (ord(s[])<=ord('Z')) then
begin
splay(get(s),);
writeln(tree[tree[root].son[]].size+);
end
else
begin
val(s,x);
splay(find(x),);
write(tree[root].str);
if num>x then splay(find(min(num,x+)),root);
k:=;
if tree[root].son[]<> then dfs(tree[root].son[]);
writeln;
end;
end;
end;
end; begin
main;
end.
1056: [HAOI2008]排名系统 - BZOJ的更多相关文章
- bzoj 1056 [HAOI2008]排名系统(1862 [Zjoi2006]GameZ游戏排名系统)
1056: [HAOI2008]排名系统 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1854 Solved: 502[Submit][Statu ...
- 【BZOJ】1862: [Zjoi2006]GameZ游戏排名系统 & 1056: [HAOI2008]排名系统(treap+非常小心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1862 http://www.lydsy.com/JudgeOnline/problem.php?id ...
- bzoj 1862/1056 [HAOI2008]排名系统
原题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1862 很恶心的 一道题,我也不晓得自己是第几次写这题了%>_<%. 写了两种方 ...
- [BZOJ 1056][HAOI2008]排名系统
传送门 \(\color{green}{solution}\) \(fhq \_treap\)模板题. 对于 \(+\) 操作,如果当前人不存在,那么直接加入;如果存在,那么先将他删除,再加入.复杂度 ...
- bzoj 1862: [Zjoi2006]GameZ游戏排名系统 & bzoj 1056: [HAOI2008]排名系统
傻叉了一晚上,把t打成x,然后这题神奇在于输出一段数,不足的不用输出,一开始我的是直接找没有后面就退,然后这样会格式错误囧……然后最后zj的还卡了下空间,于是不用string就过了……string毁一 ...
- [HAOI2008]排名系统& [Zjoi2006]GameZ游戏排名系统
1056: [HAOI2008]排名系统 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2487 Solved: 711[Submit][Statu ...
- 【BZOJ1056】[HAOI2008]排名系统(Splay)
[BZOJ1056][HAOI2008]排名系统(Splay) 题面 BZOJ 洛谷 题解 \(Splay\)随便维护一下就好了,至于名字什么的,我懒得手写哈希表了,直接哈希之后拿\(map\)压. ...
- 数据结构(Splay平衡树):HAOI2008 排名系统
[HAOI2008] 排名系统 [题目描述] 排名系统通常要应付三种请求:上传一条新的得分记录.查询某个玩家的当前排名以及返回某个区段内的排名记录.当某个玩家上传自己最新的得分记录时,他原有的得分记录 ...
- BZOJ_1862_[Zjoi2006]GameZ游戏排名系统&&BZOJ_1056_[HAOI2008]排名系统_Splay
BZOJ_1862_[Zjoi2006]GameZ游戏排名系统&&BZOJ_1056_[HAOI2008]排名系统_Splay Description 排名系统通常要应付三种请求:上传 ...
随机推荐
- Android之按钮
Button 表示一个按钮.用户点击后会作出响应.具体的响应行为需要我们来定义(一 般通过监听器来处理). Button 是 TextView 的子类,因此,原则上,TextView 的属性设置均 ...
- SQL Server内存数据写入磁盘方法比较
众所周知,SQLServer增删改数据最先都是在内存中进行的,这可以大大加快数据操作的速度: 当内存中的数据被修改了,而磁盘中的数据还没有被修改时,就产生了所谓的“脏页”,SQLServer是如何同步 ...
- sql server 2008 查询语句的红色波浪线
在 Microsoft sql server management studio 里点击“编辑”——“IntelliSense”——“刷新本地缓存” 就会发现红色波浪线没了(前提是你的代码没错)
- Quartz 第三课 More About Jobs & JobDetails(官方文档翻译)
当学完第二课之后,你欣喜的发现,让jobs工作起来是还是相当简单的.虽然让jobs运行起来很简单,对于其执行的关键内容还是需要知道的.它们是IJob接口中的Execute和JobDetails. 当你 ...
- CSS选择器介绍
一.元素选择器 E{...} 二.属性选择器 E[attr]{...}:指定该CSS对具有attr的元素起作用: E[attr=value]{...}::指定该CSS对具有attr的值为value的元 ...
- 【ios控件】UIScrollView 事件说明
// // UIDemoViewController.m // 06-1UIScrollDemo // // Created by k on 14-9-4. // Copyright (c) 2014 ...
- Mysql 的安装与配置
MySQL的安装 第1步:下载 第2 步:以管理员身份进行安装 第3步:选择安装类型. 第4步:设置MySQL安装目录,及数据库文件目录 第5步:安装结束,开启配置向导 第6步:选择配置类型 第7步: ...
- 服务器发布WebService返回DataTable
初始化Datatable时,需要为Datatable命名.否则在客户端使用时,会报“datatable不能序列化...”导致表格无法从服务器端读取到. 例如: 服务器端: DataTable dt = ...
- Poj 2109 / OpenJudge 2109 Power of Cryptography
1.Link: http://poj.org/problem?id=2109 http://bailian.openjudge.cn/practice/2109/ 2.Content: Power o ...
- PHP 魔术方法(所有的魔术方法)
慢慢长寻夜,明月高空挂. 目前PHP所有的魔术方法有一下这些 __construct() __destruct() __call() __callStatic() __get() __set() __ ...