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. [Tool] 取得APP的Store URL Scheme (Android、iOS)

    [Tool] 取得APP的Store URL Scheme (Android.iOS) 前言 在企业网站中,如果希望使用URL连结的方式,开启Store APP来下载APP(非网页下载).开发人员可以 ...

  2. JavaScriptOO.com – 快速找到你需要的 JS 框架

    JavaScriptOO.com 集合了目前 Web 开发中最常用的422(截至目前)款 JavaScript 框架,你可以根据功能类别(Ajax,动画,图表,游戏等)进行过滤和排序,快速找到你需要的 ...

  3. 15个最佳的代码评审(Code Review)工具

    代码评审可以被看作是计算机源代码的测试,它的目的是查找和修复引入到开发阶段的应用程序的错误,提高软件的整体素质和开发者的技能.代码审查程序以各种形式,如结对编程,代码抽查等.在这个列表中,我们编制了1 ...

  4. CSS3 使用自定义字体

    CSS3 @font-face 规则 在 CSS3 之前,web 设计师必须使用已在用户计算机上安装好的字体.通过 CSS3,web 设计师可以使用他们喜欢的任意字体.当您您找到或购买到希望使用的字体 ...

  5. 高性能javascript学习笔记系列(2)-数据存取

    参考 高性能javascript Tom大叔深入理解javascript系列 相关概念 1.执行上下文   当控制器转到ecmascript可执行代码的时候,就会进入一个执行上下文,执行上下文是以堆栈 ...

  6. CRM 2015 快速抓取表单上字段

    var table='<table><tr><td>序号</td><td>字段名</td><td>Schema名称& ...

  7. 调用CRM自己的Dialogue

    var DialogOption = new Xrm.DialogOptions; DialogOption.width = document.body.clientWidth * 0.9; Dial ...

  8. SharePoint2010升级到SharePoint2013操作手册

    SharePoint2010升级到SharePoint2013操作手册 目 录 第一章 前言    3 第二章 升级前准备    3 第三章 升级流程图    5 第四章 升级过程    5 4.1 ...

  9. leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  10. Android - 模块添加与编译

    Android5.1 Ubuntu14.04 Android系统编译依靠.mk文件 添加google服务 我们自己的ROM里没有google服务 完整的google包里包含google框架和各种服务, ...