1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. using namespace std;
  7. #define maxn 400005
  8. #define p1 63
  9. #define p2 103
  10. #define mod1 1000007
  11. #define mod2 2000007
  12. int n,tot,len,need,fact,fa[maxn],son[maxn][],val[maxn],size[maxn];
  13. char Name[maxn][],name[];
  14. void read(int &x){
  15. x=; int f=; char ch;
  16. for (ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') f=-;
  17. for (;isdigit(ch);ch=getchar()) x=x*+ch-''; x*=f;
  18. }
  19. struct S{
  20. int root;
  21. void prepare(){root=,memset(son,,sizeof(son));}
  22. int which(int x){
  23. return son[fa[x]][]==x;
  24. }
  25. void updata(int x){
  26. size[x]=;
  27. if (son[x][]) size[x]+=size[son[x][]];
  28. if (son[x][]) size[x]+=size[son[x][]];
  29. }
  30. void rotata(int x){
  31. int y=fa[x],d=which(x),dd=which(y);
  32. if (fa[y]) son[fa[y]][dd]=x; fa[x]=fa[y];
  33. fa[son[x][d^]]=y,son[y][d]=son[x][d^];
  34. fa[y]=x,son[x][d^]=y,updata(y);
  35. }
  36. void splay(int x,int goal){
  37. while (fa[x]!=goal){
  38. if (fa[fa[x]]==goal) rotata(x);
  39. else if (which(x)==which(fa[x])) rotata(fa[x]),rotata(x);
  40. else rotata(x),rotata(x);
  41. }
  42. updata(x); if (goal==) root=x;
  43. }
  44. void insert(int x){
  45. int y=root; bool bo;
  46. if (root==){
  47. root=x; updata(x);
  48. return;
  49. }
  50. for (;;){
  51. bo=;
  52. if (val[x]<=val[y]){
  53. if (!son[y][]) son[y][]=x,fa[x]=y,updata(x),updata(y),bo=,splay(x,);
  54. else y=son[y][];
  55. }else{
  56. if (son[y][]==) son[y][]=x,fa[x]=y,updata(x),updata(y),bo=,splay(x,);
  57. else y=son[y][];
  58. }
  59. if (bo==) break;
  60. }
  61. }
  62. int prep(int x){
  63. splay(x,);
  64. int y=son[x][];
  65. while (son[y][]) y=son[y][];
  66. return y;
  67. }
  68. void Delete(int x){
  69. int y=prep(x),z;
  70. if (y==){
  71. splay(x,); z=son[x][];
  72. root=z,son[x][]=son[x][]=fa[x]=size[x]=fa[z]=;
  73. }else{
  74. splay(y,),splay(x,y); z=son[x][];
  75. fa[z]=y,son[y][]=z,updata(y);
  76. fa[x]=son[x][]=son[x][]=size[x]=;
  77. }
  78. }
  79. int rank(int x){
  80. splay(x,);
  81. return size[son[x][]]+;
  82. }
  83. int kth(int x){
  84. int y=root; bool bo;
  85. for (;;){
  86. if (size[son[y][]]+==x) return y;
  87. else if (size[son[y][]]>=x) y=son[y][];
  88. else x-=size[son[y][]]+,y=son[y][];
  89. }
  90. }
  91. void print(int x){
  92. if (son[x][]) print(son[x][]);
  93. fact++;
  94. for (int i=;i<=Name[x][];i++) printf("%c",Name[x][i]);
  95. if (fact<need) printf(" ");
  96. if (son[x][]) print(son[x][]);
  97. }
  98. void query(int u,int v){
  99. int x=kth(u-),y=kth(v+),z; fact=;
  100. splay(x,),splay(y,x); z=son[y][];
  101. print(z); puts("");
  102. }
  103. }Splay;
  104. struct hash{
  105. int now[mod1+],prep[maxn],Ha[maxn][];
  106. int ha1(){
  107. int x=;
  108. for (int i=;i<len;i++){
  109. x=(x+(int)name[i])%mod1*p1%mod1;
  110. }
  111. return x;
  112. }
  113. int ha2(){
  114. int x=;
  115. for (int i=;i<len;i++){
  116. x=(x+(int)name[i])%mod2*p2%mod2;
  117. }
  118. return x;
  119. }
  120. bool exist(){
  121. int x1=ha1(),x2=ha2(); bool bo=;
  122. for (int i=now[x1];i;i=prep[i]){
  123. if (Ha[i][]==x2){
  124. bo=; break;
  125. }
  126. }
  127. return bo;
  128. }
  129. int number(){
  130. int x1=ha1(),x2=ha2();
  131. for (int i=now[x1];i;i=prep[i]){
  132. if (Ha[i][]==x2) return Ha[i][];
  133. }
  134. }
  135. void insert(){
  136. int x1=ha1(),x2=ha2();
  137. prep[++tot]=now[x1],now[x1]=tot;
  138. Ha[tot][]=x2,Ha[tot][]=tot;
  139. for (int i=;i<len;i++) Name[tot][]++,Name[tot][Name[tot][]]=name[i];
  140. }
  141. }Hash;
  142. int main(){
  143. char op[];
  144. memset(size,,sizeof(size));
  145. read(n),tot=; Splay.prepare();
  146. val[n+]=-,val[n+]=;
  147. Splay.insert(n+),Splay.insert(n+);
  148. for (int w,u,i=;i<=n;i++){
  149. scanf("%s",op+);
  150. if (op[]=='+'){
  151. len=strlen(op+); read(w);
  152. for (int j=;j<=len;j++) name[j-]=op[j];
  153. if (!Hash.exist()) Hash.insert(),u=Hash.number(),val[u]=w,Splay.insert(u);
  154. else{
  155. u=Hash.number();
  156. Splay.Delete(u),val[u]=w,Splay.insert(u);
  157. }
  158. }else if (op[]=='?'&&!isdigit(op[])){
  159. len=strlen(op+);
  160. for (int j=;j<=len;j++) name[j-]=op[j];
  161. w=Hash.number();
  162. printf("%d\n",tot-Splay.rank(w)+);
  163. }else{
  164. len=strlen(op+); w=;
  165. for (int j=;j<=len;j++) w=w*+op[j]-'';
  166. w=tot-w+;
  167. u=max(,w-+); need=w-u+;
  168. Splay.query(u+,w+);
  169. }
  170. }
  171. return ;
  172. }

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1862

题意参照题面。

做法:Splay+Hash。

裸的splay,支持插入删除查询排名即可,为什么要用Hash呢,因为如果某玩家上传过记录就得把之前的记录清空,所以我们需要用字符串Hash来判重,字符串我们使用双hash值,一个用来确定地址,第一个来作为val,这样就能降低在哈希表中查找的复杂度,再记录该玩家在Splay树中的标号即可。

splay+Hash。

Hash_bzoj1862: [Zjoi2006]GameZ游戏排名系统的更多相关文章

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

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

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

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

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

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

  4. bzoj1862: [Zjoi2006]GameZ游戏排名系统

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

  5. [ZJOI2006]GameZ游戏排名系统

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

  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. [HAOI2008]排名系统& [Zjoi2006]GameZ游戏排名系统

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

  9. [洛谷P2584][ZJOI2006]GameZ游戏排名系统

    题目大意:同[洛谷P4291][HAOI2008]排名系统(双倍经验) 题解:略 卡点:无 C++ Code: #include <cstdio> #include <map> ...

随机推荐

  1. Frequently Asked Questions - P-thresholds

    Source: http://mindhive.mit.edu/book/export/html 1. What is the multiple-comparison problem? What is ...

  2. ubuntu12.04禁止单用户本地无密码root登录

    1)grub-mkpasswd-pbkdf2 拿到类似grub.pbkdf2.sha512.10000.C093FE6825CDCC2F84934ABC406445E92EE098733C60E6D1 ...

  3. 【MVC】自定义Scaffold Template

    MVC提供了基本的CRUD Scaffold Template模板,创建视图的时候,只要勾选创建一个强类型视图 , 并选择模型类,就可以选择支架模板了,这些模板包括List,Detail,Create ...

  4. JQuery实现资讯上下滚动悬停效果

    第一步:使用repeater绑定一个table. <table width="530" id="rollBar"> <asp:Repeater ...

  5. 如何拿到半数面试公司Offer——我的Python求职之路

    从八月底开始找工作,短短的一星期多一些,面试了9家公司,拿到5份Offer,可能是因为我所面试的公司都是些创业性的公司吧,不过还是感触良多,因为学习Python的时间还很短,没想到还算比较容易的找到了 ...

  6. 用 Linux自带的logrotate 来管理日志

    大家可能都有管理日志的需要,比如定时压缩日志,或者当日志超过一定大小时就自动分裂成两个文件等.最近就接到这样一个小任务.我们的程序用的是C语言,用log4cpp的library来实现日志记录.但是问题 ...

  7. iostat命令详解

    iostat iostat用于输出CPU和磁盘I/O相关的统计信息. 命令格式: iostat [ -c | -d ] [ -k | -m ] [ -t ] [ -V ] [ -x ] [ devic ...

  8. EF增删改查操作

    增 using (StudentEntities ent = new StudentEntities()) { User aNewUser = new User() { Age = , Name = ...

  9. thinkphp 配置多数据库

    1配置文件中配置另一数据库连接信息 例如: 'TestModelConfig' => array( //'配置项'=>'配置值' 'DB_TYPE' => 'mysql', // 数 ...

  10. 70 sudo-用来以其他身份来执行命令

    sudo命令用来以其他身份来执行命令,预设的身份为root.在/etc/sudoers中设置了可执行sudo指令的用户.若其未经授权的用户企图使用sudo,则会发出警告的邮件给管理员.用户使用sudo ...