HNOI2008 and ZJOI2006 排名系统
1056: [HAOI2008]排名系统
Time Limit: 10 Sec Memory Limit: 162 MB
Submit: 1311 Solved: 337
[Submit][Status]
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
+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
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为什么跑得这么慢?????
先是WA,然后是TLE,后来有MLE。。。我已经彻底无语了。。。
是我写跪了,还是怎么的。。。不得已只好上lyd的代码了。。。
代码:
1.mine
{$inline on}
const maxn=+;inf=maxlongint;
var s,fa,v,next:array[..maxn] of longint;
ss:array[..maxn] of string[];
head:array[..] of longint;
c:array[..maxn,..] of longint;
i,j,n,x,y,rank,rt,tot,cnt:longint;
st,name:ansistring;
first:boolean;
ch:char;
function hash(s:ansistring):int64;inline;
var i,x:longint;
begin
x:=;
for i:= to length(s) do x:=(x*+ord(s[i])-ord('A')+) mod ;
i:=head[x];
while i<> do
begin
if ss[i]=s then exit(i);
i:=next[i];
end;
inc(tot);ss[tot]:=s;v[tot]:=y;next[tot]:=head[x];head[x]:=tot;
exit();
end;
procedure pushup(x:longint);inline;
begin
s[x]:=s[c[x,]]+s[c[x,]]+;
end;
procedure rotate(x:longint;var k:longint);inline;
var y,z,l,r:longint;
begin
y:=fa[x];z:=fa[y];
l:=ord(c[y,]=x);r:=l xor ;
if y=k then k:=x else c[z,ord(c[z,]=y)]:=x;
fa[x]:=z;fa[y]:=x;fa[c[x,r]]:=y;
c[y,l]:=c[x,r];c[x,r]:=y;
pushup(y);pushup(x);
end;
procedure splay(x:longint;var k:longint);inline;
var y,z:longint;
begin
while x<>k do
begin
y:=fa[x];z:=fa[y];
if y<>k then
if (c[z,]=y) xor (c[y,]=x) then rotate(x,k)
else rotate(y,k);
rotate(x,k);
end;
end;
function find(x,rank:longint):longint;inline;
var l,r:longint;
begin
l:=c[x,];r:=c[x,];
if s[l]+=rank then exit(x)
else if s[l]>=rank then exit(find(l,rank))
else exit(find(r,rank-s[l]-));
end;
procedure insert(var x:longint;f,k:longint);inline;
begin
if x= then
begin
fa[k]:=f;
x:=k;
s[k]:=;
c[k,]:=;c[k,]:=;
exit;
end;
inc(s[x]);
if v[k]>v[x] then insert(c[x,],x,k) else insert(c[x,],x,k);
end;
procedure del(rank:longint);inline;
var x,y,z:longint;
begin
x:=find(rt,rank-);y:=find(rt,rank+);
splay(x,rt);splay(y,c[x,]);
z:=c[y,];fa[z]:=;c[y,]:=;s[z]:=;
pushup(y);pushup(x);
end;
procedure print(x:longint);inline;
begin
if x= then exit;
print(c[x,]);
if first then begin first:=false;write(ss[x]);end else write(' ',ss[x]);
print(c[x,]);
end;
procedure main;
begin
readln(n);
rt:=n+;fa[n+]:=;v[n+]:=-inf;v[n+]:=inf;
insert(rt,,n+);
tot:=;
for j:= to n do
begin
read(ch);
case ch of
'+':begin
readln(st);
name:=copy(st,,pos(' ',st)-);
delete(st,,pos(' ',st));
val(st,y);
x:=hash(name);
if x= then insert(rt,,tot)
else
begin
splay(x,rt);del(s[c[rt,]]+);
v[x]:=y;
insert(rt,,x);
end;
end;
'?':begin
readln(st);
if (ord(st[])>ord('')) and (ord(st[])<=ord('')) then
begin
val(st,rank);
x:=find(rt,rank);
if rank+<=tot then y:=find(rt,rank+) else y:=find(rt,tot+);
splay(x,rt);splay(y,c[x,]);
first:=true;print(c[y,]);writeln;
end
else
begin
x:=hash(st);
splay(x,rt);
writeln(s[c[x,]]);
end;
end;
end;
end;
end;
begin
assign(input,'input.txt');assign(output,'output.txt');
reset(input);rewrite(output);
main;
close(input);close(output);
end.
2.lyd
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int u=,mod=;
struct splaytree{
int l,r,dat,size;
#define l(x) t[x].l
#define r(x) t[x].r
#define dat(x) t[x].dat
#define size(x) t[x].size
}t[u];
unsigned long long ver[u],temp;
int L[u],R[u],head[],sc[u],id[u],next[u];
char name[u][],str[];
int n,m,tot,root,i,j,x,y,sco,z,now,rec; int hash(int x,int y)
{
int i,z;
for(temp=,i=;i<strlen(str);i++) temp=temp*+str[i]-'A'+;
z=temp%mod;
for(i=head[z];i;i=next[i])
if(ver[i]==temp) return i;
ver[++m]=temp; sc[m]=x; id[m]=y;
next[m]=head[z]; head[z]=m;
for(i=;i<strlen(str);i++) name[m][i-]=str[i]; name[m][i-]='\0';
return m;
} inline void update(int x)
{
size(x)=size(l(x))+size(r(x))+;
} inline void zig(int &x)
{
int y=l(x); l(x)=r(y); r(y)=x;
update(x),update(y),x=y;
} inline void zag(int &x)
{
int y=r(x); r(x)=l(y); l(y)=x;
update(x),update(y),x=y;
} inline int cmp(int x,int p)
{
int y=dat(p);
if(sc[x]==sc[y]&&id[x]==id[y]) return ;
if(sc[x]>sc[y]||sc[x]==sc[y]&&id[x]<id[y]) return -;
return ;
} inline void splay(int &x,int y)
{
int i; L[]=R[]=;
while()
{
i=cmp(y,x);
if(!i||!l(x)&&i<||!r(x)&&i>) break;
if(i<)
{
if(cmp(y,l(x))<) {zig(x); if(!l(x)) break;}
R[++R[]]=x; x=l(x);
}
else{
if(cmp(y,r(x))>) {zag(x); if(!r(x)) break;}
L[++L[]]=x; x=r(x);
}
}
L[L[]+]=l(x); R[R[]+]=r(x);
for(i=L[];i;i--) {r(L[i])=L[i+]; update(L[i]);}
for(i=R[];i;i--) {l(R[i])=R[i+]; update(R[i]);}
l(x)=L[]; r(x)=R[]; update(x);
} void insert(int x)
{
tot++; dat(tot)=x; size(tot)=;
if(!root) {root=tot; return;}
splay(root,x);
if(cmp(x,root)<) l(tot)=l(root),l(root)=tot;
else r(tot)=r(root),r(root)=tot;
update(tot),update(root);
} void clear(int x)
{
splay(root,x);
if(!l(root)) {root=r(root); return;}
splay(l(root),x);
r(l(root))=r(root); root=l(root);
update(root);
} void ask(int x)
{
splay(root,x);
printf("%d\n",size(l(root))+);
} void dfs(int x)
{
if(l(x)&&now) dfs(l(x));
if(now) {now--; printf(" %s",name[dat(x)]);}
if(r(x)&&now) dfs(r(x));
} void print(int rank)
{
int x;
for(x=root;;)
if(size(l(x))+==rank) break;
else if(rank<=size(l(x))) x=l(x);
else rank-=size(l(x))+,x=r(x);
printf("%s",name[dat(x)]);
splay(root,dat(x));
now=; if(r(x)) dfs(r(x));
puts("");
} int main()
{
cin>>n;
for(i=;i<=n;i++)
{
scanf("%s",str);
if(str[]=='+')
{
scanf("%d",&sco);
rec=m;
z=hash(sco,i);
if(rec==m) {clear(z); sc[z]=sco; id[z]=i;}
insert(z);
}
else if(str[]>=''&&str[]<='')
{
for(j=,x=;j<strlen(str);j++) x=x*+str[j]-'';
print(x);
}
else ask(hash(,));
}
return ;
}
HNOI2008 and ZJOI2006 排名系统的更多相关文章
- [BZOJ1056][BZOJ1862][HAOI2008][Zjoi2006]排名系统
[BZOJ1056][BZOJ1862][HAOI2008][Zjoi2006]排名系统 试题描述 排名系统通常要应付三种请求:上传一条新的得分记录.查询某个玩家的当前排名以及返回某个区段内的排名记录 ...
- BZOJ 1862: [Zjoi2006]GameZ游戏排名系统 [treap hash]
1862: [Zjoi2006]GameZ游戏排名系统 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1318 Solved: 498[Submit][ ...
- 【洛谷P2584】【ZJOI2006】GameZ游戏排名系统题解
[洛谷P2584][ZJOI2006]GameZ游戏排名系统题解 题目链接 题意: GameZ为他们最新推出的游戏开通了一个网站.世界各地的玩家都可以将自己的游戏得分上传到网站上.这样就可以看到自己在 ...
- 【BZOJ1862】[ZJOI2006]游戏排名系统 (Splay)
[BZOJ1862][ZJOI2006]游戏排名系统 (Splay) 题面 BZOJ 洛谷 题解 双倍经验题
- BZOJ_1862_[Zjoi2006]GameZ游戏排名系统&&BZOJ_1056_[HAOI2008]排名系统_Splay
BZOJ_1862_[Zjoi2006]GameZ游戏排名系统&&BZOJ_1056_[HAOI2008]排名系统_Splay Description 排名系统通常要应付三种请求:上传 ...
- 【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 1056 [HAOI2008]排名系统(1862 [Zjoi2006]GameZ游戏排名系统)
1056: [HAOI2008]排名系统 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1854 Solved: 502[Submit][Statu ...
- 1056/1862. [ZJOI2006]GameZ游戏排名系统【平衡树-splay】
Description GameZ为他们最新推出的游戏开通了一个网站.世界各地的玩家都可以将自己的游戏得分上传到网站上.这样就可以看到自己在世界上的排名.得分越高,排名就越靠前.当两个玩家的名次相同时 ...
- [HAOI2008]排名系统& [Zjoi2006]GameZ游戏排名系统
1056: [HAOI2008]排名系统 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2487 Solved: 711[Submit][Statu ...
随机推荐
- .net中压缩和解压缩的处理
最近在网上查了一下在.net中进行压缩和解压缩的方法,方法有很多,我找到了以下几种: 1.利用.net自带的压缩和解压缩方法GZip 参考代码如下: //======================= ...
- linux下源码安装软件
在linux下的很多软件都是通过源码包方式发布的,这样做对于最终用户而言,虽然相对于二进制软件包,配置和编译起来繁琐点,但是它的可移植性却好得多,针对不同的体系结构,软件开发者往往仅需发布同一份源码包 ...
- 如何在VC++ 中调试MEX文件
MEX文件对应的是将C/C++文件语言的编写之后 得到的相关文件加载到Matlab中运行的一种方式, 现对于Matlab 中的某些程序运行效率而言, C/C++ 代码某些算法的领域上面执行效率很高,若 ...
- STORM 免费且开源的WebSerivce测试工具
一.名称 STORM 是一款免费且开源的WebSerivce测试工具 二.使用方式 1.发布自己的webservice服务 例如:http://www.webxml.com.cn/WebService ...
- tar 命令基本使用(加密)
本文讲述tar命令的基本使用,special: 使用tar命令对文件加密. 假定在当前目录下有一个文件夹/stuff. 1.将/stuff目录下的所有文件打包成为.tar 文件. $ tar -cvf ...
- new Date() 倒计时
js中单独调用new Date() 显示的结果是:Fri May 20 2015 20:00:00 GMT+0800这种格式的时间 JS获取当前时间戳的方法 JavaScript 获取当前时间戳: 第 ...
- 在ARM Linux 使用 Valgrind
Linux valgrind 移植到ARM-Linux 一.Cross-Compile/交叉编译 (1)下载及解压Valgrind-3.11 (2)修改confirure 将armv7*)修改为ar ...
- gcc命令以及makefile文件
(一)makefile里涉及到的gcc命令 gcc -I./inc:指定头文件寻找目录 将按照 ./inc --> /usr/include --> /usr/local/include的 ...
- IOS 学习笔记 2015-03-24 OC-API-不可变字符串
大部分是模仿// // main.m // OC-API-不可变字符串 // // Created by wangtouwang on 15/3/25. // Copyright (c) 2015年 ...
- [翻译][MVC 5 + EF 6] 1:创建数据模型
原文:Getting Started with Entity Framework 6 Code First using MVC 5 1.新建MVC项目: 2.修改Views\Shared\_Layou ...