1. 程式人生 > >HDOJ2665 Kth number --- 劃分樹求區間第k小數

HDOJ2665 Kth number --- 劃分樹求區間第k小數

Problem Description

Give you a sequence and ask you the kth big number of a inteval.

Input

The first line is the number of the test cases.  For each test case, the first line contain two integer n and m (n, m <= 100000), indicates the number of integers in the sequence and the number of the quaere.  The second line contains n integers, describe the sequence.  Each of following m lines contains three integers s, t, k.  [s, t] indicates the interval and k indicates the kth big number in interval [s, t]

Output

For each test case, output m lines. Each line contains the kth big number.

Sample Input

1 10 1 1 4 2 3 5 6 7 8 9 0 1 3 2

Sample Output

2

題意: 有n組樣例,每次輸入n,m代表n個數(1...n)和m次詢問。 然後輸入這n個數用a陣列存下來。

每次詢問給3個數l,r,k,表示詢問第a[l...r]中第k小的數。

注: 這題有點坑了,題目說是求第k大數,其實是求k小。。。

題解:單純的求第k小數,可以用快排和優先佇列來做,但是這題要求m次詢問,這兩種方法明顯複雜度大了。

這種題我所知道的可以用劃分樹和主席樹來做,不過由於我太菜不會主席樹,所以此題採用劃分樹來做。

這題基本就是一個裸的劃分樹模板,劃分樹的資料網上很多,不會的話花一個多小時的時候差不多能看懂的。寫劃分樹的時候需要注意的是Query函式裡面有個大的陣列範圍L和R,還有個小的查詢範圍l,r,一定要注意每次遞迴訪問陣列的時候不能越過L--R的邊界,不然就會像我一樣一直Runtime Error。

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
// 從1開始計數
int a[22][111111];  // 儲存劃分樹
int sorted[111111]; 
int num[22][111111]; // 記錄進入左子樹的數目
int n,m;
int l,r,k; // 每次詢問的區間和

// 建樹
void Build(int l,int r,int depth) {
    if(l == r) {
        return;  // 結束遞迴
    }
    int mid = (l + r) / 2;    
    int same = 0;// 記錄有多少與mid的數相等且會進入左子樹的元素
    for(int i = l;i <= mid;i++)
        if(sorted[i] == sorted[mid])
            same++;
    int left = l,right = mid+1;    
    num[depth][l] = 0;
    for(int i = l;i <= r;i++) {                
        if(i != l) {
            num[depth][i] = num[depth][i-1];
        }
        if(a[depth][i] < sorted[mid] || (a[depth][i] == sorted[mid] && same > 0)) {
            num[depth][i]++;
            a[depth+1][left++] = a[depth][i];
            if(a[depth][i] == sorted[mid])
                same--;            
        } else {
            a[depth+1][right++] = a[depth][i];
        }        
    }             
    Build(l,mid,depth+1);
    Build(mid+1,r,depth+1);
}

// 查詢區間第k小數
int Query(int l,int r,int L,int R,int k,int depth) {
    if(l == r)
        return a[depth][l];        
    int cnt;
    if(l-1>=L) {    
        cnt = num[depth][r] - num[depth][l-1];// 區間中進入左子樹的個數
    }
    else {
        cnt = num[depth][r];// 區間中進入左子樹的個數
    }        
    int mid = (L + R) / 2;
    if(k <= cnt) {
        // 進入左子樹查詢
        int newl;
        if(l-1 >= L)
        	newl = L + (num[depth][l-1]);
        else
			newl = L;	
        int newr = newl + cnt - 1;
        return Query(newl,newr ,L,mid,k,depth+1);
    } else {
        int newr = R - (R-r - (num[depth][R]-num[depth][r]));
        int newl = newr - (r-l+1-cnt) + 1;
        return Query(newl,newr,mid+1,R,k-cnt,depth+1);
    }
}


int main()
{
    int t;
    scanf("%d",&t);
    while(t--) {
        scanf("%d%d",&n,&m);
        for(int i = 1;i <= n;i++) {
            scanf("%d",&a[1][i]);
        }
        for(int i = 1;i <= n;i++)
            sortn ed[i] = a[1][i];
        sort(sorted+1,sorted+1+n);
        Build(1,n,1);                
        // m次問
        for(int i = 0;i < m;i++) {
            scanf("%d%d%d",&l,&r,&k);
            printf("%d\n",Query(l,r,1,n,k,1));
        }
        
    }
    
    return 0;
}