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.  

Sample Output

  1.  

Hint

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

Solution

主席树模版题

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. using namespace std;
  6. inline int read(){
  7. int x=,c=getchar(),f=;
  8. for(;c<||c>;c=getchar())
  9. if(!(c^))
  10. f=-;
  11. for(;c>&&c<;c=getchar())
  12. x=(x<<)+(x<<)+c-;
  13. return x*f;
  14. }
  15. int n,q,sz,tot,ls[],rs[],s[],a[],root[],num[],hsh[];
  16. inline int bin(int x){
  17. int l=,r=tot,mid;
  18. for(;l<=r;){
  19. mid=l+r>>;
  20. if(hsh[mid]<x)
  21. l=mid+;
  22. else
  23. r=mid-;
  24. }
  25. return l;
  26. }
  27. void ins(int l,int r,int x,int &y,int v){
  28. y=++sz;
  29. s[y]=s[x]+;
  30. if(!(l^r))
  31. return;
  32. ls[y]=ls[x]; rs[y]=rs[x];
  33. int mid=l+r>>;
  34. if(v<=mid)
  35. ins(l,mid,ls[x],ls[y],v);
  36. else
  37. ins(mid+,r,rs[x],rs[y],v);
  38. }
  39. int req(int l,int r,int x,int y,int k){
  40. if(!(l^r))
  41. return l;
  42. int mid=l+r>>;
  43. if(k<=s[ls[y]]-s[ls[x]])
  44. return req(l,mid,ls[x],ls[y],k);
  45. else
  46. return req(mid+,r,rs[x],rs[y],k-s[ls[y]]+s[ls[x]]);
  47. }
  48. int main(){
  49. n=read(),q=read();
  50. for(int i=;i<=n;i++)
  51. num[i]=a[i]=read();
  52. sort(num+,num++n);
  53. hsh[++tot]=num[];
  54. for(int i=;i<=n;i++)
  55. if(num[i]^num[i-])
  56. hsh[++tot]=num[i];
  57. for(int i=;i<=n;i++)
  58. ins(,tot,root[i-],root[i],bin(a[i]));
  59. while(q--){
  60. int x=read(),y=read(),k=read();
  61. printf("%d\n",hsh[req(,tot,root[x-],root[y],k)]);
  62. }
  63. return ;
  64. }

poj[2104]K-th Number的更多相关文章

  1. POJ 2104:K-th Number(主席树静态区间k大)

    题目大意:对于一个序列,每次询问区间[l,r]的第k大树. 分析: 主席树模板题 program kthtree; type point=record l,r,s:longint; end; var ...

  2. POJ 2104:K-th Number(整体二分)

    http://poj.org/problem?id=2104 题意:给出n个数和m个询问求区间第K小. 思路:以前用主席树做过,这次学整体二分来做.整体二分在yr大佬的指点下,终于大概懂了点了.对于二 ...

  3. POJ 2104:K-th Number 整体二分

    感觉整体二分是个很有趣的东西. 在别人的博客上看到一句话 对于二分能够解决的询问,如果有多个,那么如果支持离线处理的话,那么就可以使用整体二分了 树套树写了一天还是WA着,调得焦头烂额,所以决定学cd ...

  4. 【POJ 2104】 K-th Number 主席树模板题

    达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026 2016-02-23:真的是模板题诶,主席树模板水过.今天新校网不好,没 ...

  5. POJ 2104&HDU 2665 Kth number(主席树入门+离散化)

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

  6. K-th Number POJ - 2104

    K-th Number POJ - 2104 You are working for Macrohard company in data structures department. After fa ...

  7. K-th Number POJ - 2104 划分树

    K-th Number You are working for Macrohard company in data structures department. After failing your ...

  8. poj 2104 K-th Number 主席树+超级详细解释

    poj 2104 K-th Number 主席树+超级详细解释 传送门:K-th Number 题目大意:给出一段数列,让你求[L,R]区间内第几大的数字! 在这里先介绍一下主席树! 如果想了解什么是 ...

  9. poj 2104 K-th Number(主席树,详细有用)

    poj 2104 K-th Number(主席树) 主席树就是持久化的线段树,添加的时候,每更新了一个节点的线段树都被保存下来了. 查询区间[L,R]操作的时候,只需要用第R棵树减去第L-1棵树就是区 ...

  10. K-th Number Poj - 2104 主席树

    K-th Number Poj - 2104 主席树 题意 给你n数字,然后有m次询问,询问一段区间内的第k小的数. 解题思路 这个题是限时训练做的题,我不会,看到这个题我开始是拒绝的,虽然题意清晰简 ...

随机推荐

  1. 转载:《TypeScript 中文入门教程》 12、类型推导

    版权 文章转载自:https://github.com/zhongsp 建议您直接跳转到上面的网址查看最新版本. 介绍 这节介绍TypeScript里的类型推论.即,类型是在哪里如何被推断的. 基础 ...

  2. sql执行循序

    (8) select (9) distinct (11) top 1 (6) Table1.id,COUNT(Table1.name) as nameCount (1) from Table1 (3) ...

  3. [转载]C#使用Interlocked进行原子操作

    原文链接:王旭博客 » C# 使用Interlocked进行原子操作 什么是原子操作? 原子(atom)本意是"不能被进一步分割的最小粒子",而原子操作(atomic operat ...

  4. inline-block 空白间距问题

    一. 问题 元素是inline-block属性时,会有空白间隙 二. 解决方案 1. html方式 1)将元素之间的空隙去除 <div class="space"> & ...

  5. NSString 的常用操作

    NSString *testStr01=@"HelloWord"; NSString *testStr02=[testStr01 substringToIndex:];//取头(从 ...

  6. UITableView或UIScrollVIew上的UIButton的高亮效果

    UITableView或UIScrollVIew上的UIButton的高亮效果 原文地址:http://www.jianshu.com/p/b4331f06bd34 最近做项目的时候发现,UIScro ...

  7. iOS Swift-简单值(The Swift Programming Language)

    iOS Swift-简单值(The Swift Programming Language) 常量的声明:let 在不指定类型的情况下声明的类型和所初始化的类型相同. //没有指定类型,但是初始化的值为 ...

  8. 全新的membership框架Asp.net Identity(1)——.Net membership的历史

    在Asp.net上,微软的membershop框架经历了Asp.net membership到Asp.net simple membership,再到现在的Asp.net Identity. 每一次改 ...

  9. Linux LVM学习总结——删除物理卷

    本篇介绍LVM管理中的命令vgreduce, pvremove.其实前面几篇中以及有所涉及. vgreduce:通过删除LVM卷组中的物理卷来减少卷组容量.注意:不能删除LVM卷组中剩余的最后一个物理 ...

  10. 深入解析Windows操作系统笔记——CH2系统结构

    2.系统结构 本章主要介绍系统的总体结构,关键部件之间的交互,以及运行在什么环境. 2.系统结构 2.1 需求和设计目标 2.2 操作系统模型 2.3 总体结构 2.3.1 可移植性 2.3.2 对称 ...