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;
    }
    
  • 相关阅读:
    Python学习 Day 1-简介 安装 Hello world
    R Programming week2 Functions and Scoping Rules
    R in action读书笔记(6)-第七章:基本统计分析(中)
    R in action读书笔记(5)-第七章:基本统计分析
    Redis进阶应用:Redis+Lua脚本实现复合操作
    一个项目的SpringCloud微服务改造过程
    中间件增强框架之-CaptureFramework框架
    关系型数据库全表扫描分片详解
    AI中台——智能聊天机器人平台的架构与应用(分享实录)
    如何设计实时数据平台(设计篇)
  • 原文地址:https://www.cnblogs.com/WABoss/p/5936129.html
Copyright © 2011-2022 走看看