记录编号 272331 评测结果 AAAAAAAAAA
题目名称 [HAOI 2008]排名系统 最终得分 100
用户昵称 GravatarTenderRun 是否通过 通过
代码语言 C++ 运行时间 2.550 s
提交时间 2016-06-16 20:17:44 内存使用 6.46 MiB
显示代码纯文本
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <string>
  5. #include <map>
  6. using namespace std;
  7. const int maxn=300010;
  8. const int INF=2147483647;
  9. map<string,int>ID;
  10. char c;
  11. string name[maxn],str;
  12. int rt,cnt,fa[maxn],ch[maxn][2],sz[maxn],key[maxn];
  13.  
  14. void Push_up(int x){
  15. sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;
  16. }
  17.  
  18. void Rotate(int x){
  19. int y=fa[x],g=fa[y],c=ch[y][1]==x;
  20. ch[y][c]=ch[x][c^1];
  21. if(ch[x][c^1])fa[ch[x][c^1]]=y;
  22. ch[x][c^1]=y;fa[y]=x;fa[x]=g;
  23. if(g)ch[g][ch[g][1]==y]=x;
  24. Push_up(y);
  25. }
  26.  
  27. void Splay(int x,int g=0){
  28. for(int y;(y=fa[x])!=g;Rotate(x))
  29. if(fa[y]!=g)Rotate((ch[fa[y]][1]==y)==(ch[y][1]==x)?y:x);
  30. Push_up(x);
  31. if(!g)rt=x;
  32. }
  33.  
  34. void Get_LR(int x,int &l,int &r){
  35. Splay(x);
  36. l=ch[x][0];r=ch[x][1];
  37. while(ch[l][1])l=ch[l][1];
  38. while(ch[r][0])r=ch[r][0];
  39. }
  40.  
  41. void Delete(int x){
  42. int l,r;
  43. Get_LR(x,l,r);
  44. Splay(l);Splay(r,rt);
  45. ch[ch[rt][1]][0]=0;
  46. Push_up(ch[rt][1]);
  47. Push_up(rt);
  48. }
  49.  
  50. void Insert(int &p,int f,int id,int x){
  51. if(!p){
  52. key[p=id]=x;
  53. fa[p]=f;
  54. sz[p]=1;
  55. Splay(p);
  56. return;
  57. }
  58. sz[p]+=1;
  59. Insert(ch[p][key[p]<x],p,id,x);
  60. }
  61.  
  62. int Get_ID(int k){
  63. int p=rt;
  64. while(true){
  65. if(sz[ch[p][0]]+1==k)break;
  66. if(sz[ch[p][0]]+1>k)p=ch[p][0];
  67. else k-=sz[ch[p][0]]+1,p=ch[p][1];
  68. }
  69. return p;
  70. }
  71.  
  72. void Print(int p){
  73. if(!p)return;
  74. Print(ch[p][1]);
  75. cout<<name[p]<<" ";
  76. Print(ch[p][0]);
  77. }
  78.  
  79. void Init(){
  80. Insert(rt,0,1,-INF);
  81. Insert(rt,0,2,INF);cnt=2;
  82. }
  83.  
  84. int main(){
  85. #ifndef ONLINE_JUDGE
  86. freopen("rank.in","r",stdin);
  87. freopen("rank.out","w",stdout);
  88. #endif
  89. Init();
  90. int Q,d,tot=0;
  91. scanf("%d",&Q);
  92. while(Q--){
  93. do c=getchar();
  94. while(c!='+'&&c!='?');
  95. if(c=='+'){
  96. cin>>str;
  97. scanf("%d",&d);
  98. if(ID[str]){
  99. Delete(ID[str]);
  100. Insert(rt,0,ID[str],d);
  101. }
  102. else{
  103. ID[str]=++cnt;tot+=1;name[cnt]=str;
  104. Insert(rt,0,ID[str],d);
  105. }
  106. }
  107. else{
  108. cin>>str;
  109. if(str[0]<='9'&&str[0]>='0'){
  110. int l,r=0;
  111. for(int i=0;str[i];i+=1)
  112. r=r*10+(str[i]-'0');
  113. r=tot-r+1;l=max(r-9,1);
  114. Splay(Get_ID(l));Splay(Get_ID(r+2),rt);
  115. Print(ch[ch[rt][1]][0]);
  116. printf("\n");
  117. }
  118. else{
  119. Splay(Get_ID(1));Splay(ID[str],rt);
  120. printf("%d\n",tot-sz[ch[ch[rt][1]][0]]);
  121. }
  122. }
  123. }
  124. return 0;
  125. }