【POJ2104】【HDU2665】K-th Number

Description

You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment. 
That is, given an array a[1...n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: "What would be the k-th number in a[i...j] segment, if this segment was sorted?" 
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2...5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.

Input

The first line of the input file contains n --- the size of the array, and m --- the number of questions to answer (1 <= n <= 100 000, 1 <= m <= 5 000). 
The second line contains n different integer numbers not exceeding 109 by their absolute values --- the array for which the answers should be given. 
The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).

Output

For each question output the answer to it --- the k-th number in sorted a[i...j] segment.

Sample Input

7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3

Sample Output

5
6
3

Hint

This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.
题意:n个数,m次询问,每次求区间[l,r]中的第k小的数
题解:主席树模板(什么是主席树?)
主席树也叫可持久化线段树、函数式线段树,我感觉就跟动态开点线段树差不多(什么是动态开点线段树?)
正常的线段树就是lson=x<<1,rson=x<<1|1,但动态开点线段树不同,它的节点的左右儿子是即用即开的,并用数组记录,每搜到一个点,如果以前没开过,就新开一个点,然后继续搜。这样我们就可以将多个线段树放到一起(怎么做?)
如果许多互部重叠线段树都建在[1,n]这个区间上,那么我们就可以直接将他们都放到一起,分别记录每棵线段树的根root[i],然后正常的在线段树上搞,当需要用到一段没开过的区间时,就新开一个点记录这个区间,然后继续向下查询,如果需要就在开新的点。这样我们可以对每棵线段树进行操作,空间复杂度O(nlogn)
好了,下面说主席树,主席树当然也要动态开点,建n棵线段树(都是权值线段树),不过第i棵线段树保存的是[1,i]这段区间,也就相当于一个前缀(那空间复杂度岂不是O(n^2)?)
于是我们发现,这一堆线段树其实有很大一部分是重复的,比如第i棵线段树,它相当于第i-1棵线段树中新开了一个点a[i],那么如果a[i]在[1,mid]这段区间里,那么第i棵线段树和第i-1课线段树的[mid+1,r]这段区间就是完全相同的!于是我们直接让他们共用这段区间即可(方法:把root[i]的右儿子指针指到root[i-1]的右儿子上,仅新建一个root[i]的左儿子)。同理,如果a[i]在[mid+1,r]这段区间里,我们就让他们共用左儿子,然后继续这样做,直到l==r。于是空间复杂度:O(nlogn)
那查询的时候该怎么做呢,对于本题,[l,r]中的第k小,那么我们已经处理出了[1,r]和[1,l-1]这两棵线段树,那么我们将这两棵线段树相减(因为是权值线段树,所以将每个点都相减即可),就相当于得到了一棵新的线段树[l,r],然后再求整体的第k小(这就很简单了吧)
上面说的这些也不算详解吧,其实就是我个人对主席树的一些理解罢了。
注意:可能有负数
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=100010;
struct node
{
int ls,rs,siz;
}s[4000010];
struct NUM
{
int num,org;
}v[maxn];
int n,m,tot,nm;
int root[maxn],ref[maxn];
bool cmp1(NUM a,NUM b)
{
return a.num<b.num;
}
bool cmp2(NUM a,NUM b)
{
return a.org<b.org;
}
int readin()
{
int ret=0,f=1; char gc;
while(gc<'0'||gc>'9') {if(gc=='-')f=-f;gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret*f;
}
void pushup(int x)
{
s[x].siz=s[s[x].ls].siz+s[s[x].rs].siz;
}
void insert(int &x,int &y,int l,int r,int p)
{
y=++tot;
if(l==r)
{
s[y].siz=s[x].siz+1;
return ;
}
int mid=l+r>>1;
if(p<=mid) s[y].rs=s[x].rs,insert(s[x].ls,s[y].ls,l,mid,p);
else s[y].ls=s[x].ls,insert(s[x].rs,s[y].rs,mid+1,r,p);
pushup(y);
}
int query(int x,int y,int l,int r,int p)
{
if(l==r) return ref[l];
int mid=l+r>>1;
if(s[s[y].ls].siz-s[s[x].ls].siz>=p) return query(s[x].ls,s[y].ls,l,mid,p);
return query(s[x].rs,s[y].rs,mid+1,r,p-s[s[y].ls].siz+s[s[x].ls].siz);
}
int main()
{
n=readin(),m=readin();
int i,a,b,c;
for(i=1;i<=n;i++) v[i].num=readin(),v[i].org=i;
sort(v+1,v+n+1,cmp1);
ref[0]=-1<<30;
for(i=1;i<=n;i++)
{
if(v[i].num>ref[nm]) ref[++nm]=v[i].num;
v[i].num=nm;
}
sort(v+1,v+n+1,cmp2);
for(i=1;i<=n;i++)
insert(root[i-1],root[i],1,nm,v[i].num);
for(i=1;i<=m;i++)
{
a=readin(),b=readin(),c=readin();
printf("%d\n",query(root[a-1],root[b],1,nm,c));
}
return 0;
}

【POJ2104】【HDU2665】K-th Number 主席树的更多相关文章

  1. poj2104 k-th number 主席树入门讲解

    poj2104 k-th number 主席树入门讲解 定义:主席树是一种可持久化的线段树 又叫函数式线段树   刚开始学是不是觉得很蒙逼啊 其实我也是 主席树说简单了 就是 保留你每一步操作完成之后 ...

  2. poj 2104 K-th Number 主席树+超级详细解释

    poj 2104 K-th Number 主席树+超级详细解释 传送门:K-th Number 题目大意:给出一段数列,让你求[L,R]区间内第几大的数字! 在这里先介绍一下主席树! 如果想了解什么是 ...

  3. poj2104 K-th Number区间第k小值 主席树

    原来主席树就是可持久化线段树啊,刚知道,,, 作为一道裸题,还是必A的,然而一开始偷懒不写离散化跪了N多遍,后来在缪大的帮助下发现了这个问题,遂A之 ——又是这种破问题,实在不想说自己了 把n个数看成 ...

  4. POJ2104 K-th Number[主席树]【学习笔记】

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

  5. [poj2104] K-th Number (主席树)

    主席树 Description You are working for Macrohard company in data structures department. After failing y ...

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

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

  7. POJ 2104 K-th Number 主席树(区间第k大)

    题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...

  8. 【poj2104】K-th Number 主席树

    题目描述 You are working for Macrohard company in data structures department. After failing your previou ...

  9. POJ 2104 K-th Number ( 求取区间 K 大值 || 主席树 || 离线线段树)

    题意 : 给出一个含有 N 个数的序列,然后有 M 次问询,每次问询包含 ( L, R, K ) 要求你给出 L 到 R 这个区间的第 K 大是几 分析 : 求取区间 K 大值是个经典的问题,可以使用 ...

随机推荐

  1. WPF(x:Null 使用)

    <Window x:Class="TestOfNull.MainWindow" xmlns="http://schemas.microsoft.com/winfx/ ...

  2. windows程序设计(三)

    MFC所有封装类一共200多个,但在MFC的内部技术不只是简单的封装 MFC的内部总共有六大关键技术,架构起了整个MFC的开发平台 一.MFC的六大关键技术包括: a).MFC程序的初始化过程 b). ...

  3. Linux学习 -- Shell基础 -- Bash基本功能

    历史命令 history -c   clear -w   写入 ~/.bash_history 默认保存1000条, 可在/etc/profile中修改 调用 Tab补全 命令.目录.文件 命令别名 ...

  4. JavaBean,List,Map转成json格式

    public class User { private String username; private String password; public String getUsername() { ...

  5. python socket编程---从使用Python开发一个Socket示例说到开发者的思维和习惯问题

    今天主要说的是一个开发者的思维和习惯问题. 思维包括编程的思维和解决一个具体问题的分析思维,分析思路,分析方法,甚至是分析工具. 无论是好习惯还是不好的习惯,都是在者一天一天的思维中形成的.那些不好的 ...

  6. AJAX(XMLHttpRequest)进行跨域请求方法详解(一)

    注意:以下代码请在Firefox 3.5.Chrome 3.0.Safari 4之后的版本中进行测试.IE8的实现方法与其他浏览不同. 跨域请求,顾名思义,就是一个站点中的资源去访问另外一个不同域名站 ...

  7. java集合框架工具类Collections,集合的操作

    1 import java.util.*; public class asList { public static void main(String args[]) { // int arr[] = ...

  8. a href="javascript:void(0)" 是什么意思?加不加上有什么区别?

    <A href="javascript:void(0)">点击</a>点击链接后不会回到网页顶部<A href="#">点击 ...

  9. js中将 整数转成字符,,将unicode 编码后的字符还原出来的方法。

    一.将整数转成字符: String.fromCharCode(17496>>8,17496&0xFF,19504>>8,19504&0xFF,12848> ...

  10. CodeForces 158C - Cd and pwd commands(模拟)

    这个题我们又把题意理解错了,队友翻译了以后给我解释,我问这个直接一个单词开头的是要找到这个文件夹吗,他说是,然后我就呵呵了..奔着树形结构去和字符串维护就去了...做了好久都没模拟出来,感觉做出来的人 ...