K-th Number

Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 58759   Accepted: 20392
Case Time Limit: 2000MS

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.

Source

Northeastern Europe 2004, Northern Subregion
 
 //2017-08-07
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
#define mid ((l+r)>>1) using namespace std; const int N = ;
const int M = N * ;
struct node{//第i棵线段树的节点维护插入i个数字,每个区间的数字个数。
int lson, rson, sum;
}tree[M];
int root[N], arr[N], arr2[N], tot;
int n, m, q; void init(){//将原数列排序并去重
tot = ;
for(int i = ; i <= n; i++)
arr2[i] = arr[i];
sort(arr2+, arr2++n);
m = unique(arr2+, arr2++n)-arr2-;
} int getID(int x){
return lower_bound(arr2+, arr2++m, x) - arr2;
} int build(int l, int r){
int rt = tot++;
tree[rt].sum = ;
if(l != r){
tree[rt].lson = build(l, mid);
tree[rt].rson = build(mid+, r);
}
return rt;
} int update(int rt, int pos, int value){
int newroot = tot++, tmp = newroot;
tree[newroot].sum = tree[rt].sum + value;
int l = , r = m;
while(l < r){
if(pos <= mid){
tree[newroot].lson = tot++;
tree[newroot].rson = tree[rt].rson;
newroot = tree[newroot].lson;
rt = tree[rt].lson;
r = mid;
}else{
tree[newroot].rson = tot++;
tree[newroot].lson = tree[rt].lson;
newroot = tree[newroot].rson;
rt = tree[rt].rson;
l = mid+;
}
tree[newroot].sum = tree[rt].sum + value;
}
return tmp;
} int query(int lroot, int rroot, int k){
int l = , r = m;
while(l < r){
if(tree[tree[lroot].lson].sum - tree[tree[rroot].lson].sum >= k){
r = mid;
lroot = tree[lroot].lson;
rroot = tree[rroot].lson;
}else{
l = mid + ;
k -= tree[tree[lroot].lson].sum - tree[tree[rroot].lson].sum;
lroot = tree[lroot].rson;
rroot = tree[rroot].rson;
}
}
return l;
} int main()
{
while(scanf("%d%d", &n, &q)!=EOF){
for(int i = ; i <= n; i++)
scanf("%d", &arr[i]);
init();
root[n+] = build(, m);
for(int i = n; i > ; i--){
int pos = getID(arr[i]);
root[i] = update(root[i+], pos, );
}
while(q--){
int l, r, k;
scanf("%d%d%d", &l, &r, &k);
printf("%d\n", arr2[query(root[l], root[r+], k)]);
}
} return ;
}

POJ2104(可持久化线段树)的更多相关文章

  1. [poj2104]可持久化线段树入门题(主席树)

    解题关键:离线求区间第k小,主席树的经典裸题: 对主席树的理解:主席树维护的是一段序列中某个数字出现的次数,所以需要预先离散化,最好使用vector的erase和unique函数,很方便:如果求整段序 ...

  2. POJ- 2104 hdu 2665 (区间第k小 可持久化线段树)

    可持久化线段树 也叫函数式线段树也叫主席树,其主要思想是充分利用历史信息,共用空间 http://blog.sina.com.cn/s/blog_4a0c4e5d0101c8fr.html 这个博客总 ...

  3. 【可持久化线段树】POJ2104 查询区间第k小值

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

  4. [POJ2104] 区间第k大数 [区间第k大数,可持久化线段树模板题]

    可持久化线段树模板题. #include <iostream> #include <algorithm> #include <cstdio> #include &l ...

  5. 主席树(可持久化线段树) 静态第k大

    可持久化数据结构介绍 可持久化数据结构是保存数据结构修改的每一个历史版本,新版本与旧版本相比,修改了某个区域,但是大多数的区域是没有改变的, 所以可以将新版本相对于旧版本未修改的区域指向旧版本的该区域 ...

  6. PYOJ 44. 【HNSDFZ2016 #6】可持久化线段树

    #44. [HNSDFZ2016 #6]可持久化线段树 统计 描述 提交 自定义测试 题目描述 现有一序列 AA.您需要写一棵可持久化线段树,以实现如下操作: A v p x:对于版本v的序列,给 A ...

  7. 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集

    3673: 可持久化并查集 by zky Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 1878  Solved: 846[Submit][Status ...

  8. 【BZOJ-2653】middle 可持久化线段树 + 二分

    2653: middle Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1298  Solved: 734[Submit][Status][Discu ...

  9. HDU 4866 Shooting(持久化线段树)

    view code//第二道持久化线段树,照着别人的代码慢慢敲,还是有点不理解 #include <iostream> #include <cstdio> #include & ...

随机推荐

  1. cad.net之ACAD移植到GCAD的自动加载问题

    将acad.pgp,lsp,fas,vlx,名称增加一份gcad.pgp,lsp,fas,vlx.涉及系统加载用. Lisp的拖拉加载在gcad无法通过lastprompt获取命令历史栏最后一行(含路 ...

  2. canvas制作完美适配分享海报

    基于mpvue实现的1080*1900小程序海报 html   <canvas class="canvas" :style="'width:'+windowWidt ...

  3. JAVA常见安全问题复现

    地址来源于乌云知识库,作者z_zz_zzz 0x01 任意文件下载 web.xml的配置: <servlet> <description></description> ...

  4. iOS-iOS9系统SEGV_ACCERR问题处理【v3.6.3的一些bug修复】

    前言 最近APP不断地更新版本,却发现一些未知的错误导致崩溃,我把能测出来的错误,全部修复了,因为项目里集成了腾讯Bugly,看了下后台的崩溃,依旧千篇一律啊,然后就纠结了,很多SEGV_ACCERR ...

  5. Scala之隐式转换implicit详解

    假设我们有一个表示文本的行数的类LineNumber: class LineNumber ( val num : Int ) 我们可以用这个类来表示一本书中每一页的行数: val lineNumOfP ...

  6. 线程同步辅助类CyclicBarrier

    CyclicBarrier 是一个可重置的多路同步点,在某些并行编程风格中很有用. 集合点同步:CyclicBarrier 多条线程同时执行一个阶段性任务时,相互等待,等到最后一个线程执行完阶段后,才 ...

  7. (转)每天一个linux命令(21):find命令之xargs

    原文:http://www.cnblogs.com/peida/archive/2012/11/15/2770888.html https://blog.csdn.net/ly1358152944/a ...

  8. (转)contextlib — 上下文管理器工具

    原文:https://pythoncaff.com/docs/pymotw/contextlib-context-manager-tool/95 这是一篇社区协同翻译的文章,你可以点击右边区块信息里的 ...

  9. 无监督学习——K-均值聚类算法对未标注数据分组

    无监督学习 和监督学习不同的是,在无监督学习中数据并没有标签(分类).无监督学习需要通过算法找到这些数据内在的规律,将他们分类.(如下图中的数据,并没有标签,大概可以看出数据集可以分为三类,它就是一个 ...

  10. JavaScript -- Window-状态栏

    -----024-Window-状态栏.html----- <!DOCTYPE html> <html> <head> <meta http-equiv=&q ...