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


Sample Output


Hint

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

Solution

主席树模版题

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
inline int read(){
int x=,c=getchar(),f=;
for(;c<||c>;c=getchar())
if(!(c^))
f=-;
for(;c>&&c<;c=getchar())
x=(x<<)+(x<<)+c-;
return x*f;
}
int n,q,sz,tot,ls[],rs[],s[],a[],root[],num[],hsh[];
inline int bin(int x){
int l=,r=tot,mid;
for(;l<=r;){
mid=l+r>>;
if(hsh[mid]<x)
l=mid+;
else
r=mid-;
}
return l;
}
void ins(int l,int r,int x,int &y,int v){
y=++sz;
s[y]=s[x]+;
if(!(l^r))
return;
ls[y]=ls[x]; rs[y]=rs[x];
int mid=l+r>>;
if(v<=mid)
ins(l,mid,ls[x],ls[y],v);
else
ins(mid+,r,rs[x],rs[y],v);
}
int req(int l,int r,int x,int y,int k){
if(!(l^r))
return l;
int mid=l+r>>;
if(k<=s[ls[y]]-s[ls[x]])
return req(l,mid,ls[x],ls[y],k);
else
return req(mid+,r,rs[x],rs[y],k-s[ls[y]]+s[ls[x]]);
}
int main(){
n=read(),q=read();
for(int i=;i<=n;i++)
num[i]=a[i]=read();
sort(num+,num++n);
hsh[++tot]=num[];
for(int i=;i<=n;i++)
if(num[i]^num[i-])
hsh[++tot]=num[i];
for(int i=;i<=n;i++)
ins(,tot,root[i-],root[i],bin(a[i]));
while(q--){
int x=read(),y=read(),k=read();
printf("%d\n",hsh[req(,tot,root[x-],root[y],k)]);
}
return ;
}

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. 【linux草鞋应用编程系列】_6_ 重定向和VT100编程

    一.文件重定向     我们知道在linux shell 编程的时候,可以使用文件重定向功能,如下所示: [root@localhost pipe]# echo "hello world&q ...

  2. Cats(2)- Free语法组合,Coproduct-ADT composition

    上篇我们介绍了Free类型可以作为一种嵌入式编程语言DSL在函数式编程中对某种特定功能需求进行描述.一个完整的应用可能会涉及多样的关联功能,但如果我们为每个应用都设计一套DSL的话,那么在我们的函数式 ...

  3. SVG简介

    最近遇到SVG这个名词,于是查阅资料,做个笔记. 前言 图片的数字化.将图片存储为数据有两种方案. 位图.也被称为光栅图.即是以自然的光学的眼光将图片看成在平面上密集排布的点的集合.每个点发出的光有独 ...

  4. Dynamics XRM Tools 2015 2016

    Download Link: Dynamics XRM Tools 2015/2016 Overview Dynamics XRM Tools brings you a quality range o ...

  5. iOS之两个ImageView实现图片滚动

    原创作者:codingZero 导语 在不少项目中,都会有图片轮播这个功能,现在网上关于图片轮播的框架层出不穷,千奇百怪,笔者根据自己的思路,用两个imageView也实现了图片轮播,这里说说笔者的主 ...

  6. android重复的文件复制APK META-INF许可证错误记录

    * What went wrong:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.> c ...

  7. [转]Design Pattern Interview Questions - Part 4

    Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...

  8. iOS UIRefreshControl-刷新tableView

    override func viewDidLoad() {        super.viewDidLoad()        refreshControl = UIRefreshControl.in ...

  9. 【代码笔记】iOS-文字走马灯效果

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...

  10. JavaScript的个人学习随手记(二)

    JS HTML DOM 改变 HTML 输出流 JavaScript 能够创建动态的 HTML 内容: 今天的日期是: Sat Sep 24 2016 15:06:50 GMT+0800 (中国标准时 ...