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. vue-页面回退

    <template> <div> <button @click="goback">我是Home01</button> </di ...

  2. 实习培训——Java基础(1)

    实习培训——Java基础(1) 1.我的第一个JAVA程序 首先好配置好JDK环境,百度上有很多.创建文件HelloWorld.java(文件名与类名相同),代码如下: public class He ...

  3. R实现的最小二乘lsfit函数学习

    1.源码 function (x, y, wt = NULL, intercept = TRUE, tolerance = 1e-, yname = NULL) { x <- as.matrix ...

  4. PAT 1076 Forwards on Weibo[BFS][一般]

    1076 Forwards on Weibo (30)(30 分) Weibo is known as the Chinese version of Twitter. One user on Weib ...

  5. [LeetCode]94, 144, 145 Binary Tree InOrder, PreOrder, PostOrder Traversal_Medium

    Given a binary tree, return the inorder, preorder, postorder traversal of its nodes' values. Example ...

  6. [LeetCode] questions conclustion_Path in Tree

    Path in Tree: [LeetCode] 112. Path Sum_Easy tag: DFS       input: root, target,   return True if exi ...

  7. chkconfig添加进入服务后,出现的现象

    比如在php-fpm添加服务中,一部分脚步如下 #!/bin/sh       #       # php-fpm - this script starts and stops the php-fpm ...

  8. C#中NPOI操作excel之读取和写入excel数据

    一.下载引用 下载需要引用的dll,即:NPOI.dll,NPOI.OOXML.dll,NPOI.OpenXml4Net.dll,ICSharpCode.SharpZipLib.dll(office2 ...

  9. LCD驱动

    LCD的驱动情况比较多. 对于一般的LCD,驱动方式有MCU,MPU,SPI等.其中MCU方式不需要输入clk,vsync,hsync等信号.完全可以通过异步来驱动,但是这样难以将屏 幕做到很大.MP ...

  10. UVM中的factory机制实现

    首先在Systemverilog中便有对于重载的最基本的支持. 1)定义task/function时,使用virtual关键字.那之后在test_case中调用时,便使用句柄指向的对象的类型而不是句柄 ...