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

题意:
求区间$[l,r]$的第k小。

思路:
主席树不好理解啊,简单叙述一下吧。

主席树就是由多棵线段树组成的,对于数组$a[1,2...n]$,对于每个i,我们都去建立一棵线段树维护$a[1,..i]$出现的数的个数。

但是如果每一棵线段树都去新建结点的话,那这内存的开销是十分巨大的。。。

我们可以发现,第i棵线段树和第i-1棵线段树有很多结点都是相同的,这样一来,我们就没必要再去重新新建结点了,直接套用上一棵线段树的结点即可。

这里我想引用一张某大神博客的手绘图解:(来自http://blog.csdn.net/regina8023/article/details/41910615

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn=+; int n, m;
int tot_num;
int tot=;
int b[maxn],c[maxn],t[maxn]; struct node
{
int l,r,num;
}a[maxn*]; int build(int l ,int r)
{
int root=++tot;
a[root].num=;
if(l==r) return root;
int mid=(l+r)>>;
a[root].l=build(l,mid);
a[root].r=build(mid+,r);
return root;
} int update(int root, int x)
{
int now=++tot;
int tmp=now;
a[tot].num=a[root].num+;
int l=,r=tot_num;
while(l<r)
{
int mid=(l+r)>>;
if(x<=mid)
{
a[now].l=++tot;
a[now].r=a[root].r; //这儿不需修改,套用上一棵线段树的数即可
root=a[root].l;
now=tot;
r=mid;
}
else
{
a[now].l=a[root].l; //同理
a[now].r=++tot;
root=a[root].r;
now=tot;
l=mid+;
}
a[now].num=a[root].num+;
}
return tmp;
} int query(int ql, int qr, int k)
{
int l=,r=tot_num;
while (l<r)
{
int mid=(l+r)>>;
if (a[a[qr].l].num-a[a[ql].l].num>=k) //>=k,说明肯定在该区间内,继续往下缩小范围
{
r=mid;
ql=a[ql].l;
qr=a[qr].l;
}
else
{
l=mid+;
k-=a[a[qr].l].num-a[a[ql].l].num; //<k,说明左区间的数不够,先减去左区间的数,然后往右区间搜索
ql=a[ql].r;
qr=a[qr].r;
}
}
return l;
} int main()
{
//freopen("in.txt","r",stdin);
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%d",&c[i]);
b[i]=c[i];
}
sort(b+,b+n+);
tot_num=unique(b+,b+n+)-b-;
t[]=build(,tot_num); //初始化
for(int i=;i<=n;i++)
{
t[i]=update(t[i-],lower_bound(b+,b+tot_num+,c[i])-b);
}
while(m--)
{
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",b[query(t[l-],t[r],k)]);
}
return ;
}

POJ 2104 K-th Number(主席树模板题)的更多相关文章

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

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

  2. 主席树:POJ2104 K-th Number (主席树模板题)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 44952   Accepted: 14951 Ca ...

  3. SPOJ MKTHNUM & POJ 2104 - K-th Number - [主席树模板题]

    题目链接:http://poj.org/problem?id=2104 Description You are working for Macrohard company in data struct ...

  4. 【BZOJ 1901】【Zju 2112】 Dynamic Rankings 动态K值 树状数组套主席树模板题

    达神题解传送门:http://blog.csdn.net/dad3zz/article/details/50638360 说一下我对这个模板的理解: 看到这个方法很容易不知所措,因为动态K值需要套树状 ...

  5. poj2104 主席树模板题

    题意 给出n个数字组成的数字序列,有m组询问.每次询问包含三个数字l,r,k.对于每个询问输出序列区间[l,r]中第k大的数字. 分析 这是主席树的模板题,套板子就可以 #include <cs ...

  6. hdu2665(主席树模板题)

    hdu2665 题意 求区间第 k 小. 分析 参考 这类题目做法挺多的,例如 划分树. 这里使用主席树再写一发,不得不说主席树相比而言要好写的多,比起普通线段树,主席树就是复用了线段树共有的信息. ...

  7. POJ-2104-K-th Number(区间第K大+主席树模板题)

    Description You are working for Macrohard company in data structures department. After failing your ...

  8. POJ 2104 主席树模板题

    #include <iostream> #include <cstdio> #include <algorithm> int const maxn = 200010 ...

  9. POJ2104 K-th Number 划分树 模板题啊

    /*Source Code Problem: 2104 User: 96655 Memory: 14808K Time: 1282MS Language: G++ Result: Accepted S ...

随机推荐

  1. spring boot 中用@value给static变量赋值

    需求:改写一个JedisUtils,工具类,所以最好用静态方法和变量. @value("${redis.host}") private static String redisHos ...

  2. Ubuntu 下Apache安装和配置

    在Ubuntu上安装Apache,有两种方式:1 使用开发包的打包服务,例如使用apt-get命令:2 从源码构建Apache.本文章将详细描述这两种不同的安装方式. 方法一:使用开发包的打包服务—— ...

  3. KMP(http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2772)

    #include <stdio.h>#include <string.h>#include <stdlib.h>char a[1000001],b[1000001] ...

  4. etcd 集群部署

    etcd web管理 https://nikfoundas.github.io/etcd-viewer/ # git clone https://github.com/nikfoundas/etcd- ...

  5. 在 Angularjs 中 ui-sref 和 $state.go

    ui-sref 一般使用在 <a>...</a>: <a ui-sref="message-list">消息中心</a> $stat ...

  6. liunx anacoda 安装pyltp

    anacoda 默认的gcc是4.7需要更新 https://anaconda.org/nlesc/gcc 更新之后再安装即可. 报错: /usr/lib64/libstdc++.so.6: vers ...

  7. Python正则处理多行日志一例(可配置化)

    正则表达式基础知识请参阅<正则表达式基础知识>,本文使用正则表达式来匹配多行日志并从中解析出相应的信息. 假设现在有这样的SQL日志: SELECT * FROM open_app WHE ...

  8. python 文件不存在时才能写入,读写模式xt

    想向一个文件中写入数据,但是前提必须是这个文件在文件系统上不存在.也就是不允许覆盖已存在的文件内容. 可以在open() 函数中使用x 模式来代替w 模式的方法来解决这个问题.比如: >> ...

  9. Python: 猴子分桃。海滩上有一堆桃子,五只猴子来分。

    海滩上有一堆桃子,五只猴子来分.第一只猴子把这堆桃子平均分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份.第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一 ...

  10. VMWare中桥接、NAT、Host-only

    1.概述 2.bridged(桥接模式) 3.NAT(网络地址转换模式) 4.host-only(主机模式) 5.replicate physical network connection state ...