K-th Number

持久化:http://www.cnblogs.com/tedzhao/archive/2008/11/12/1332112.html

结构:http://www.docin.com/p-627462377.html

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = ;
int n, m, tot;
int a[N], x[N], xcnt;
int T[N*], ls[N*], rs[N*], sum[N*]; void build(int l, int r ,int &rt)
{
rt=++tot;
sum[rt]=;
if(l==r) return ;
int m=(l+r)>>;
build(l, m, ls[rt]);
build(m+,r,rs[rt]);
} void update(int last, int p, int l, int r, int &rt)
{
rt = ++tot;
ls[rt]=ls[last], rs[rt]=rs[last], sum[rt]=sum[last]+;
if(l==r) return ;
int m = (l+r)>>;
if(p<=m) update(ls[last], p, l, m, ls[rt]);
else update(rs[last], p, m+, r, rs[rt]);
} int query(int ss, int tt, int l, int r, int k)
{
if(l==r) return l;
int m = (l+r)>>;
int num = sum[ls[tt]] - sum[ls[ss]];
if(k<=num) return query(ls[ss], ls[tt], l, m, k);
else return query(rs[ss], rs[tt], m+, r, k-num);
return ;
} void solve()
{
tot = ;
for(int i=; i<=n; i++)
{
scanf("%d", &a[i]);
x[i] = a[i];
}
sort(x+, x+n+);
xcnt = unique(x+, x++n)-x-;
for(int i=; i<=n; i++) a[i] = lower_bound(x+, x+n+, a[i])-x;
build(, xcnt, T[]);
for(int i=; i<=n; i++) update(T[i-],a[i],,xcnt,T[i]);
int l, r, k;
while(m--)
{
scanf("%d%d%d", &l, &r, &k);
printf("%d\n", x[query(T[l-],T[r],,xcnt,k)]);
}
} int main()
{
while(scanf("%d%d", &n, &m)> ) solve();
return ;
}

poj 2104 K-th Number(可持久线段树)的更多相关文章

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

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

  2. POJ 2828 Buy Tickets(排队问题,线段树应用)

    POJ 2828 Buy Tickets(排队问题,线段树应用) ACM 题目地址:POJ 2828 Buy Tickets 题意:  排队买票时候插队.  给出一些数对,分别代表某个人的想要插入的位 ...

  3. [BZOJ 3110] [luogu 3332] [ZJOI 2013]k大数查询(权值线段树套线段树)

    [BZOJ 3110] [luogu 3332] [ZJOI 2013]k大数查询(权值线段树套线段树) 题面 原题面有点歧义,不过从样例可以看出来真正的意思 有n个位置,每个位置可以看做一个集合. ...

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

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

  5. POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25904   Accepted: 7682 Descr ...

  6. poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

    A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...

  7. POJ - 2777——Count Color(懒标记线段树二进制)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 53639   Accepted: 16153 Des ...

  8. POJ 1151 Atlantis 矩形面积求交/线段树扫描线

    Atlantis 题目连接 http://poj.org/problem?id=1151 Description here are several ancient Greek texts that c ...

  9. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  10. #5 DIV2 A POJ 3321 Apple Tree 摘苹果 构建线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25232   Accepted: 7503 Descr ...

随机推荐

  1. HTTP协议(转)

    HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前在WWW中使用的是HTTP/1.0的第 ...

  2. a标签的target属性

    _blank 浏览器总在一个新打开.未命名的窗口中载入目标文档. _self 这个目标的值对所有没有指定目标的 <a> 标签是默认目标,它使得目标文档载入并显示在相同的框架或者窗口中作为源 ...

  3. 分享25个新鲜出炉的 Photoshop 高级教程

    网络上众多优秀的 Photoshop 实例教程是提高 Photoshop 技能的最佳学习途径.今天,我向大家分享25个新鲜出炉的 Photoshop 高级教程,提高你的设计技巧,制作时尚的图片效果.这 ...

  4. html4基础知识梳理

    基础的html知识,只放Xmind的截图. 第一部分: 第二部分: 某些标签的使用示例及注意事项,在印象笔记里.

  5. 最近喜欢听的英文歌——Because Of You - Kelly Clarkson

    没了解过歌曲背景,总觉得像是一首女儿唱给母亲的歌= =也许是我的错觉 I will not make the same mistakes that you did 我不会重复你犯过的错误 I will ...

  6. 三分钟玩转jQuery.noConflict()

      jQuery是目前使用最广泛的前端框架之一,有大量的第三方库和插件基于它开发.为了避免全局命名空间污染,jQuery提供了jQuery.noConflict()方法解决变量冲突.这个方法,毫无疑问 ...

  7. [javascript svg fill stroke stroke-width points polygon属性讲解] svg fill stroke stroke-width points polygon绘制多边形属性并且演示polyline和polygon区别讲解

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...

  8. Projects\Portal_Content\Indexer\CiFiles文件夹下文件占用磁盘空间过大问题。

    C:\Program Files\Microsoft Office Servers\12.0\Data\Office Server\Applications\9765757d-15ee-432c-94 ...

  9. [Android]实现类似微信的延迟加载的Fragment——LazyFragment

    以下内容为原创,转载请注明: 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4303910.html  参考微信,使用ViewPager来显示不同的t ...

  10. Android 4.4 KitKat 新特性

    New in Android 4.4 KitKat 本文是一个概览,关于KitKat,也即Android4.4的新东西,先是功能型的,之后是设计上的. 很多特性本文并没有提到,很多提到的特性也只是简短 ...