记录编号 457077 评测结果 AAAAAAAAAA
题目名称 [SDOI 2007] 超级数组 最终得分 100
用户昵称 GravatarHzoi_Mafia 是否通过 通过
代码语言 C++ 运行时间 0.388 s
提交时间 2017-10-06 17:50:36 内存使用 0.34 MiB
显示代码纯文本
  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #include<cstdio>
  5. #include<ctime>
  6. using namespace std;
  7. inline int read(){
  8. int sum(0);
  9. char ch(getchar());
  10. for(;ch<'0'||ch>'9';ch=getchar());
  11. for(;ch>='0'&&ch<='9';sum=sum*10+(ch^48),ch=getchar());
  12. return sum;
  13. }
  14. #define get_size(x) (x?x->size:0)
  15. struct node{
  16. node *lch,*rch;
  17. int size,key,fix;
  18. node(int x=0):lch(NULL),rch(NULL),size(1),key(x),fix(rand()){}
  19. inline void pushup(){
  20. this->size=get_size(this->lch)+get_size(this->rch)+1;
  21. }
  22. }*root;
  23. inline void left_rotate(node *&x){
  24. node *tmp(x->rch);
  25. x->rch=tmp->lch;
  26. tmp->lch=x;
  27. x->pushup();
  28. tmp->pushup();
  29. x=tmp;
  30. }
  31. inline void right_rotate(node *&x){
  32. node *tmp(x->lch);
  33. x->lch=tmp->rch;
  34. tmp->rch=x;
  35. x->pushup();
  36. tmp->pushup();
  37. x=tmp;
  38. }
  39. inline void insert(node *&x,int v){
  40. if(!x){
  41. x=new node(v);
  42. return;
  43. }
  44. if(v<=x->key){
  45. insert(x->lch,v);
  46. x->pushup();
  47. if(x->lch->fix<x->fix)
  48. right_rotate(x);
  49. }
  50. else{
  51. insert(x->rch,v);
  52. x->pushup();
  53. if(x->rch->fix<x->fix)
  54. left_rotate(x);
  55. }
  56. }
  57. inline void del(node *&x,int v){
  58. if(x->key==v){
  59. if(x->lch&&x->rch){
  60. if(x->lch->fix<x->rch->fix){
  61. right_rotate(x);
  62. del(x->rch,v);
  63. }
  64. else{
  65. left_rotate(x);
  66. del(x->lch,v);
  67. }
  68. }
  69. else{
  70. node *tmp(NULL);
  71. if(x->lch)
  72. tmp=x->lch;
  73. else
  74. tmp=x->rch;
  75. delete x;
  76. x=tmp;
  77. }
  78. }
  79. else{
  80. if(v<=x->key)
  81. del(x->lch,v);
  82. else
  83. del(x->rch,v);
  84. }
  85. if(x)
  86. x->pushup();
  87. }
  88. inline int kth(int k){
  89. node *now(root);
  90. while(now){
  91. if(get_size(now->lch)+1==k)
  92. return now->key;
  93. if(get_size(now->lch)+1>=k)
  94. now=now->lch;
  95. else
  96. k-=get_size(now->lch)+1,now=now->rch;
  97. }
  98. }
  99. int n,m;
  100. char op[2];
  101. inline int gg(){
  102. freopen("arr.in","r",stdin);
  103. freopen("arr.out","w",stdout);
  104. srand(time(NULL));
  105. n=read(),m=read();
  106. while(m--){
  107. scanf("%s",op);
  108. if(op[0]=='i'){
  109. int x(read());
  110. insert(root,x);
  111. }
  112. else{
  113. int x(read());
  114. int ans(kth(x));
  115. printf("%d\n",ans);
  116. del(root,ans);
  117. }
  118. }
  119. return 0;
  120. }
  121. int K(gg());
  122. int main(){;}