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.

题意:给你一个大小为n的序列和m个询问,每次询问求[L,R]区间第k大的值。

正解:主席树或整体二分

以前一直觉得主席树是一个很高深的东西,自己写了以后才知道原来主席树代码极短。。

建n颗值域线段树,第i颗线段树保存1-i对应区间的结点个数,如根结点为1-maxa的个数,左右儿子分别二分得到。

那么,这颗主席树就能满足前缀和的性质,在查询时,只需将第r颗树上的贡献减去第l-1颗树上的贡献就好。查询贡献时,判断当前两结点之差与k的关系。如果当前差<=k,那么就查询左子树,否则查询右子树,查询到叶子结点时就得出答案。。

还有一个问题,如果真的直接开n颗线段树肯定会MLE,那么我们可以利用可持久化技术,每次插入一棵线段树时只加入与之前的树不同的一条链,其他的结点不变,那么我们每次插入的空间复杂度为O(logN),总复杂度为O(NlogN),可以接受。

不多说了,看代码吧。。

 //It is made by wfj_2048~
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define inf 1<<30
#define il inline
#define RG register
#define ll long long
#define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout) using namespace std; int root[],a[],num[],hash[],s[],ls[],rs[],n,m,sz,tot; il int gi(){
RG int x=,q=; RG char ch=getchar();
while ((ch<'' || ch>'') && ch!='-') ch=getchar(); if (ch=='-') q=,ch=getchar();
while (ch>='' && ch<='') x=x*+ch-,ch=getchar(); return q ? -x : x;
} il void insert(RG int l,RG int r,RG int x,RG int &y,RG int k){
y=++sz,s[y]=s[x]+; if (l==r) return; ls[y]=ls[x],rs[y]=rs[x]; RG int mid=(l+r)>>;
if (k<=mid) insert(l,mid,ls[x],ls[y],k); else insert(mid+,r,rs[x],rs[y],k); return;
} il int ask(RG int l,RG int r,RG int x,RG int y,RG int k){
while (l<r){
RG int mid=(l+r)>>;
if (s[ls[y]]-s[ls[x]]>=k) r=mid,x=ls[x],y=ls[y];
else l=mid+,k-=s[ls[y]]-s[ls[x]],x=rs[x],y=rs[y];
}
return l;
} il void work(){
n=gi(),m=gi(); for (RG int i=;i<=n;++i) num[i]=a[i]=gi(); sort(num+,num+n+);
hash[++tot]=num[]; for (RG int i=;i<=n;++i) if (num[i]!=num[i-]) hash[++tot]=num[i];
for (RG int i=;i<=n;++i) insert(,tot,root[i-],root[i],lower_bound(hash+,hash+tot+,a[i])-hash);
for (RG int i=;i<=m;++i){ RG int l=gi(),r=gi(),k=gi(); printf("%d\n",hash[ask(,tot,root[l-],root[r],k)]); }
return;
} int main(){
File("kth");
work();
return ;
}

poj2104 Kth-Number的更多相关文章

  1. POJ2104 K-th Number —— 区间第k小 整体二分

    题目链接:https://vjudge.net/problem/POJ-2104 K-th Number Time Limit: 20000MS   Memory Limit: 65536K Tota ...

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

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

  3. POJ2104 K-th Number [整体二分]

    题目传送门 K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 69053   Accepted: 24 ...

  4. POJ2104 K-th Number(主席树)

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

  5. POJ2104 K-th Number[主席树]【学习笔记】

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

  6. [POJ2104]K-th Number

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

  7. [poj2104] K-th Number (主席树)

    主席树 Description You are working for Macrohard company in data structures department. After failing y ...

  8. 主席树:POJ2104 K-th Number (主席树模板题)

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

  9. POJ2104 K-th Number(线段树)

    题目链接 K-th Number #include <cstdio> #include <cstring> #include <iostream> #include ...

  10. POJ2104 K-th Number (子区间内第k大的数字)【划分树算法模板应用】

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

随机推荐

  1. ELK5.0安装教程

    ELK升级后,安装稍微发生了点变化,在Elasticsearch中增加了很多资源上的限制,其他的倒是没什么变化.不过所有的安装都是基于JDK已经安装完的情况,且为1.8版本. 安装Elasticsea ...

  2. 使用Func<>和Action简化委托

    /// <summary> /// 入口 /// </summary> public void Run() { TestDelegate t = test; t(); Acti ...

  3. Android两种为ViewPager+Fragment添加Tab的方式

    在Android开发中ViewPager的使用是非常广泛的,而它不仅仅能够实现简单的开始引导页,还可以结合Fragment并添加Tab作为选项卡或为显示大批量页面实现强大的顺畅滑动 下面介绍两种为Vi ...

  4. windows系统下安装composer

    使用安装程序安装 这是将 Composer 安装在你机器上的最简单的方法. 下载并且运行 Composer-Setup.exe,它将安装最新版本的 Composer 安装完成后,将composer的b ...

  5. 修复关于apache-xampp的问题:Port 443 in use by “vmware-hostd.exe”!

    内容提要:复关于apache-xampp的问题:Port 443 in use by “vmware-hostd.exe”!在电脑里装了VMware后,再要装xampp,十有八九就会出现这个问题: 复 ...

  6. js,jq获取元素位置属性及兼容性写法

    网页被卷起来的高度/宽度 document.documentElement.scrolltop //火狐 和 其他浏览器 document.body.scrolltop   //ie,谷歌浏览器和没有 ...

  7. Page-Object设计模式

    自动化脚本初写之际一定是只求完成功能测试,页面by.id.by.name.by.xpath满篇飞.业务逻辑代码重复率也是越来越高.慢慢的写着写着开始重构,开始封装一些方法.代码量好一些的人会在代码开始 ...

  8. CentOS 7 Root用户密码重置 2017-04-02

    跨平台系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#linux 开机的时候按e (如果正在使用,你可以输入reboot,然后赶紧按回车键,也可 ...

  9. [SinGuLaRiTy] 树形存储结构阶段性测试

    [SinGuLaRiTy-1011] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. G2019级信息奥赛专项训练 题目 程序名 时间 内存 ...

  10. WPF 截屏软件开发

    最近由于工程需要开始研发基于Windows的自动录屏软件,很多细节很多功能需要处理,毕竟一个完美的录屏软件不是你随随便便就可以写出来的.首先参考了大部分的录屏软件,在研发的过程中遇到了很多的问题:比如 ...