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 排名系统通常要应付三种请求:上传 ...
随机推荐
- 在VS2010中使用附加进程的方式调试IIS中的页面
h3{background:#333333; } 准备篇-配置IIS环境 在发布网站之前,需要安装iis环境! 之后点击确定即可! 发布网站至IIS-附加到进程调试 1. 用VS2010将 ...
- bootstrap学习起步篇:初识bootstrap之html5语法构建hello篇(一)
目前选择使用bootstrap作为前端页面模板,是件很省心的事情.官网上给出的实例和教程也很多.在实际使用过程中,我们也许还要借助文档去了解它的元素和样式.但也不能减少我们使用他的兴趣. 我准备将其整 ...
- SQL数据库设计三范式
关系型数据库将数据库设计需要遵循的一些规则叫做“范式”,最基本的三个范式(1NF.2NF.3NF)简称三范式.第一范式是满足第二范式的基础,而第一.二范式又是满足第三范式的基础. 第一范式 表中的字段 ...
- 【Cocos2d入门教程一】Cocos2d-x环境搭建
在进行Cocos2d游戏开发前 我们先来配置一下环境,我们先来准备一下工具,我们所需要的工具分别为: 1.Cocos2d引擎 2.JDK 3.SDK 4.NDK 5.ANT 6.ADT 1.下载Coc ...
- Native App, Hybrid App, Web App对比
Native App,Hybrid App和Web App简介 目前基本所有的移动互联网app可以分为三类:Native App,Hybrid App和Web App. Native App是基于智能 ...
- Access和Sql区别
假设表game有一字段为gameYuiJian为bit字段(SQL SERVER 20005)和"是/否"字段(ACCSS数据库),在编写脚本文件时,如下才能正确执行 SQL st ...
- C# WinForm打开IE浏览器并访问网址
C# WinForm 打开浏览器并访问网址代码: System.Diagnostics.Process.Start("iexplore.exe", "http://kel ...
- 让 Putty 保存密码,自动登陆的四种方法
Putty 基本是我在紧急时候用来登陆 Linux/Unix 终端的不二之先,因其小,开源,界面也非常实用.可是当你要在私有的机器上,经常性的要登陆很多机器的时候就觉得烦琐了,不光打开一堆的窗口,还要 ...
- CLI-error
SQL_ERROR: One of the following occurred: RecNumber was negative or 0. BufferLength was less than ze ...
- 创建型模式——Builder
1.意图 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 2.结构 3.参与者 Builder为创建一个Product对象的各个部件指定抽象接口 ConcreteBuild ...