POJ2104 POJ2761 区间第K大 主席树

题意

查询一个区间第k大的数

解法

之前只是套版,没仔细看模板里的代码很吃亏,重新仔细看了一遍,理解更深了一点。

这道题里面建了N个线段树,第i个线段树维护后缀[i…n]中数字在大小为[L,R]的区间里面的个数,但是数字很大,离散化一下就好(用unique)

精髓在查询里面,查询区间[L,R]里面第K大的数字,其实就是找到一个mid,使sum[1,mid] = k(在T[L]上查询的结果 - T[R+1]上查询的结果就是在[L,R]上查询的结果) 使用二分即可,在线段树上二分其实就是相当于在树上进行二分查找。

代码

/* ***********************************************
Author        :czw
注:使用了kuangbin大神的主席树模板
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

/*
 * 给出一个序列,查询区间内有多少个不相同的数
 */

namespace PST {

    const int MAXN = 100005;
    const int M = MAXN * 50;
    int tot;
    int T[M],lson[M],rson[M],c[M];
    int n;
    void init(int _n) {
        tot = 0;
        n = _n;
    }
    int build(int l = 1,int r = n)
    {
        int root = tot++;
        c[root] = 0;
        if(l != r)
        {
            int mid = (l+r)>>1;
            lson[root] = build(l,mid);
            rson[root] = build(mid+1,r);
        }
        return root;
    }
    int update(int root,int pos,int val)
    {
        int newroot = tot++, tmp = newroot;
        c[newroot] = c[root] + val;
        int l = 1, r = n;
        while(l < r)
        {
            int mid = (l+r)>>1;
            if(pos <= mid)
            {
                lson[newroot] = tot++; rson[newroot] = rson[root];
                newroot = lson[newroot]; root = lson[root];
                r = mid;
            }
            else
            {
                rson[newroot] = tot++; lson[newroot] = lson[root];
                newroot = rson[newroot]; root = rson[root];
                l = mid+1;
            }
            c[newroot] = c[root] + val;
        }
        return tmp;
    }
    int queryK(int lroot, int rroot, int k) {
        //后缀[l...n],后缀[r+1...n],第k大
        //注意二分的形式要与上面build相同,才能保证区间的对应关系
        int l = 1, r = n, mid;
        while(l < r) {
            mid = (l + r) >> 1;
            if(c[lson[lroot]] - c[lson[rroot]] >= k) {
                r = mid;
                lroot = lson[lroot];
                rroot = lson[rroot];
            }
            else {
                k -= c[lson[lroot]] - c[lson[rroot]];
                l = mid + 1;
                lroot = rson[lroot];
                rroot = rson[rroot];
            }
        }
        return l;
    }

}

const int SIZE = PST::MAXN;
int A[SIZE];
int B[SIZE];
int bc;

int init(int &n) {
    for(int i = 1; i <= n; i++) {
        B[i] = A[i];
    }
    sort(B+1, B+1+n);
    bc = unique(B+1, B+1+n) - B - 1;
}

int Rank(int a) {
    return lower_bound(B+1, B+bc+1, a) - B;
}

int main() {
    int n, q;
    while(~scanf("%d%d",&n,&q)) {
        for(int i = 1; i <= n; i++) {
            scanf("%d",&A[i]);
        }
        init(n);
        PST::init(bc);
        PST::T[n+1] = PST::build();
        for(int i = n; i >= 1; i--) {
            PST::T[i] = PST::update(PST::T[i+1], Rank(A[i]), 1);
        }
        while(q--) {
            int l, r, k;
            scanf("%d%d%d",&l,&r,&k);
            printf("%d\n",B[PST::queryK(PST::T[l],PST::T[r+1],k)]);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值