/*K-th Number
Time Limit: 20000MS Memory Limit: 65536K
Total Submissions: 44535 Accepted: 14779
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*/
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int ls[8000008],rs[8000008],sum[8000008],root[100008],num[100009],n,m,hash[100009];
int tmp,size,a[100008];
void jia(int l,int r,int x,int &y,int v)
{
y=++size;
sum[y]=sum[x]+1;
if(l==r)
return;
ls[y]=ls[x];
rs[y]=rs[x];
int mid=(l+r)>>1;
if(v<=mid)
jia(l,mid,ls[x],ls[y],v);
else
jia(mid+1,r,rs[x],rs[y],v);
return;
}
int xun(int L,int R,int V)
{
int x=root[L-1],y=root[R],l=1,r=tmp,mid=(l+r)/2;
for(;l!=r;)
if(sum[ls[y]]-sum[ls[x]]>=V)
{
x=ls[x];
y=ls[y];
r=mid;
mid=(l+r)>>1;
}
else
{
V-=sum[ls[y]]-sum[ls[x]];
x=rs[x];
y=rs[y];
l=mid+1;
mid=(l+r)>>1;
}
return l;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<n; i++)
{
scanf("%d",&num[i]);
a[i+1]=num[i];
}
sort(num,num+n);
hash[++tmp]=num[0];
for(int i=1;i<n;i++)
if(hash[tmp]!=num[i])
hash[++tmp]=num[i];
for(int i=1;i<=n;i++)
jia(1,tmp,root[i-1],root[i],lower_bound(hash+1,hash+n+1,a[i])-hash);
for(int i=0;i<m;i++)
{
int l,r,v;
scanf("%d%d%d",&l,&r,&v);
printf("%d\n",hash[xun(l,r,v)]);
}
return 0;
}

主席树K-th Number的更多相关文章

  1. 【静态主席树】POJ2104-K-th Number

    求区间第k大.裸线段树. 莫队版本:☆ #include<iostream> #include<cstdio> #include<cstring> #include ...

  2. 可持久化线段树(主席树)快速简洁教程 图文并茂 保证学会。kth number例题

    如果学不会也不要打我. 假设你会线段树 开始! --- 主席树也叫可持久化线段树 顾名思义,它能够保存线段树在每个时刻的版本. 什么叫每个时刻的版本?你可能对一棵普通线段树进行各种修改,这每种样子就是 ...

  3. bzoj3932 / P3168 [CQOI2015]任务查询系统(主席树+差分)

    P3168 [CQOI2015]任务查询系统 看到第k小,就是主席树辣 对于每一段任务(a,b,k),在版本a的主席树+k,版本b+1的主席树-k 同一时间可能有多次修改,所以开个vector存操作, ...

  4. 【POJ】2104 K-th Number(区间k大+主席树)

    http://poj.org/problem?id=2104 裸题不说.主席树水过. #include <cstdio> #include <iostream> #includ ...

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

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

  6. HDU 2665 Kth number(主席树静态区间第K大)题解

    题意:问你区间第k大是谁 思路:主席树就是可持久化线段树,他是由多个历史版本的权值线段树(不是普通线段树)组成的. 具体可以看q学姐的B站视频 代码: #include<cmath> #i ...

  7. A - 低阶入门膜法 - K-th Number (主席树查询区间第k小)

    题目链接:https://cn.vjudge.net/contest/284294#problem/A 题目大意:主席树查询区间第k小. 具体思路:主席树入门. AC代码: #include<i ...

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

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

  9. [POJ2104] K – th Number (可持久化线段树 主席树)

    题目背景 这是个非常经典的主席树入门题--静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个正整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入输 ...

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

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

随机推荐

  1. vue 下拉刷新数据的插件的使用:

    1.安装: npm i vue-scroller -S npm install vue-scroller -D 2.在需要加载的页面中引入,或在公共js文件中引入: import VueScrolle ...

  2. javascript-state-machine

    import StateMachine from 'javascript-state-machine' import EventEmitter from 'events' /** * 上传的文档的状态 ...

  3. Spring Cloud Alibaba学习笔记(15) - 整合Spring Cloud Gateway

    Spring Cloud Gateway 概述 Spring Cloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于Netty.Reactor以及WEbFlux构建,它 ...

  4. RVA与RWA的关系

    RVA与RWA的关系 原理比较简单:首先判断这个地址是否在PE头中,如果在,文件偏移和内存偏移相等,如果存在于文件的区段中,则利用以下公式: 内存偏移 - 该段起始的RVA(VirtualAddres ...

  5. java.lang.RuntimeException: Unable to instantiate activity ComponentInfo异常解决

    不能实例化activity有如下三种情况: 1.没有在Manifest.xml 清单中注册该activity,或者在创建完activity后,修改了包名或者activity的类名,而配置清单中没有修改 ...

  6. 3.asp.net core 关键概念

    1. StartUp类 在Startup.ConfigureServices方法里配置或注册服务 在Startup.Configure方法里配置请求处理管道.请求处理管道由一系列中间件组建构成,每个中 ...

  7. ReLU函数的缺陷

    ReLU激活功能并不完美. 它有一个被称为 “ReLU 死区” 的问题:在训练过程中,一些神经元会“死亡”,即它们停止输出 0 以外的任何东西.在某些情况下,你可能会发现你网络的一半神经元已经死亡,特 ...

  8. 艾瑞克·弗洛姆 ( Erich Fromm )

    艾瑞克·弗洛姆 ( Erich Fromm ) 来源 https://www.zhihu.com/question/22891103 ------------------------------ 作者 ...

  9. 拦截器中获取不到controller注解问题

    刚刚在测试接口的时候发现一个奇怪的问题:通过拦截器获取 controller 类注解,有些能获取到,有些又不能获取到,见鬼了. [环境]: 1. springboot :2.2.0.RELEASE [ ...

  10. map函数的应用

    map函数在Python中的应用 函数介绍: map() 会根据提供的函数对指定序列做映射. map(function, iterable, ...) 第一个参数function 以参数序列中的每一个 ...