题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5249

思路分析:使用queue记录管道中的值并使用treap能够查询第K大的功能查询第floor(m/2)+1大的数值;

对于in value操作,将value插入queue中和treap中;对于out操作,在treap中删除queue中队头元素,并在queue中使队头元素出队;

对于query操作,因为tail – head的值即为管道中的值的个数,使用treap查询第floor((tail – head) / 2) + 1大的元素即可;

代码如下:

  1. #include <cstdio>
  2. #include <ctime>
  3. #include <cstring>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. const int MAX_N = + ;
  8. int queue[MAX_N];
  9.  
  10. struct Node{
  11. Node *ch[];
  12. int value, key;
  13. int size;
  14. int cmp(int x) const{
  15. if (x == value) return -;
  16. return x < value ? : ;
  17. }
  18. void Maintain(){
  19. size = ;
  20. if (ch[] != NULL) size += ch[]->size;
  21. if (ch[] != NULL) size += ch[]->size;
  22. }
  23. };
  24.  
  25. void Rotate(Node *&o, int d){
  26. Node *k = o->ch[d ^ ];
  27. o->ch[d ^ ] = k->ch[d];
  28. k->ch[d] = o;
  29. o->Maintain();
  30. k->Maintain();
  31. o = k;
  32. }
  33.  
  34. void Insert(Node *&o, int x){
  35. if (o == NULL){
  36. o = new Node();
  37. o->ch[] = o->ch[] = NULL;
  38. o->value = x;
  39. o->key = rand();
  40. }
  41. else{
  42. int d = (x < (o->value) ? : );
  43. Insert(o->ch[d], x);
  44. if (o->ch[d]->key > o->key)
  45. Rotate(o, d ^ );
  46. }
  47. o->Maintain();
  48. }
  49.  
  50. void Remove(Node *&o, int x){
  51. int d = o->cmp(x);
  52.  
  53. if (d == -){
  54. Node *u = o;
  55.  
  56. if (o->ch[] != NULL && o->ch[] != NULL){
  57. int d2 = (o->ch[]->key > o->ch[]->key ? : );
  58.  
  59. Rotate(o, d2);
  60. Remove(o->ch[d2], x);
  61. }else{
  62. if (o->ch[] == NULL)
  63. o = o->ch[];
  64. else
  65. o = o->ch[];
  66. delete u;
  67. }
  68. }else
  69. Remove(o->ch[d], x);
  70. if (o != NULL)
  71. o->Maintain( );
  72. }
  73.  
  74. int Find(Node *o, int x){
  75. while (o != NULL){
  76. int d = o->cmp(x);
  77.  
  78. if (d == -) return ;
  79. else o = o->ch[d];
  80. }
  81. return ;
  82. }
  83.  
  84. int FindKth(Node *o, int k){
  85. int l_size = (o->ch[] == NULL ? : o->ch[]->size);
  86. if (k == l_size + )
  87. return o->value;
  88. else if (k <= l_size)
  89. return FindKth(o->ch[], k);
  90. else
  91. return FindKth(o->ch[], k - l_size - );
  92. }
  93.  
  94. void MakeEmpty(Node *root){
  95. if (root == NULL)
  96. return;
  97. if (root->ch[])
  98. MakeEmpty(root->ch[]);
  99. if (root->ch[])
  100. MakeEmpty(root->ch[]);
  101. delete root;
  102. }
  103.  
  104. int main(){
  105. int n, case_id = , value;
  106.  
  107. srand();
  108. while (scanf("%d", &n) != EOF){
  109. Node *root = NULL;
  110. int head = , tail = , ans;
  111. char str[];
  112.  
  113. printf("Case #%d:\n", ++case_id);
  114. for (int i = ; i < n; ++i){
  115. scanf("%s", str);
  116. if (str[] == 'i'){
  117. scanf("%d", &value);
  118. queue[tail++] = value;
  119. Insert(root, value);
  120. }
  121. else if (str[] == 'o')
  122. Remove(root, queue[head++]);
  123. else{
  124. ans = FindKth(root, (tail - head) / + );
  125. printf("%d\n", ans);
  126. }
  127. }
  128. MakeEmpty(root);
  129. }
  130. return ;
  131. }

hdoj 5249 KPI(treap)的更多相关文章

  1. hdu 5249 KPI

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5249 KPI Description 你工作以后, KPI 就是你的全部了. 我开发了一个服务,取得了 ...

  2. HDU 5249:KPI(权值线段树)

    KPI Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Desc ...

  3. 2015年百度之星初赛(1) --- D KPI

    KPI Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. fhq treap最终模板

    新学习了fhq treap,厉害了 先贴个神犇的版, from memphis /* Treap[Merge,Split] by Memphis */ #include<cstdio> # ...

  5. 大数据慎行,数据管理要落实到KPI

    近年来,"大数据"一词被IT和互联网行业广泛提及,但真正落到实处的案例没有多少,大数据量支撑.数据挖掘技术.非结构化数据是阻碍的主要原因.大多数企业的信息化并没有达到到成熟水平,关 ...

  6. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  8. HDOJ 1326. Box of Bricks 纯水题

    Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  9. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]

    1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 786  Solved: 391[Submit][S ...

随机推荐

  1. idea15破解

    注册方法:   注册码可以沿用14的,只是在 注册时选择 License server ,填 http://idea.lanyus.com ,然后点击 OK 14的话,网上可以找到一个,根据你的用户名 ...

  2. c#Ulong用一个高位Uint和低位Uint表示

    有时候考虑到平台之间的通用性,可能把一个Ulong拆分成2个Uint来进行各平台之间的通讯,当时转换的时候有点头晕,对与或预算不是很熟悉,不过还是花了半小时弄出来了,代码: //ulong的最大值2^ ...

  3. B - Moving Tables

    B - Moving Tables Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  4. Mysql 如何做双机热备和负载均衡 (方法二)

    先简要介绍一下mysql双向热备:mysql从3.23.15版本以后提供数据库复制功能.利用该功能可以实现两个数据库同步,主从模式(A->B),互相备份模式(A<=>B)的功能. m ...

  5. Android 修改host文件的3种方法

    Android修改hosts文件的方法介绍 本文介绍三种Android手机修改hosts文 件的方法,但修改hosts文件一定要谨慎:Android手机hosts文件的换行符必须是n而不是window ...

  6. 3种方式实现可滑动的Tab

    1. 第一种,使用 TabHost + ViewPager 实现 该方法会有一个Bug,当设置tabHost.setCurrentTab()为0时,ViewPager不显示(准确的说是加载),只有点击 ...

  7. .net mvc笔记2_Essential C# Features

    Essential C# Features 1.Using Automatically Implemented Properties public class Product { private st ...

  8. MySQLD 配置

    http://blog.163.com/sir_876/blog/static/11705223201372710303382/ http://www.kankanews.com/ICkengine/ ...

  9. POJ 3581 Sequence(后缀数组)

    [题目链接] http://poj.org/problem?id=3581 [题目大意] 给出一个数列,将这个数列分成三段,每段分别翻转,使得其字典序最小,输出翻转后的数列. [题解] 首先,第一个翻 ...

  10. Linux操作系统定时任务系统Cron入门、PHP计划任务以及rpc示例

    一.简单介绍 1.cron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业.由于Cron 是Linux的内置服务,但它不自动起来,可以用以下的方法启动.关闭这个服务: servic ...