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

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

Sample Output

5
6
3

Hint

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

【思路】

  主席树 

  戳这 click here

【代码】

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; typedef long long ll;
const int N = 1e5+;
const int M = *1e6+; int n,m,tot,sz;
int v[N],hash[N],root[N];
int sum[M],ls[M],rs[M]; ll read() {
char c=getchar();
ll x=,f=;
while(!isdigit(c)) {
if(c=='-') f=-; c=getchar();
}
while(isdigit(c))
x=x*+c-'',c=getchar();
return x*f;
}
void update(int l,int r,int x,int& y,int num)
{
y=++sz;
sum[y]=sum[x]+;
if(l==r) return ;
ls[y]=ls[x],rs[y]=rs[x];
int mid=(l+r)>>;
if(num<=mid) update(l,mid,ls[x],ls[y],num);
else update(mid+,r,rs[x],rs[y],num);
}
int query(int x,int y,int rk)
{
int a=root[x-],b=root[y];
int l=,r=tot;
while(l<r) {
int mid=(l+r)>>;
int now=sum[ls[b]]-sum[ls[a]];
if(rk<=now) r=mid,a=ls[a],b=ls[b];
else l=mid+,rk-=now,a=rs[a],b=rs[b];
}
return hash[l];
}
int main() {
n=read(),m=read();
FOR(i,,n)
v[i]=read(),hash[i]=v[i];
sort(hash+,hash+n+);
tot=unique(hash+,hash+n+)-hash-;
FOR(i,,n)
v[i]=lower_bound(hash+,hash+tot+,v[i])-hash;
FOR(i,,n) {
update(,tot,root[i-],root[i],v[i]);
}
int x,y,z;
FOR(i,,m) {
x=read(),y=read(),z=read();
printf("%d",query(x,y,z));
if(i!=m) puts("");
}
return ;
}

poj 2104 K-th Number(主席树)的更多相关文章

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

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

  2. 静态区间第k大(主席树)

    POJ 2104为例(主席树入门题) 思想: 可持久化线段树,也叫作函数式线段树,也叫主席树(高大上). 可持久化数据结构(Persistent data structure):利用函数式编程的思想使 ...

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

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

  4. poj2104 k-th number 主席树入门讲解

    poj2104 k-th number 主席树入门讲解 定义:主席树是一种可持久化的线段树 又叫函数式线段树   刚开始学是不是觉得很蒙逼啊 其实我也是 主席树说简单了 就是 保留你每一步操作完成之后 ...

  5. POJ 2104 K-th Number 主席树(区间第k大)

    题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...

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

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

  7. POJ 2104 K-th Number ( 求取区间 K 大值 || 主席树 || 离线线段树)

    题意 : 给出一个含有 N 个数的序列,然后有 M 次问询,每次问询包含 ( L, R, K ) 要求你给出 L 到 R 这个区间的第 K 大是几 分析 : 求取区间 K 大值是个经典的问题,可以使用 ...

  8. SPOJ MKTHNUM & POJ 2104 - K-th Number - [主席树模板题]

    题目链接:http://poj.org/problem?id=2104 Description You are working for Macrohard company in data struct ...

  9. poj 2104 K-th Number(主席树 视频)

    K-th Number 题意: 给你一些数,让你求一个区间内,第k大的数是多少. 题解: 主席树第一题,看的qsc视频写的,戳戳戳 学到了unique函数,他的作用是:把相邻的重复的放到后面,返回值是 ...

  10. Poj 2104 K-th Number(主席树&&整体二分)

    K-th Number Time Limit: 20000MS Memory Limit: 65536K Case Time Limit: 2000MS Description You are wor ...

随机推荐

  1. linux常见命令的列表

    http://www.pixelbeat.org/cmdline_zh_CN.html 命令 描述 • apropos whatis 显示和word相关的命令. 参见线程安全 • man -t man ...

  2. 1015: [JSOI2008]星球大战starwar - BZOJ

    Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...

  3. HIBERNATE一对一双向外键联合主键关联

    HIBERNATE一对一双向外键联合主键关联: 一. 创建主键类:这个主键必须实现serializedable接口和重写其中的hashCode方法和equals方法:为主键类添加一个叫做@Embedd ...

  4. C#跳出循环的几种方法的区别

    break是循环结束执行,执行循环体后面的代码. continue是跳过本次循环未执行的代码,继续执行下一次循环. goto是跳到指定的指令去,你指哪,他跳到哪. return是函数返回,如果循环在M ...

  5. spoj 394

    每段可以连续的串的可能性是个Fibonacci数列   但是直接dp更好吧~~ #include <cstdio> #include <cstring> using names ...

  6. 软考类----编码、ASII码等

    淘米2014实习生笔试,今年是淘米第一年招暑期实习生,笔试好大部分考的是软考的题目啊啊啊啊(劳资后悔当年没考软考刷加权),其他是浅而泛的风格,C++,SQL语句,数据结构(哈夫曼树,二叉查找树,栈后缀 ...

  7. Android ViewPager的每个页面的显示与销毁的时机

    大家在用viewPager的时候要创建一个pagerAdapter对象,用于给viewPager设置页面的. viewPager里面有一个container容器. viewPager的容器缓存3个显示 ...

  8. linux中class_create和class_register说明

    http://blog.csdn.net/angle_birds/article/details/16802099 本文介绍linux中class_create和class_register的相关使用 ...

  9. 如何用Java语言向串口读写数据

    原作者:赛迪网作者 shihuchen ,我在他的基础上进行了部分修改 [赛迪网讯]串口, RS-232-C(又称EIA RS-232-C,以下简称RS232)是在1970年由美国电子工业协会(EIA ...

  10. 在自己的网站上实现QQ授权登录

    最近在实现QQ授权登录,现将我的实现过程以及我的理解整理如下.以下所述如有不对之处,请指正. 官方提供的SDK有:JS,PHP,Java.我的网站使用Scala+Play搭建的,所以只能用JS SDk ...