• 【poj2104】K-th Number 主席树


    题目描述

    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.

    输入

    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 109 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).

    输出

    For each question output the answer to it --- the k-th number in sorted a[i...j] segment.

    样例输入

    7 3

    1 5 2 6 3 7 4

    2 5 3

    4 4 1

    1 7 3

    样例输出

    5

    6

    3


    题目大意

    给你n个数和m组询问,对于每组询问输出区间[i,j]中第k大的数是多少。

    题解

    主席树模板题。

    先将数据离散化,然后加入到n棵动态开点线段树中。

    由于每次只修改一个值,所以其余的子节点可以直接继承上一个子节点。

    查询时,直接作差即可。

    #include <cstdio>
    #include <algorithm>
    using namespace std;
    struct data
    {
        int num , pos;
    }a[100010];
    int lp[4000010] , rp[4000010] , sum[4000010] , root[100010] , val[100010] , tot , cnt;
    bool cmp1(data a , data b)
    {
        return a.num < b.num;
    }
    bool cmp2(data a , data b)
    {
        return a.pos < b.pos;
    }
    void pushup(int x)
    {
        sum[x] = sum[lp[x]] + sum[rp[x]];
    }
    void ins(int x , int &y , int l , int r , int p)
    {
        y = ++tot;
        if(l == r)
        {
            sum[y] = sum[x] + 1;
            return;
        }
        int mid = (l + r) >> 1;
        if(p <= mid) rp[y] = rp[x] , ins(lp[x] , lp[y] , l , mid , p);
        else lp[y] = lp[x] , ins(rp[x] , rp[y] , mid + 1 , r , p);
        pushup(y);
    }
    int query(int x , int y , int l , int r , int p)
    {
        if(l == r) return val[l];
        int mid = (l + r) >> 1;
        if(sum[lp[y]] - sum[lp[x]] >= p) return query(lp[x] , lp[y] , l , mid , p);
        else return query(rp[x] , rp[y] , mid + 1 , r , p - sum[lp[y]] + sum[lp[x]]);
    }
    int main()
    {
        int n , m , i , x , y , z;
        scanf("%d%d" , &n , &m);
        for(i = 1 ; i <= n ; i ++ )
            scanf("%d" , &a[i].num) , a[i].pos = i;
        sort(a + 1 , a + n + 1 , cmp1);
        val[0] = 0x80000000;
        for(i = 1 ; i <= n ; i ++ )
        {
            if(a[i].num > val[cnt]) val[++cnt] = a[i].num;
            a[i].num = cnt;
        }
        sort(a + 1 , a + n + 1 , cmp2);
        for(i = 1 ; i <= n ; i ++ )
            ins(root[i - 1] , root[i] , 1 , cnt , a[i].num);
        for(i = 1 ; i <= m ; i ++ )
        {
            scanf("%d%d%d" , &x , &y , &z);
            printf("%d
    " , query(root[x - 1] , root[y] , 1 , cnt , z));
        }
    }
  • 相关阅读:
    停止与暂停线程
    flume日志收集框架
    mysql数据库索引
    junit
    freemarker
    python脚本
    java多线程编程核心技术学习-1
    spring 网站
    [MongoDB]学习笔记--Linux 安装和运行MongoDB
    [Spring MVC]学习笔记--form表单标签的使用
  • 原文地址:https://www.cnblogs.com/GXZlegend/p/6296381.html
Copyright © 2020-2023  润新知