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. Tomcat 8080端口被占用解决方法

    使用lsof命令查看端口占用情况 sudo lsof -i:8080 端口占用情况 java 1564 tomcat8 50u IPv6 19336 0t0 TCP *:http-alt (LISTE ...

  2. 用Fiddler做为手机的上网代理

    1. 首先需要知道你电脑的ip地址,这个略. 2. 打开手机,wifi那里,选择你的路由器,编辑或者设置,每个手机叫法不一样,点进去 到了里面,每个手机不一样,但是大致上,有一个[使用代理]的这么一个 ...

  3. java泛型详解

    http://www.cnblogs.com/lwbqqyumidi/p/3837629.html

  4. ABP 初探 之User、Role、Permission数据库设计 (EntityFramework 继承的另一种使用方法)

    最近群里(134710707)的朋友都在讨论ABP源码,我把最近学习的内容记录下来,同时也分享给大家,希望正在研究ABP源码的朋友有一定帮助. 上篇介绍ABP的多语言,本篇主要介绍权限的数据库设计,用 ...

  5. jQuery:详解jQuery中的事件(二)

    上一篇讲到jQuery中的事件,深入学习了加载DOM和事件绑定的相关知识,这篇主要深入讨论jQuery事件中的合成事件.事件冒泡和事件移除等内容. 接上篇jQuery:详解jQuery中的事件(一) ...

  6. VS 快捷键

    项目相关的快捷键 Ctrl + Shift + B = 生成项目 Ctrl + Alt + L = 显示Solution Explorer(解决方案资源管理器) Shift + Alt+ C = 添加 ...

  7. gulp安装说明

    1.安装node-v6.3.0-x64,安装成功后再点击node-v6.3.0-x64卸载(点击remove). 2.安装node-v4.4.7-x64. 3.打开cmd命令行,输入node -v,查 ...

  8. 走进SVG

    什么是SVG?也许现在很多人都听说过SVG的人比较多,但不一定了解什么是SVG:SVG(Scalable Vector Graphics 一大串看不懂的英文)可伸缩矢量图形,它是用XML格式来定义用于 ...

  9. AO安装需要Microsoft Visual Studio 2013?

    从接触ArcGIS9.2到 10.4,在不断升级的 过程中,既给我们带来了很多惊喜,也带来一些麻烦,因为ArcGIS版本不兼容.出于体验,安装了ArcGIS Desktop10.4,AO也得升到10. ...

  10. MS SQL 需要定期清理日志文件

    前言碎语 关于对SQL SERVER 日志文件管理方面了解不多的话,可以参考我的这篇博客文章“MS SQL 日志记录管理”,不过这篇文章只是介绍对SQL SERVER日志记录的深入认知了解,并没有提出 ...