POJ 2104 主席树 解题报告

来源:互联网 发布:windows优化大师电脑版 编辑:程序博客网 时间:2024/05/22 16:03

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

【解题报告】

查询区间第k小。
主席树其实相当于建立了n棵线段树,第i棵线段树是根据区间[1,i]按值建立的。对于每一棵线段树我们记录它对应的区间每个数出现的次数,所以首先要对所有的数离散化。
先考虑最简单的情况,只查询[1,n]的第k小,对于[1,n]我们按值建立一棵线段树,对于a[i]我们在位置a[i]上加1。查询第k小那么先看左子区间出现了多少个数cnt,假设左区间出现的数cnt>=k,那么直接递归到左区间查询(因为是按值建立的,左区间的数肯定小于右区间),否则递归到右区间查询第k-cnt小(左区间已经有了最小的cnt个数了)
对于任意区间查询[l,r],我们只需要比较第l-1棵线段树和第r棵线段树,[l,r]之间的数就是第r棵线段树相比于第l-1棵多出来的数。只需要对比两颗树同一个节点,对比到哪个数为止第r棵比第l-1棵刚好多出k个数。(先比较左区间cntr-cntl,cntr-cntl>=k,则递归到左区间,否则递归查询右区间k-cntr-cntl)
主席树就相当于n棵线段树,但是对比建立在[1,i]的线段树和[1,i+1]的线段树,只多出了一个值,也就是相当于单点更新他们之间只有logn个节点是不同的,所以可以将[1,i+1]的一些节点指针指向前一棵的共同部分。每次新增的空间只需要logn。

代码如下:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define N 100010int n,m,cnt,b[N],cpy[N],now,root[N];struct Tree{    int l,r,cnt;}tree[N*20];void pushup(int pos){    tree[pos].cnt=tree[tree[pos].l].cnt+tree[tree[pos].r].cnt;}int build(int l,int r){    int pos=now++;    if(l==r) return pos;    int mid=(l+r)>>1;    tree[pos].l=build(l,mid);    tree[pos].r=build(mid+1,r);    pushup(pos);    return pos;}int insert(int o,int l,int r,int arr){    int pos=now++;    tree[pos]=tree[o];    if(l==arr&&r==arr)    {        tree[pos].cnt++;        return pos;    }    int mid=(l+r)>>1;    if(arr<=mid)        tree[pos].l=insert(tree[o].l,l,mid,arr);    else         tree[pos].r=insert(tree[o].r,mid+1,r,arr);    pushup(pos);    return pos;}int query(int l,int r,int o,int v,int kth){    if(l==r) return l;    int mid=(l+r)>>1,res=tree[tree[v].l].cnt-tree[tree[o].l].cnt;    if(kth<=res) return query(l,mid,tree[o].l,tree[v].l,kth);    else return query(mid+1,r,tree[o].r,tree[v].r,kth-res);}int main(){    scanf("%d%d",&n,&m);    for(int i=1;i<=n;i++)    {        scanf("%d",&b[i]);        cpy[i]=b[i];        }    sort(cpy+1,cpy+1+n);    cnt=unique(cpy+1,cpy+1+n)-cpy-1;    root[0]=build(1,cnt);    for(int i=1;i<=n;i++)    root[i]=insert( root[i-1],1,cnt,lower_bound( cpy+1,cpy+1+cnt,b[i] )-cpy );    while(m--)    {        int xx,yy,zz;        scanf("%d%d%d",&xx,&yy,&zz);        printf("%d\n",cpy[query(1,cnt,root[xx-1],root[yy],zz)]);    }    return 0;}
原创粉丝点击