zoukankan      html  css  js  c++  java
  • POJ2104 K-th Number(主席树)

    题目

    Source

    http://poj.org/problem?id=2104

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

    分析

    题目是给一个序列,多次询问序列区间中第k小的元素。

    经典主席树入门题。。相当于序列各个前缀建一棵权值线段树,区间上的信息就是两个前缀和的差分了。。复习复习,已经忘了。

    代码

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    #define MAXN 111111
    
    int root[MAXN],tree[MAXN*20],lch[MAXN*20],rch[MAXN*20],x,y,N;
    void update(int i,int j,int a,int &b){
        b=++N;
        if(i==j){
            tree[b]=tree[a]+1;
            return;
        }
        lch[b]=lch[a];
        rch[b]=rch[a];
        int mid=i+j>>1;
        if(x<=mid) update(i,mid,lch[a],lch[b]);
        else update(mid+1,j,rch[a],rch[b]);
        tree[b]=tree[lch[b]]+tree[rch[b]];
    }
    int query(int i,int j,int l,int r,int k){
        if(i==j) return i;
        int mid=i+j>>1;
        int lcnt=tree[lch[r]]-tree[lch[l]];
        if(k<=lcnt) return query(i,mid,lch[l],lch[r],k);
        return query(mid+1,j,rch[l],rch[r],k-lcnt);
    }
    
    int a[MAXN],b[MAXN];
    int main(){
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; ++i){
            scanf("%d",&a[i]);
        }
    
        memcpy(b,a,sizeof(b));
        sort(b+1,b+1+n);
        int bn=unique(b+1,b+1+n)-b-1;
    
        for(int i=1; i<=n; ++i){
            x=lower_bound(b+1,b+1+n,a[i])-b;
            update(1,bn,root[i-1],root[i]);
        }
    
        int l,r,k;
        while(m--){
            scanf("%d%d%d",&l,&r,&k);
            printf("%d
    ",b[query(1,bn,root[l-1],root[r],k)]);
        }
        return 0;
    }
    
  • 相关阅读:
    CF932E Team Work(第二类斯特林数)
    BZOJ 3732: Network(Kruskal重构树)
    BZOJ 2753: [SCOI2012]滑雪与时间胶囊(最小生成树)
    BZOJ 2286: [Sdoi2011]消耗战(虚树+树形dp)
    hdu 4336 Card Collector(状压dp/Min-Max反演)
    BZOJ 3622: 已经没有什么好害怕的了(二项式反演)
    BZOJ 2839: 集合计数(二项式反演)
    CF gym 101933 K. King's Colors(二项式反演)
    BZOJ 1101: [POI2007]Zap(莫比乌斯反演)
    BZOJ 3747: [POI2015]Kinoman(线段树)
  • 原文地址:https://www.cnblogs.com/WABoss/p/5936129.html
Copyright © 2011-2022 走看看