题目链接:

http://poj.org/problem?id=2104

K-th Number

Time Limit: 20000MS
Memory Limit: 65536K
#### 问题描述
> 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.
#### 输入
> The first line of the input file contains n --- the size of the array, and m --- the number of questions to answer (1 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 For each question output the answer to it --- the k-th number in sorted a[i...j] segment.
####样例输入
> 7 3
> 1 5 2 6 3 7 4
> 2 5 3
> 4 4 1
> 1 7 3
####样例输出
> 5
> 6
> 3
## 题意
> 给你n个数m个询问,每个询问给你l,r,k,求(l,r)区间里第k大的数,保证每个数只出现一次。

题解

主席树裸板。

一个简洁形象的教程:[port]

代码:[port]

代码

本土化:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#include<sstream>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson(i) (tre[(i)].ls)
#define rson(i) (tre[(i)].rs)
#define sumv(i) tre[(i)].sum
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=1e5+10; ///nlogn空间复杂度
struct Tre{
int ls,rs,sum;
Tre(){ls=rs=sum=0;}
}tre[maxn*20]; int n,m;
int rt[maxn],tot; int _v;
void update(int &o,int l,int r){
tre[++tot]=tre[o],o=tot;
if(l==r){
sumv(o)++;
}else{
if(_v<=mid) update(lson(o),l,mid);
else update(rson(o),mid+1,r);
sumv(o)=sumv(lson(o))+sumv(rson(o));
}
} int _res;
void query(int o1,int o2,int l,int r,int k){
if(l==r){
_res=l;
}else{
///前缀和思想
int cnt=sumv(lson(o2))-sumv(lson(o1));
if(cnt>=k) query(lson(o1),lson(o2),l,mid,k);
else query(rson(o1),rson(o2),mid+1,r,k-cnt);
}
} int idx[maxn],arr[maxn],ra[maxn]; bool cmp(int x,int y){
return arr[x]<arr[y];
} ///0是个超级节点
void init(){
rt[0]=tot=0;
} int main() {
while(scf("%d%d",&n,&m)==2&&n){
init();
for(int i=1;i<=n;i++) scf("%d",&arr[i]);
for(int i=1;i<=n;i++) idx[i]=i; ///离散化
sort(idx+1,idx+n+1,cmp);
for(int i=1;i<=n;i++) ra[idx[i]]=i; ///主席树
for(int i=1;i<=n;i++){
_v=ra[i];
rt[i]=rt[i-1];
update(rt[i],1,n);
} ///查询区间第k大
while(m--){
int l,r,k;
scf("%d%d%d",&l,&r,&k);
query(rt[l-1],rt[r],1,n,k);
prf("%d\n",arr[idx[_res]]);
}
}
return 0;
} //end-----------------------------------------------------------------------

POJ 2104 K-th Number 主席树(区间第k大)的更多相关文章

  1. 【POJ 2104】 K-th Number 主席树模板题

    达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026 2016-02-23:真的是模板题诶,主席树模板水过.今天新校网不好,没 ...

  2. 主席树区间第K大

    主席树的实质其实还是一颗线段树, 然后每一次修改都通过上一次的线段树,来添加新边,使得每次改变就改变logn个节点,很多节点重复利用,达到节省空间的目的. 1.不带修改的区间第K大. HDU-2665 ...

  3. 牛客多校第九场H Cutting Bamboos(主席树 区间比k小的个数)题解

    题意: 标记为\(1-n\)的竹子,\(q\)个询问,每次给出\(l,r,x,y\).要求为砍区间\(l,r\)的柱子,要求砍\(y\)次把所有竹子砍完,每次砍的时候选一个高度,把比他高的都砍下来,并 ...

  4. HDU 6278 主席树(区间第k大)+二分

    Just h-index Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)To ...

  5. poj 2104 K-th Number 主席树+超级详细解释

    poj 2104 K-th Number 主席树+超级详细解释 传送门:K-th Number 题目大意:给出一段数列,让你求[L,R]区间内第几大的数字! 在这里先介绍一下主席树! 如果想了解什么是 ...

  6. Poj 2104 K-th Number(主席树&&整体二分)

    K-th Number Time Limit: 20000MS Memory Limit: 65536K Case Time Limit: 2000MS Description You are wor ...

  7. hdu 5919 主席树(区间不同数的个数 + 区间第k大)

    Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  8. zoj2112 树状数组+主席树 区间动第k大

    Dynamic Rankings Time Limit: 10000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Subm ...

  9. 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)

    Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...

随机推荐

  1. 【TOJ 2406】Power Strings(KMP找最多重复子串)

    描述 Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc& ...

  2. jQuery----获取兄弟元素的方法

    ① $(this).next():        获取的是当前元素的下一个兄弟元素 ②$(this).nextAll();       获取的是当前元素的后面的所有的兄弟元素 ③$(this).pre ...

  3. chrome浏览器使用jqprint插件打印时偶尔空白页问题

    最近测试老是提bug说是有50%的概率打印出空白页,之前我也一直发现偶尔会出现这个问题,只是一直没有发现原因. 今天终于下定决心找到问题所在.开始吧! 查看源码一行行debug,发现问题只可能出现在这 ...

  4. Android内核编译

    说明: 我这里用的是Android 4.2.2自带的交叉编译工具.如果是使用host系统安装的,我觉得应该也是可以的.但是没有测试过.因为Android源代码已经自带了,所以大多数情况下,不需要再安装 ...

  5. 17-[JavaScript]- 内置对象 Array,String,Date,Math

    1.Array数组 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  6. spark-sql用hive表格,在spark-submit运行jar包时遇到的问题

    1.编程时无法加载hive包,需要在编译好的spark(用spark-shell启动,用spark-sql能够直接访问hive表)的lib目录下,考出assembly包,为其创建一个maven的rep ...

  7. css绘制常见的几何图形

    前言:终于我的大一生活结束了,迎来了愉快的暑假,大家都开始了各自的忙碌.一直忙着一些项目的事情,终于决定今天要更新一篇博客了,对上一阶段的学习做简单的总结. 这次我主要总结一下用Css绘制各种形状的技 ...

  8. 开源项目CIIP(企业信息管理系统框架).2018.0904版更新介绍

    源码: https://github.com/tylike/CIIP https://gitee.com/ciip/CIIP 一,CIIP的目标是什么? 更加简单,快速的建立信息类管理系统.让实施人员 ...

  9. TCP Over HTTP 的Buffer问题

    记录下备忘. 场景:要把TCP拆成一个个HTTP请求,通过Proxy 1.HTTP Client上载数据到CCProxy ,然后再到Web服务器的时候. 如果数据量比较小,例如10个字节,Proxy就 ...

  10. 手动搭建openstack的痛苦经历

    openstack真的是一个十分痛苦的东西,好在有自动部署工具,虽然有自动部署工具可以方便我们部署使用,但是学习的话,第一次最好手动部署,因为手动部署更能我们了解openstack的工作流程和各组建之 ...