#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 1e5+5; int N,M;
int A[MAXN]; int has[MAXN];
int htot;
int get(int x){
int tt = lower_bound(has,has+htot,x)-has;
return tt+1;
}
int show(int x){
return has[x-1];
} struct Node{
int ls; int rs; int sum;
}tree[MAXN*30];
int root[MAXN];
int tot; int Ed;
int Build(int l,int r) {
int rt = tot++;
tree[rt].sum = 0;
if(l == r) return rt;
int m = (l+r) >>1;
tree[rt].ls = Build(l,m);
tree[rt].rs = Build(m+1,r);
return rt;
}
int Update(int pos,int num,int l,int r,int pre) {
int rt = tot++;
tree[rt] = tree[pre];
tree[rt].sum += num;
if(l == r) return rt;
int m = (l+r) >>1;
if(pos <= m) tree[rt].ls = Update(pos,num,l,m,tree[pre].ls);
else tree[rt].rs = Update(pos,num,m+1,r,tree[pre].rs);
return rt;
} int Qans;
void Query(int num,int l,int r,int rt1,int rt2) {
if(l == r) {
Qans = r; return;
}
int m = (l + r) >>1;
int tt = tree[tree[rt2].ls].sum - tree[tree[rt1].ls].sum;
if(tt >= num) Query(num,l,m,tree[rt1].ls,tree[rt2].ls);
else Query(num-tt, m+1, r, tree[rt1].rs,tree[rt2].rs);
}
void Debug(int l,int r,int rt){
printf("%d %d %d\n",l,r,tree[rt].sum);
if(l == r) {
return;
}
int m = (l+r) >>1;
Debug(l,m,tree[rt].ls); Debug(m+1,r,tree[rt].rs);
}
int main(){
while(~scanf("%d %d",&N,&M)) {
htot = 0;
for(int i = 1; i <= N; ++i) { scanf("%d",&A[i]); has[htot ++] = A[i]; }
sort(has,has+htot);
htot = unique(has, has+htot) - has;
Ed = htot; tot = 0; root[0] = Build(1,Ed);
for(int i = 1; i <= N; ++i) {
int tt = get(A[i]);
root[i] = Update(tt, 1, 1, Ed, root[i-1]);
}
// Debug(1,Ed,root[5]); printf("\n");
// for(int i = 1; i <= N; ++i) printf("%d ",root[i]); printf("\n"); for(int i = 1; i <= M; ++i) {
int a,b,c; scanf("%d %d %d",&a,&b,&c);
Query(c,1,Ed,root[a-1],root[b]);
printf("%d\n",show(Qans) );
}
}
return 0;
}

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. (转)Elasticsearch 5 Ik+pinyin分词配置详解

    今天以这篇文章结束同城旅游网的面试,正好面试官也问到站内检索,可以尝试一下这篇文章介绍的方法.Elasticsearch 5 Ik+pinyin分词配置详解

  2. BZOJ 3990: [SDOI2015]排序 [搜索]

    3990: [SDOI2015]排序 题意:\(2^n\)的一个排列,给你n种操作,第i种把每\(2^{i-1}\)个数看成一段,交换任意两段.问是这个序列有序的操作方案数,两个操作序列不同,当且仅当 ...

  3. CodeChef Chef and Churu [分块]

    题意: 单点修改$a$ 询问$a$的区间和$f$的区间和 原来普通计算机是这道题改编的吧... 对$f$分块,预处理$c[i][j]$为块i中$a_j$出现几次,$O(NH(N))$,只要每个块差分加 ...

  4. BZOJ 3173: [Tjoi2013]最长上升子序列 [splay DP]

    3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1613  Solved: 839[Submit][St ...

  5. CDN设置回源host的意义

    CDN设置回源host的意义 如果CDN后端用户的的源站web服务上没有绑定加速域名,只绑定了其他域名,未限制域名访问(比如通过服务器IP可以访问到默认网站),可以在CDN控制台填写回源host,这样 ...

  6. ionic2+Angular 依赖注入之Subject ——使用Subject来实现组件之间的通信

    在Angular+ionic2 开发过程中,我们不难发现,页面之间跳转之后返回时是不会刷新数据的. 场景一:当前页面需要登录之后才能获取数据--去登录,登录成功之后返回--页面需要手动刷新才能获取到数 ...

  7. [Python Study Notes]实现对鼠标控制

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  8. Math Jax开源数学编辑器的使用

    首先,这是一个开源免费,同时也可以支持扩展的软件. 使用API文档: 中文网站(http://mathjax-chinese-doc.readthedocs.io/en/latest/index.ht ...

  9. LVS的NAT模式测试

    dir 分别配置ip  eth0 :10.222.138.200   eth0:1 10.222.21.190 rs1 eth0  :10.222.138.201 rs2 eth0: 10.222.1 ...

  10. MSSql Server 批量插入数据优化

    针对批量入库, .Net Framework  提供了一个批量入库Class : SqlBulkCopy , 批量入库性能不错,经测试 四万左右数据 2秒入库. 以下是测试Demo , 使用外部传入事 ...