题意 就是求区间第k大,区间 不互相包含。

尝试用treap解决一下 第k大的问题。

  1. #include <set>
  2. #include <map>
  3. #include <cmath>
  4. #include <ctime>
  5. #include <queue>
  6. #include <stack>
  7. #include <cstdio>
  8. #include <string>
  9. #include <vector>
  10. #include <cstdlib>
  11. #include <cstring>
  12. #include <iostream>
  13. #include <algorithm>
  14. using namespace std;
  15. typedef unsigned long long ull;
  16. typedef long long ll;
  17. const int inf = 0x3f3f3f3f;
  18. const double eps = 1e-;
  19. const int maxn = 1e5+;
  20. struct Node
  21. {
  22. Node *ch[];
  23. int key, siz, r;
  24. Node (int val)
  25. {
  26. ch[] = ch[] = NULL;
  27. siz = ;
  28. key = val;
  29. r = rand();
  30. }
  31. bool operator < (const Node &rhs)const
  32. {
  33. return r < rhs.r;
  34. }
  35. int cmp(int x)const
  36. {
  37. if (key == x)
  38. return -;
  39. return key > x ? : ;
  40. }
  41. void maintain()
  42. {
  43. siz = ;
  44. if (ch[] != NULL)
  45. siz += ch[] -> siz;
  46. if (ch[] != NULL)
  47. siz += ch[] -> siz;
  48. }
  49. };
  50. void rotate(Node* &root, int d)
  51. {
  52. Node *tmp = root -> ch[d^];
  53. root -> ch[d^] = tmp -> ch[d];
  54. tmp -> ch[d] = root;
  55. root -> maintain();
  56. tmp -> maintain();
  57. root = tmp;
  58. }
  59. void insert (Node* &root, int x)
  60. {
  61. if (root == NULL)
  62. root = new Node (x);
  63. else
  64. {
  65. int d = x < root -> key ? : ;
  66. insert(root -> ch[d], x);
  67. if (root -> ch[d] -> r > root -> r)
  68. rotate(root, d^);
  69. }
  70. root -> maintain();
  71. }
  72. void dele(Node* &root, int x)
  73. {
  74. int d = root -> cmp(x);
  75. if (d == -)
  76. {
  77. Node* tmp = root;
  78. if (root -> ch[] != NULL && root -> ch[] != NULL)
  79. {
  80. int d1 = (root -> ch[] -> r > root -> ch[] -> r ? : );
  81. rotate(root, d1);
  82. dele(root -> ch[d1], x);
  83. }
  84. else
  85. {
  86. if (root -> ch[] == NULL)
  87. root = root -> ch[];
  88. else
  89. root = root -> ch[];
  90. delete tmp;
  91. }
  92. }
  93. else
  94. dele(root -> ch[d], x);
  95. if (root != NULL)
  96. root -> maintain();
  97. }
  98. int Get_kth(Node* root,int k)
  99. {
  100. if (root == NULL || k <= || k > root -> siz)
  101. return ;
  102. int s = (root -> ch[] == NULL ? : root -> ch[] ->siz);
  103. if (s + == k)
  104. return root -> key;
  105. else if (s >= k)
  106. return Get_kth(root -> ch[], k);
  107. else
  108. return Get_kth(root -> ch[], k - s -);
  109. }
  110. struct Query
  111. {
  112. int l, r, k,idx;
  113. bool operator < (const Query &rhs)const
  114. {
  115. return r < rhs.r;
  116. //return l < rhs.l || (l == rhs.l&&r < rhs.r);
  117. }
  118. }Q[];
  119. int a[], ans[];
  120. Node* treap;
  121. int main()
  122. {
  123. #ifndef ONLINE_JUDGE
  124. freopen("in.txt","r",stdin);
  125. #endif
  126. int n, m;
  127. while (~scanf ("%d%d",&n, &m))
  128. {
  129. for (int i = ; i < n; i++)
  130. scanf ("%d", a+i+);
  131. for (int i = ; i < m; i++)
  132. {
  133. scanf ("%d%d%d",&Q[i].l, &Q[i].r, &Q[i].k);
  134. Q[i].idx = i;
  135. if (Q[i].l > Q[i].r)
  136. swap(Q[i].l,Q[i].r);
  137. }
  138. sort (Q, Q+m);
  139. int index = Q[].l, lidx = ;
  140. for (int i = ; i < m; i++)
  141. {
  142. for (int j = lidx; j && j < min(Q[i].l, Q[i-].r+); j++)
  143. dele(treap, a[j]);
  144. lidx = Q[i].l;
  145. for (int j = max(Q[i].l,index); j <= Q[i].r; j++)
  146. {
  147. insert(treap, a[j]);
  148. }
  149. index = Q[i].r+;
  150. ans[Q[i].idx] = Get_kth(treap, Q[i].k);
  151.  
  152. }
  153. for (int i = ; i < m; i++)
  154. {
  155. printf("%d\n",ans[i]);
  156. }
  157. }
  158. return ;
  159. }

POJ2761---Feed the dogs (Treap求区间第k大)的更多相关文章

  1. POJ 1442 Black Box treap求区间第k大

    题目来源:POJ 1442 Black Box 题意:输入xi 输出前xi个数的第i大的数 思路:试了下自己的treap模版 #include <cstdio> #include < ...

  2. [hdu2665]Kth number(划分树求区间第k大)

    解题关键:划分树模板题. #include<cstdio> #include<cstring> #include<algorithm> #include<cs ...

  3. HDU 3473 Minimum Sum (划分树求区间第k大带求和)(转)

    题意:在区间中找一个数,求出该区间每个数与这个数距离的总和,使其最小 找的数字是中位数(若是偶数个,则中间随便哪个都可)接着找到该区间比此数大的数的总和 区间中位数可以使用划分树,然后在其中记录:每层 ...

  4. G - KiKi's K-Number(树状数组求区间第k大)

    For the k-th number, we all should be very familiar with it. Of course,to kiki it is also simple. No ...

  5. [POJ2761] Feed the dogs (Treap)

    题目链接:http://poj.org/problem?id=2761 题目大意:给你n个数,m次查询,m次查询分别是a,b,k,查询下表从a到b的第k小元素是哪个.这m个区间不会互相包含. Trea ...

  6. [poj2104]kth-number(归并树求区间第k大)

    复杂度:$O(nlog^3n)$ #include<cstdio> #include<cstring> #include<algorithm> #include&l ...

  7. HDU2665 求区间第K大 主席树

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2665 代码: //#include<bits/stdc++.h> #include< ...

  8. hdu2665可持久化线段树,求区间第K大

    Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. 【POJ2761】【区间第k大】Feed the dogs(吐槽)

    Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs ...

随机推荐

  1. 线段树---HDU2795Billboard

    这道题跟第二个题差不多,求单点的最大值. 题目大意:有个高和宽分别为h, w的广告牌, 这个广告牌分成高为 1 的长条, 每条分别能贴长度为wi长度的广告, 输入的n为广告的条数,广告优先贴在最上边和 ...

  2. nginx 根据IP 进行灰度发布

    灰度发布,简单来说,就是根据各种条件,让一部分用户使用旧版本,另一部分用户使用新版本. nginx 的语法本身可以看作是一门小型的编程语言,通过简单的编程,可以轻松实现基于IP的灰度发布. 需求:搭建 ...

  3. C# 面向对象 , 抽象基类

    抽象基类 关键字,  abstract abstract class SSS { public void aaa() { } } 作为抽象基类, 只能在 继承关系 中 担任父类的角色,不能出现在其他地 ...

  4. Oracle 空间管理

    表空间:组数据文件的一种途径 分类: 目录表空间(sysaux) 常表空间(system) 系统临时表空间(temp) 用户临时表空间(user) undo表空间 创建表空间: //表空间名为name ...

  5. php5.3.3安装mongo扩展

    /usr/bin/phpize./configure --with-php-config=/usr/bin/php-configmake && make install/usr/sbi ...

  6. TextView过长显示省略号, TextView文字中间加横线

    1.TextView显示的内容过长时自动显示省略号:  省略号的位置:android:ellipsize="end"   省略号在结尾android:ellipsize=" ...

  7. MySQL查看数据库大小、表大小和最后修改时间

    查看数据库表基本信息. select * from information_schema.TABLES where information_schema.TABLES.TABLE_SCHEMA = ' ...

  8. iOS UI_APPEARANCE_SELECTOR

    iOS后属性带UI_APPEARANCE_SELECTOR 可以统一设置全局作用 例如: 1>开关控件 @property(nullable, nonatomic, strong) UIColo ...

  9. Fiddler 网页采集抓包利器

    最近这段时间,网页采集方面的工作做得比较多.用curl技术开发了一个微信文章聚合类产品,把抓取到的数据转换成json格式,并在android端调用json数据接口加以显示:基于weiphp做了一个掌上 ...

  10. animation中的steps()逐帧动画

    在我们平时做宽高确定,需要背景图片切换的效果时,我如果用的是一张大的png图片.而且恰好是所有小图都是从左向右排列的,那么 我们只需测量出某一个小图距左侧有多少像素(x),然后我们banckgroun ...