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

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为什么跑得这么慢?????

先是WA,然后是TLE,后来有MLE。。。我已经彻底无语了。。。

是我写跪了,还是怎么的。。。不得已只好上lyd的代码了。。。

代码:

1.mine

  1. {$inline on}
  2. const maxn=+;inf=maxlongint;
  3. var s,fa,v,next:array[..maxn] of longint;
  4. ss:array[..maxn] of string[];
  5. head:array[..] of longint;
  6. c:array[..maxn,..] of longint;
  7. i,j,n,x,y,rank,rt,tot,cnt:longint;
  8. st,name:ansistring;
  9. first:boolean;
  10. ch:char;
  11. function hash(s:ansistring):int64;inline;
  12. var i,x:longint;
  13. begin
  14. x:=;
  15. for i:= to length(s) do x:=(x*+ord(s[i])-ord('A')+) mod ;
  16. i:=head[x];
  17. while i<> do
  18. begin
  19. if ss[i]=s then exit(i);
  20. i:=next[i];
  21. end;
  22. inc(tot);ss[tot]:=s;v[tot]:=y;next[tot]:=head[x];head[x]:=tot;
  23. exit();
  24. end;
  25. procedure pushup(x:longint);inline;
  26. begin
  27. s[x]:=s[c[x,]]+s[c[x,]]+;
  28. end;
  29.  
  30. procedure rotate(x:longint;var k:longint);inline;
  31. var y,z,l,r:longint;
  32. begin
  33. y:=fa[x];z:=fa[y];
  34. l:=ord(c[y,]=x);r:=l xor ;
  35. if y=k then k:=x else c[z,ord(c[z,]=y)]:=x;
  36. fa[x]:=z;fa[y]:=x;fa[c[x,r]]:=y;
  37. c[y,l]:=c[x,r];c[x,r]:=y;
  38. pushup(y);pushup(x);
  39. end;
  40. procedure splay(x:longint;var k:longint);inline;
  41. var y,z:longint;
  42. begin
  43. while x<>k do
  44. begin
  45. y:=fa[x];z:=fa[y];
  46. if y<>k then
  47. if (c[z,]=y) xor (c[y,]=x) then rotate(x,k)
  48. else rotate(y,k);
  49. rotate(x,k);
  50. end;
  51. end;
  52. function find(x,rank:longint):longint;inline;
  53. var l,r:longint;
  54. begin
  55. l:=c[x,];r:=c[x,];
  56. if s[l]+=rank then exit(x)
  57. else if s[l]>=rank then exit(find(l,rank))
  58. else exit(find(r,rank-s[l]-));
  59. end;
  60. procedure insert(var x:longint;f,k:longint);inline;
  61. begin
  62. if x= then
  63. begin
  64. fa[k]:=f;
  65. x:=k;
  66. s[k]:=;
  67. c[k,]:=;c[k,]:=;
  68. exit;
  69. end;
  70. inc(s[x]);
  71. if v[k]>v[x] then insert(c[x,],x,k) else insert(c[x,],x,k);
  72. end;
  73. procedure del(rank:longint);inline;
  74. var x,y,z:longint;
  75. begin
  76. x:=find(rt,rank-);y:=find(rt,rank+);
  77. splay(x,rt);splay(y,c[x,]);
  78. z:=c[y,];fa[z]:=;c[y,]:=;s[z]:=;
  79. pushup(y);pushup(x);
  80. end;
  81. procedure print(x:longint);inline;
  82. begin
  83. if x= then exit;
  84. print(c[x,]);
  85. if first then begin first:=false;write(ss[x]);end else write(' ',ss[x]);
  86. print(c[x,]);
  87. end;
  88.  
  89. procedure main;
  90. begin
  91. readln(n);
  92. rt:=n+;fa[n+]:=;v[n+]:=-inf;v[n+]:=inf;
  93. insert(rt,,n+);
  94. tot:=;
  95. for j:= to n do
  96. begin
  97. read(ch);
  98. case ch of
  99. '+':begin
  100. readln(st);
  101. name:=copy(st,,pos(' ',st)-);
  102. delete(st,,pos(' ',st));
  103. val(st,y);
  104. x:=hash(name);
  105. if x= then insert(rt,,tot)
  106. else
  107. begin
  108. splay(x,rt);del(s[c[rt,]]+);
  109. v[x]:=y;
  110. insert(rt,,x);
  111. end;
  112. end;
  113. '?':begin
  114. readln(st);
  115. if (ord(st[])>ord('')) and (ord(st[])<=ord('')) then
  116. begin
  117. val(st,rank);
  118. x:=find(rt,rank);
  119. if rank+<=tot then y:=find(rt,rank+) else y:=find(rt,tot+);
  120. splay(x,rt);splay(y,c[x,]);
  121. first:=true;print(c[y,]);writeln;
  122. end
  123. else
  124. begin
  125. x:=hash(st);
  126. splay(x,rt);
  127. writeln(s[c[x,]]);
  128. end;
  129. end;
  130. end;
  131. end;
  132. end;
  133.  
  134. begin
  135. assign(input,'input.txt');assign(output,'output.txt');
  136. reset(input);rewrite(output);
  137. main;
  138. close(input);close(output);
  139. end.

2.lyd

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. using namespace std;
  5. const int u=,mod=;
  6. struct splaytree{
  7. int l,r,dat,size;
  8. #define l(x) t[x].l
  9. #define r(x) t[x].r
  10. #define dat(x) t[x].dat
  11. #define size(x) t[x].size
  12. }t[u];
  13. unsigned long long ver[u],temp;
  14. int L[u],R[u],head[],sc[u],id[u],next[u];
  15. char name[u][],str[];
  16. int n,m,tot,root,i,j,x,y,sco,z,now,rec;
  17.  
  18. int hash(int x,int y)
  19. {
  20. int i,z;
  21. for(temp=,i=;i<strlen(str);i++) temp=temp*+str[i]-'A'+;
  22. z=temp%mod;
  23. for(i=head[z];i;i=next[i])
  24. if(ver[i]==temp) return i;
  25. ver[++m]=temp; sc[m]=x; id[m]=y;
  26. next[m]=head[z]; head[z]=m;
  27. for(i=;i<strlen(str);i++) name[m][i-]=str[i]; name[m][i-]='\0';
  28. return m;
  29. }
  30.  
  31. inline void update(int x)
  32. {
  33. size(x)=size(l(x))+size(r(x))+;
  34. }
  35.  
  36. inline void zig(int &x)
  37. {
  38. int y=l(x); l(x)=r(y); r(y)=x;
  39. update(x),update(y),x=y;
  40. }
  41.  
  42. inline void zag(int &x)
  43. {
  44. int y=r(x); r(x)=l(y); l(y)=x;
  45. update(x),update(y),x=y;
  46. }
  47.  
  48. inline int cmp(int x,int p)
  49. {
  50. int y=dat(p);
  51. if(sc[x]==sc[y]&&id[x]==id[y]) return ;
  52. if(sc[x]>sc[y]||sc[x]==sc[y]&&id[x]<id[y]) return -;
  53. return ;
  54. }
  55.  
  56. inline void splay(int &x,int y)
  57. {
  58. int i; L[]=R[]=;
  59. while()
  60. {
  61. i=cmp(y,x);
  62. if(!i||!l(x)&&i<||!r(x)&&i>) break;
  63. if(i<)
  64. {
  65. if(cmp(y,l(x))<) {zig(x); if(!l(x)) break;}
  66. R[++R[]]=x; x=l(x);
  67. }
  68. else{
  69. if(cmp(y,r(x))>) {zag(x); if(!r(x)) break;}
  70. L[++L[]]=x; x=r(x);
  71. }
  72. }
  73. L[L[]+]=l(x); R[R[]+]=r(x);
  74. for(i=L[];i;i--) {r(L[i])=L[i+]; update(L[i]);}
  75. for(i=R[];i;i--) {l(R[i])=R[i+]; update(R[i]);}
  76. l(x)=L[]; r(x)=R[]; update(x);
  77. }
  78.  
  79. void insert(int x)
  80. {
  81. tot++; dat(tot)=x; size(tot)=;
  82. if(!root) {root=tot; return;}
  83. splay(root,x);
  84. if(cmp(x,root)<) l(tot)=l(root),l(root)=tot;
  85. else r(tot)=r(root),r(root)=tot;
  86. update(tot),update(root);
  87. }
  88.  
  89. void clear(int x)
  90. {
  91. splay(root,x);
  92. if(!l(root)) {root=r(root); return;}
  93. splay(l(root),x);
  94. r(l(root))=r(root); root=l(root);
  95. update(root);
  96. }
  97.  
  98. void ask(int x)
  99. {
  100. splay(root,x);
  101. printf("%d\n",size(l(root))+);
  102. }
  103.  
  104. void dfs(int x)
  105. {
  106. if(l(x)&&now) dfs(l(x));
  107. if(now) {now--; printf(" %s",name[dat(x)]);}
  108. if(r(x)&&now) dfs(r(x));
  109. }
  110.  
  111. void print(int rank)
  112. {
  113. int x;
  114. for(x=root;;)
  115. if(size(l(x))+==rank) break;
  116. else if(rank<=size(l(x))) x=l(x);
  117. else rank-=size(l(x))+,x=r(x);
  118. printf("%s",name[dat(x)]);
  119. splay(root,dat(x));
  120. now=; if(r(x)) dfs(r(x));
  121. puts("");
  122. }
  123.  
  124. int main()
  125. {
  126. cin>>n;
  127. for(i=;i<=n;i++)
  128. {
  129. scanf("%s",str);
  130. if(str[]=='+')
  131. {
  132. scanf("%d",&sco);
  133. rec=m;
  134. z=hash(sco,i);
  135. if(rec==m) {clear(z); sc[z]=sco; id[z]=i;}
  136. insert(z);
  137. }
  138. else if(str[]>=''&&str[]<='')
  139. {
  140. for(j=,x=;j<strlen(str);j++) x=x*+str[j]-'';
  141. print(x);
  142. }
  143. else ask(hash(,));
  144. }
  145. return ;
  146. }

HNOI2008 and ZJOI2006 排名系统的更多相关文章

  1. [BZOJ1056][BZOJ1862][HAOI2008][Zjoi2006]排名系统

    [BZOJ1056][BZOJ1862][HAOI2008][Zjoi2006]排名系统 试题描述 排名系统通常要应付三种请求:上传一条新的得分记录.查询某个玩家的当前排名以及返回某个区段内的排名记录 ...

  2. BZOJ 1862: [Zjoi2006]GameZ游戏排名系统 [treap hash]

    1862: [Zjoi2006]GameZ游戏排名系统 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1318  Solved: 498[Submit][ ...

  3. 【洛谷P2584】【ZJOI2006】GameZ游戏排名系统题解

    [洛谷P2584][ZJOI2006]GameZ游戏排名系统题解 题目链接 题意: GameZ为他们最新推出的游戏开通了一个网站.世界各地的玩家都可以将自己的游戏得分上传到网站上.这样就可以看到自己在 ...

  4. 【BZOJ1862】[ZJOI2006]游戏排名系统 (Splay)

    [BZOJ1862][ZJOI2006]游戏排名系统 (Splay) 题面 BZOJ 洛谷 题解 双倍经验题

  5. BZOJ_1862_[Zjoi2006]GameZ游戏排名系统&&BZOJ_1056_[HAOI2008]排名系统_Splay

    BZOJ_1862_[Zjoi2006]GameZ游戏排名系统&&BZOJ_1056_[HAOI2008]排名系统_Splay Description 排名系统通常要应付三种请求:上传 ...

  6. 【BZOJ】1862: [Zjoi2006]GameZ游戏排名系统 & 1056: [HAOI2008]排名系统(treap+非常小心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1862 http://www.lydsy.com/JudgeOnline/problem.php?id ...

  7. bzoj 1056 [HAOI2008]排名系统(1862 [Zjoi2006]GameZ游戏排名系统)

    1056: [HAOI2008]排名系统 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1854  Solved: 502[Submit][Statu ...

  8. 1056/1862. [ZJOI2006]GameZ游戏排名系统【平衡树-splay】

    Description GameZ为他们最新推出的游戏开通了一个网站.世界各地的玩家都可以将自己的游戏得分上传到网站上.这样就可以看到自己在世界上的排名.得分越高,排名就越靠前.当两个玩家的名次相同时 ...

  9. [HAOI2008]排名系统& [Zjoi2006]GameZ游戏排名系统

    1056: [HAOI2008]排名系统 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2487  Solved: 711[Submit][Statu ...

随机推荐

  1. ThinkPad E40无线网卡驱动安装 FOR CENTOS6.3

    1.看一下咱们用的本本的无线是咋子无线网卡,如下: [root@liaohg Downloads]# lspci | grep Wireless 03:00.0 Network controller: ...

  2. JavaScript之Function类型

    1. 创建方式 //1.函数声明 function sum(num1,num2){ return num1+num2; } //2.函数表达式 var sum = function(num1,num2 ...

  3. 关于tableView的那些坑(一)—— automaticallyAdjustsScrollViewInsets属性

    最近用tabbar来切换控制器,用childViewController来实现多控制器管理,多列表切换,在子控制器中设置了automaticallyAdjustsScrollViewInsets属性为 ...

  4. Xcode中实现ARC和MRC混编

    1.在Xcode中打开项目文件 2.选中项目名称 3.在右侧选择build phass 选项卡 4.选择 complite source 选项 5.选择要支持MRC编译的.m文件,双击 6.在弹出的框 ...

  5. C#语言之“string格式的日期时间字符串转为DateTime类型”的方法(转)

    原文链接:http://www.cnblogs.com/Pickuper/articles/2058880.html 方法一:Convert.ToDateTime(string) string格式有要 ...

  6. ASP.NET一些常用的东西

    三层架构的命名: UI: User Interface (数据显示层 用户界面)BLL:Business Logic Layer (业务逻辑层)DAL:Data Access Layer (数据访问层 ...

  7. linux中python环境搭建及升级后yum不可用解决方案

    1.1 LinuxCentOS 为例.1.1.1 升级 Python(1) 下载 Python 版本$ wget https://www.python.org/ftp/python/2.7.11/Py ...

  8. jbpm4.4 spring整合

    jBPM-4.4与Spring集成配置比较容易,这里我使用的是Spring-2.5.6,数据库连接池使用C3P0,将相关的两个jar文件加入到CLASSPATH中. jBPM-4.4与Spring集成 ...

  9. 《C和指针》章节后编程练习解答参考——第9章

    9.1 #include <stdio.h> #include <ctype.h> #include <string.h> #define N 100 int ma ...

  10. POJ 1416 Shredding Company

    题目: http://poj.org/problem?id=1416 又16ms 1A了,这人品... #include <stdio.h> #include <string.h&g ...