题目链接:https://vjudge.net/problem/POJ-2104

K-th Number
Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 64110   Accepted: 22556
Case Time Limit: 2000MS

Description

You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment. 
That is, given an array a[1...n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: "What would be the k-th number in a[i...j] segment, if this segment was sorted?" 
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2...5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.

Input

The first line of the input file contains n --- the size of the array, and m --- the number of questions to answer (1 <= n <= 100 000, 1 <= m <= 5 000). 
The second line contains n different integer numbers not exceeding 109 by their absolute values --- the array for which the answers should be given. 
The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).

Output

For each question output the answer to it --- the k-th number in sorted a[i...j] segment.

Sample Input

  1. 7 3
  2. 1 5 2 6 3 7 4
  3. 2 5 3
  4. 4 4 1
  5. 1 7 3

Sample Output

  1. 5
  2. 6
  3. 3

Hint

This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.

Source

Northeastern Europe 2004, Northern Subregion

题解:

查询区间第k小(不带修改),整体二分。

代码如下:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <cmath>
  7. #include <queue>
  8. #include <stack>
  9. #include <map>
  10. #include <string>
  11. #include <set>
  12. using namespace std;
  13. typedef long long LL;
  14. const int INF = 2e9;
  15. const LL LNF = 9e18;
  16. const int MOD = 1e9+;
  17. const int MAXN = 2e5+;
  18.  
  19. struct node
  20. {
  21. int x, y, k, type, id;
  22. };
  23. node q[MAXN];
  24.  
  25. int n, m, c[MAXN];
  26. int lowbit(int x) {return x&(-x);}
  27. void add(int x, int val) {for(int i=x;i<=n;i+=lowbit(i)) c[i]+=val;}
  28. int sum(int x) {int ret=; for(int i=x;i>;i-=lowbit(i))ret+=c[i]; return ret;}
  29.  
  30. int ans[MAXN];
  31. node q1[MAXN], q2[MAXN]; //两个桶
  32. void solve(int l, int r, int ql, int qr) //二分答案
  33. {
  34. if(ql>qr) return; //!!
  35. if(l==r) //当l==r时,即答案已明确
  36. {
  37. for(int i = ql; i<=qr; i++)
  38. if(q[i].type==) ans[q[i].id] = l;
  39. return;
  40. }
  41.  
  42. int mid = (l+r)>>; //写成 (l+r)/2会runtime error,不知为何
  43. int t1 = , t2 = ;
  44. for(int i = ql; i<=qr; i++) //枚举操作
  45. {
  46. if(q[i].type==)
  47. {
  48. if(q[i].y<=mid)
  49. {
  50. add(q[i].x, );
  51. q1[++t1] = q[i];
  52. }else q2[++t2] = q[i];
  53. }
  54. else
  55. {
  56. int pre = sum(q[i].y)-sum(q[i].x-);
  57. if(pre>=q[i].k) q1[++t1] = q[i];
  58. else
  59. {
  60. q[i].k -= pre;
  61. q2[++t2] = q[i];
  62. }
  63. }
  64. }
  65. for(int i = ; i<=t1; i++) //撤回对线段树的操作
  66. if(q1[i].type==) add(q1[i].x, -);
  67.  
  68. for(int i = ; i<=t1; i++) q[ql+i-] = q1[i];
  69. for(int i = ; i<=t2; i++) q[ql+t1+i-] = q2[i];
  70. solve(l, mid, ql, ql+t1-);
  71. solve(mid+, r, ql+t1, qr);
  72. }
  73.  
  74. int main()
  75. {
  76. while(scanf("%d%d",&n,&m)==)
  77. {
  78. int tot = ;
  79. for(int i = ; i<=n; i++)
  80. {
  81. ++tot;
  82. scanf("%d", &q[tot].y);
  83. q[tot].x = i; q[tot].type = ;
  84. }
  85. for(int i = ; i<=m; i++)
  86. {
  87. ++tot;
  88. scanf("%d%d%d", &q[tot].x,&q[tot].y,&q[tot].k);
  89. q[tot].id = i; q[tot].type = ;
  90. }
  91.  
  92. memset(c, , sizeof(c));
  93. solve(-MOD,MOD, ,tot);
  94. for(int i = ; i<=m; i++)
  95. printf("%d\n", ans[i]);
  96. }
  97. }

POJ2104 K-th Number —— 区间第k小 整体二分的更多相关文章

  1. HDU 2665.Kth number 区间第K小

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

  2. POJ 2014.K-th Number 区间第k小 (归并树)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 57543   Accepted: 19893 Ca ...

  3. 【XSY2720】区间第k小 整体二分 可持久化线段树

    题目描述 给你你个序列,每次求区间第\(k\)小的数. 本题中,如果一个数在询问区间中出现了超过\(w\)次,那么就把这个数视为\(n\). 强制在线. \(n\leq 100000,a_i<n ...

  4. 静态区间第k小 - 整体二分

    蒟蒻终于学会整体二分啦! 思路 实现 丑陋无比的代码 #include <bits/stdc++.h> using namespace std; const int N = 200005; ...

  5. BZOJ 3110([Zjoi2013]K大数查询-区间第k大[段修改,在线]-树状数组套函数式线段树)

    3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec   Memory Limit: 512 MB Submit: 418   Solved: 235 [ Submit][ ...

  6. BZOJ3110[Zjoi2013]K大数查询(树状数组+整体二分)

    3110 [Zjoi2013]K大数查询 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a ...

  7. bzoj 3110 [Zjoi2013]K大数查询【树套树||整体二分】

    树套树: 约等于是个暴力了.以区间线段树的方式开一棵权值线段树,在权值线段树的每一个点上以动态开点的方式开一棵区间线段树. 结果非常惨烈(时限20s) #include<iostream> ...

  8. poj2104 K-th Number区间第k小值 主席树

    原来主席树就是可持久化线段树啊,刚知道,,, 作为一道裸题,还是必A的,然而一开始偷懒不写离散化跪了N多遍,后来在缪大的帮助下发现了这个问题,遂A之 ——又是这种破问题,实在不想说自己了 把n个数看成 ...

  9. POJ 2104 K-th Number(区间第k大数)(平方切割,归并树,划分树)

    题目链接: http://poj.org/problem? id=2104 解题思路: 由于查询的个数m非常大.朴素的求法无法在规定时间内求解. 因此应该选用合理的方式维护数据来做到高效地查询. 假设 ...

随机推荐

  1. linux中shell脚本中系统预先定义的变量

    $0:脚本名称: $*:所有参数: $$:当前进程或者脚本的PID号: $!:后台运行的最后一个进程的PID号: $?:用于返回上一个命令是否成功.成功0,否则为非零: $#:参数个数: $@:所有参 ...

  2. 数据结构基础-Hash Table详解(转)

    理解Hash 哈希表(hash table)是从一个集合A到另一个集合B的映射(mapping). 映射是一种对应关系,而且集合A的某个元素只能对应集合B中的一个元素.但反过来,集合B中的一个元素可能 ...

  3. apue学习笔记(第八章 进程控制)

    本章介绍UNIX系统的进程控制,包括创建新进程.执行程序和进程终止. 进程标识 每一个进程都有一个非负整数表示的唯一进程ID,除了进程ID,每个进程还有一些其他标识符.下列函数返回这些标识符 #inc ...

  4. apue学习笔记(第四章 文件和目录)

    本章将描述文件系统的其他特性和文件的性质. 函数stat.fstat.fstatat和lstat #include <sys/stat.h> int stat(const char *re ...

  5. bootstrap selectpicker使用问题

    文档查阅:http://silviomoreto.github.io/bootstrap-select/options/ 1.实用属性 size:5  表示下拉列表默认展示5行(ie8展示4.5行) ...

  6. apache hadoop 2.4.0 64bit 在windows8.1下直接安装指南(无需虚拟机和cygwin)

    工作须要.要開始搞hadoop了,又是大数据,自己感觉大数据.云.仅仅是ERP.SOAP风潮之后与智能地球一起诞生的概念炒作. 只是Apache是个奇妙的组织.Java假设没有它也不会如今如火中天.言 ...

  7. 总结自己使用shell命令行经常使用到的8个小技巧

    原创blog,转载请注明出处 Shell是命令解释器 [root@localhost ~]# cat /etc/shells 查看本系统共支持哪些shell 1 tab 命令补全 这个差点儿每次都能用 ...

  8. matlab-1

    1.size():获取矩阵的行数和列数 (1)s=size(A), 当只有一个输出参数时,返回一个行向量,该行向量的第一个元素时矩阵的行数,第二个元素是矩阵的列数.(2)[r,c]=size(A),当 ...

  9. Spring Cloud(十三):Spring Cloud Sleuth服务链路追踪(zipkin)(转)

    这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件. 一.简介 Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案, ...

  10. python学习(八)阶段性总结

    这里学习了python的一点点基础然后来总结一下 python是一个强类型的语言 数字: 数字的类型大概有:整数.浮点数.复数.固定精度的十进制数.带分子和分母的有理数 数字与字符串之间不能直接相加, ...