poj 2104 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(可持久线段树)的更多相关文章
- [POJ2104] K – th Number (可持久化线段树 主席树)
题目背景 这是个非常经典的主席树入门题--静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个正整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入输 ...
- POJ 2828 Buy Tickets(排队问题,线段树应用)
POJ 2828 Buy Tickets(排队问题,线段树应用) ACM 题目地址:POJ 2828 Buy Tickets 题意: 排队买票时候插队. 给出一些数对,分别代表某个人的想要插入的位 ...
- [BZOJ 3110] [luogu 3332] [ZJOI 2013]k大数查询(权值线段树套线段树)
[BZOJ 3110] [luogu 3332] [ZJOI 2013]k大数查询(权值线段树套线段树) 题面 原题面有点歧义,不过从样例可以看出来真正的意思 有n个位置,每个位置可以看做一个集合. ...
- POJ 2104:K-th Number(主席树静态区间k大)
题目大意:对于一个序列,每次询问区间[l,r]的第k大树. 分析: 主席树模板题 program kthtree; type point=record l,r,s:longint; end; var ...
- POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25904 Accepted: 7682 Descr ...
- 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 ...
- POJ - 2777——Count Color(懒标记线段树二进制)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 53639 Accepted: 16153 Des ...
- POJ 1151 Atlantis 矩形面积求交/线段树扫描线
Atlantis 题目连接 http://poj.org/problem?id=1151 Description here are several ancient Greek texts that c ...
- 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 ...
- #5 DIV2 A POJ 3321 Apple Tree 摘苹果 构建线段树
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25232 Accepted: 7503 Descr ...
随机推荐
- windows / linux系统中,端口被占用解决方法
一.在windows操作系统中,查询端口占用和清除端口占用的程序 提升权限后用:netstat -b或用 1.查询端口占用的进程ID 点击"开始"-->"运行&qu ...
- java栈和堆区别
1, 垃圾回收机制仅仅作用于堆内存,与栈内存无关; 2, 栈:stack 栈的存取速度比堆快,效率高 保存局部变量和对象的引用值 3, 堆:保存较大的变量 4, 栈有一个很重要的特殊性,就是存在栈中的 ...
- Dropplets – 极简的 Markdown 博客平台
Dropplets 是一个简单的博客平台,专注于提供正是你在博客解决方案中需要的.当涉及到基础的博客功能,你真正想要做的是写和发表,而这就是 Dropplets 的过人之处.Dropplets 是一个 ...
- PyInstaller编译python3时使用的详细参数介绍
继续翻译中.... The syntax of the pyinstaller command is: pyinstaller [options] script [script ...] | spec ...
- Troubleshooting FIM: (No Display Name) in FIM Portal
from: http://social.technet.microsoft.com/wiki/contents/articles/12682.troubleshooting-fim-no-displa ...
- JavaScript学习07 内置对象
JavaScript内置对象 图像对象 导航对象 窗口对象 屏幕对象 事件对象 历史对象 文件对象(重要) 锚点对象 链接对象 框架对象 表单对象(重要) 位置对象 JS Window 窗口对象:ht ...
- Net.Sf.Json java Object to JsonObject
public class People{ private String name; public void setName(String name){ this.name = name; } publ ...
- TCP/IP 协议难点之一—— IP分片
1 IP协议简单介绍 就个人而言,网络中,抛开网络安全加密这些,就只单单讨论协议本身,比较难的有三个地方: IP分片与重组 TCP滑动窗口与TCP状态的改变 TCP定时器 其实协议本身根据<TC ...
- IOS动画
基础动画 //创建基础动画 CABasicAnimation * basic = [CABasicAnimation animation]; //动画路径 basic.keyPath ...
- fillStyle线性渐变
废话小说,沾待马 <!DOCTYPE HTML> <head> <meta charset = "utf-8"> <title>ca ...