POJ2104-K-th Number(静态主席树模板 区间第k小)

题目链接

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 10
9 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


题意

给n个数,求区间 [ i , j ] 的第k小是多少

思路

主席树思路的理解:https://blog.csdn.net/creatorx/article/details/75446472

主席树的图示:https://blog.csdn.net/a_forever_dream/article/details/80450549

求第k小值,是权值线段树的作用,权值线段树与线段树区别是,保存的是数出现的次数

 

如果用sort排序,用前缀和的思想,只建立n棵权值线段树。如果知道[1,l - 1 ] 和[1,r ]区间比k小的个数,相减就是[l,r]区间比k小的个数,因为结点维护的区间范围是一样的

n棵线段树空间复杂度O(n*n*logn),仍然太大,但是每次第i棵线段树和第i+1棵线段树的区别其实只有一条链不同,所以可用重复利用相同的部分,节省空间。

盗上面链接一张图...

每次的根都是不一样的,但是根都复制了前面根的左右子树的编号,如果需要修改就再新建,不需要修改就复用之前的。

主席树的建树空间复杂度O(nlogn),建树时间复杂度O(nlogn),访问时间复杂度O(logn)。

线段树空间复杂度O(n*4),时间复杂度一样。

 

主席树可以理解为,多棵权值线段树的空间优化。


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 1e5+10;

struct Tree
{
    int l,r,sum;
}tr[N*20];

struct node
{
    int x,id;
}a[N];
bool cmp(node v1,node v2){return v1.x<v2.x;}

int rk[N],root[N],cnt;

void update(int &k,int l,int r,int v)
{
    tr[cnt] = tr[k]; tr[cnt].sum++;
    k = cnt++;
    if(l==r) return;
    int mid = (l+r)/2;
    if(v<=mid) update(tr[k].l,l,mid,v);
    else update(tr[k].r,mid+1,r,v);
}

int query(int L,int R,int l,int r,int k)
{

    if(l==r) return l;
    int d = tr[tr[R].l].sum - tr[tr[L].l].sum;
    int mid = (l+r)/2;
    if(k<=d) return query(tr[L].l,tr[R].l,l,mid,k);
    else return query(tr[L].r,tr[R].r,mid+1,r,k-d);
}

int main()
{
    int n,m; scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i].x);
        a[i].id = i;
    }
    sort(a+1,a+1+n,cmp);
    for(int i=1;i<=n;i++) rk[a[i].id] = i;

    cnt = 1;

    for(int i=1;i<=n;i++){
        root[i] = root[i-1];
        update(root[i],1,n,rk[i]);
    }

    for(int i=1;i<=m;i++){
        int x,y,v; scanf("%d%d%d",&x,&y,&v);
        int t = query(root[x-1],root[y],1,n,v);
        printf("%d\n",a[t]);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值