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
 
Solution:区间第K大,主席树模板
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #define N 100000
  5. using namespace std;
  6. int sum[],ls[],rs[],root[N+],hash[N+],a[N+],num[N+];
  7. int n,m,cnt,size;
  8. int findpos(int x)
  9. {
  10. int l=,r=cnt,mid;
  11. while (l<=r)
  12. {
  13. mid=(l+r)>>;
  14. if (hash[mid]<x) l=mid+;
  15. else r=mid-;
  16. }
  17. return l;
  18. }
  19.  
  20. void updata(int l,int r,int x,int &y,int v)
  21. {
  22. y=++size; sum[y]=sum[x]+;
  23. if (l==r) return;
  24. ls[y]=ls[x]; rs[y]=rs[x];
  25. int mid=(l+r)>>;
  26. if (v<=mid) updata(l,mid,ls[x],ls[y],v);
  27. else updata(mid+,r,rs[x],rs[y],v);
  28. }
  29.  
  30. int query(int l,int r,int x,int y,int k)
  31. {
  32. if (l==r) return l;
  33. int mid=(l+r)>>;
  34. if (sum[ls[y]]-sum[ls[x]]>=k) return query(l,mid,ls[x],ls[y],k);
  35. else return query(mid+,r,rs[x],rs[y],k-(sum[ls[y]]-sum[ls[x]]));
  36. }
  37.  
  38. int main()
  39. {
  40. scanf("%d%d",&n,&m);
  41. for (int i=;i<=n;i++)
  42. {
  43. scanf("%d",&a[i]);
  44. num[i]=a[i];
  45. }
  46. sort(num+,num+n+);
  47. hash[++cnt]=num[];
  48. for (int i=;i<=n;i++)
  49. if (num[i]!=num[i-])
  50. hash[++cnt]=num[i];
  51. for (int i=;i<=n;i++)
  52. {
  53. int t=findpos(a[i]);
  54. updata(,cnt,root[i-],root[i],t);
  55. }
  56. for (int i=;i<=m;i++)
  57. {
  58. int a,b,x,re;
  59. scanf("%d%d%d",&a,&b,&x);
  60. re=query(,cnt,root[a-],root[b],x);
  61. printf("%d\n",hash[re]);
  62. }
  63. return ;
  64. }

【POJ2104/2761】K-th Number的更多相关文章

  1. 【codeforces 546D】Soldier and Number Game

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【11.61%】【codeforces 670F】Restore a Number

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【codeforces 514A】Chewbaсca and Number

    [题目链接]:http://codeforces.com/contest/514/problem/A [题意] 允许你把每个数字翻转 ->x变成9-x 然后问你能够变成的最小的数字是什么; 不能 ...

  4. 【分类算法】K近邻(KNN) ——kd树(转载)

    K近邻(KNN)的核心算法是kd树,转载如下几个链接: [量化课堂]一只兔子帮你理解 kNN [量化课堂]kd 树算法之思路篇 [量化课堂]kd 树算法之详细篇

  5. 【学习笔记】K 短路问题详解

    \(k\) 短路问题简介 所谓"\(k\) 短路"问题,即给定一张 \(n\) 个点,\(m\) 条边的有向图,给定起点 \(s\) 和终点 \(t\),求出所有 \(s\to t ...

  6. 【POJ 3696】 The Luckiest number

    [题目链接] http://poj.org/problem?id=3696 [算法] 设需要x个8 那么,这个数可以表示为 : 8(10^x - 1) / 9, 由题, L | 8(10^x - 1) ...

  7. 【 CodeForces - 392C】 Yet Another Number Sequence (二项式展开+矩阵加速)

    Yet Another Number Sequence Description Everyone knows what the Fibonacci sequence is. This sequence ...

  8. 【洛谷 P1216】【IOI1994】【USACO1.5】数字三角形 Number Triangles

    (如此多的标签qaq) 数字三角形 Number Triangles[传送门] 本来打算当DP练的,没想到写着写着成递推了(汗) 好的没有时间了,我们附个ac代码(改天不写): #include< ...

  9. 【LeetCode 25】K 个一组翻转链表

    题目链接 [题解] 模拟就好. 就k个k个节点地翻转. 每个节点都把next域指向它前面那个节点 修改完之后把这个节点前面的那个节点的next域改成这一段的最后一个节点. 然后把这一段最左边的那个节点 ...

随机推荐

  1. 【翻译八】java-内存一致性错误

    Memory Consistency Errors Memory consistency errors occur when different threads have inconsistent v ...

  2. Visual Studio Code 1.0发布:100+语言,300+pull请求,1000+扩展

    在第一个预览版发布一年后,微软发表了Visual Studio Code 1.0. 在//BUILD 2015大会上,微软宣布,他们的一个团队需要几个月来创建Visual Studio Code的第一 ...

  3. Oracle 备份与恢复介绍

    一.Oracle备份方式分类:Oracle有两类备份方式:(1)物理备份:是将实际组成数据库的操作系统文件从一处拷贝到另一处的备份过程,通常是从磁盘到磁带.物理备份又分为冷备份.热备份:   (2)逻 ...

  4. Java8中的default方法

    default方法 Java 8中引入了一个新的概念,叫做default方法,也可以称为Defender方法,或者虚拟扩展方法(Virtual extension methods). Default方 ...

  5. Win7下用IIS发布网站

    安装IIS控制面板->程序->程序和功能, 点击左侧的“打开或关闭Windows功能”把这几项都勾上吧,虽然有些不是必须的,多勾无碍. 进入IIS管理器控制面板-> 系统和安全-&g ...

  6. python最简单的http服务器

    人生苦短,我用python 今天有个需求就是简单的把自己的图片通过web共享,自然就想起了使用服务器了,在python下使用一个简单的服务器是非常方便的,用到标准库里面的SimpleHTTPServe ...

  7. 汇编学习(五)——表处理程序

    (一)串操作指令及重复前缀 一.串操作指令: 1.串传送指令: (1)指令格式: MOVS dst,rsc MOVSB ;ES:[DI]<--DS:[SI],SI<-SI+/-,DI< ...

  8. 【转】HTML网页中插入视频各种方法

    现在如果要在页面中使用video标签,需要考虑三种情况,支持Ogg Theora或者VP8(如果这玩意儿没出事的话)的(Opera.Mozilla.Chrome),支持H.264的(Safari.IE ...

  9. SecureCRT上传和下载文件(下载默认目录)

    SecureCR 下的文件传输协议有ASCII .Xmodem .Ymodem .Zmodem ASCII:这是最快的传输协议,但只能传送文本文件. Xmodem:这种古老的传输协议速度较慢,但由于使 ...

  10. Android 自定义实现switch开关按钮

    前几天在看蘑菇街上有个开关按钮: 就在想是怎样实现的,于是反编译了它的源码,但是这时得到了下面的几张图片: 图片对应的名称: 无色长条:switch_frame; 白色圆点:switch_btn_pr ...