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. Java Selenium封装--RemoteWebElement

    package com.liuke.selenium.driver; import java.sql.SQLException; import java.util.List; import org.j ...

  2. 在 Windows 上遇到非常多 TIME_WAIT 連線時應如何處理

        我們公司所代管的網站裡,有幾個流量是非常大的,在尖峰的時刻同時上線人數可能高達數千到數萬人,而在這個時候如果使用 netstat 或 TCPView 查看所有 TCP 連線時就會看到非常多處於 ...

  3. 关于HTML面试题汇总

    一.doctype的作用,严格与混杂模式的区别,有何意义 1.语法格式:<!DOCTYPE html> 2.<!DOCTYPE>不是Html标签,而是告知浏览器此页面使用哪个H ...

  4. 一款效果精致的 jQuery 多层滑出菜单插件

    想要以用户友好的方式呈现多级菜单是件不容易的事情,而且还要跨浏览器兼容就更难了.Multi-Level Push Menu 这款 jQuery 插件提供了呈现这种菜单的解决方案,能够让你无限制的展示菜 ...

  5. JavaScript事件对象

    一. 事件对象 事件处理三部分组成:对象.事件处理函数=函数.例如:单击文档任意处. 1.鼠标按钮 非 IE(W3C)中的 button 属性 0 表示主鼠标按钮(常规一般是鼠标左键) 1 表示中间的 ...

  6. 关于Canvas模糊的问题

    前言:当我在几个多月前,第一次在移动设备上使用canvas绘制内容的时候,我惊然地发现我绘制的图片以及文字居然都是模糊的!我的内心几乎是崩溃的,因为那是我第一个使用canvas的项目,是一个基于微信端 ...

  7. iOS Assigning to 'id<XXXDelegate>' from incompatible type 'BViewController *__strong'

    在使用代理的时候, BViewController *BVC = [[BViewController alloc]init]; self.delegate = BVC; 出现这样的警告Assignin ...

  8. Python将Excel生成SHP

    #!/usr/bin/env python # -*- coding: utf-8 -*- import gdal import xlrd import shapefile # open the ex ...

  9. 干货之UICollectionViewFlowLayout自定义排序和拖拽手势

    使用UICollectionView,需要使用UICollectionViewLayout控制UICollectionViewCell布局,虽然UICollectionViewLayout提供了高度自 ...

  10. 电脑公司最新GHOST WIN7系统32,64位优化精简版下载

    系统来自系统妈:http://www.xitongma.com 电脑公司最新GHOST win7系统32位优化精简版V2016年3月 系统概述 电脑公司ghost win7 x86(32位)万能装机版 ...