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)]);
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章