zoukankan      html  css  js  c++  java
  • poj2104 K-th Number

    K-th Number
    Time Limit: 20000MS   Memory Limit: 65536K
    Total Submissions: 62516   Accepted: 21975
    Case Time Limit: 2000MS

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

    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

    Hint

    This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.

    Source

    Northeastern Europe 2004, Northern Subregion
    分析:模板题,注意数组大小.
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    const int maxn = 2000010;
    
    int n,tot,a[maxn],b[maxn],m,sum[maxn],root[maxn],cnt;
    
    struct node
    {
        int l,r,sum;
    }e[maxn];
    
    void update(int y,int &x,int l,int r,int p)
    {
        e[x = ++cnt] = e[y];
        ++e[x].sum;
        if (l == r)
            return;
        int mid = (l + r) >> 1;
        if (p <= mid)
            update(e[y].l,e[x].l,l,mid,p);
        else
            update(e[y].r,e[x].r,mid + 1,r,p);
    }
    
    int query(int x,int y,int l,int r,int k)
    {
        if (l == r)
            return l;
        int temp = e[e[y].l].sum - e[e[x].l].sum;
        int mid = (l + r) >> 1;
        if (temp >= k)
            return query(e[x].l,e[y].l,l,mid,k);
        else
            return query(e[x].r,e[y].r,mid + 1,r,k - temp);
    }
    
    int main()
    {
        scanf("%d%d",&n,&m);
        for (int i = 1; i <= n; i++)
        {
            scanf("%d",&a[i]);
            b[i] = a[i];
        }
        sort(b + 1,b + 1 + n);
        tot = unique(b + 1,b + 1 + n) - b - 1;
        for (int i = 1; i <= n; i++)
            a[i] = lower_bound(b + 1,b + 1 + tot,a[i]) - b;
        for (int i = 1; i <= n; i++)
            update(root[i - 1],root[i],1,tot,a[i]);
        for (int i = 1; i <= m; i++)
        {
            int l,r,k;
            scanf("%d%d%d",&l,&r,&k);
            printf("%d
    ",b[query(root[l - 1],root[r],1,tot,k)]);
        }
    
        return 0;
    }
  • 相关阅读:
    使用VMware Workstation安装win7镜像文件时遇见的错误
    每次启动懂maven项目都必须关闭javaw.exe进程
    laravel excel导出调节列宽度,对某列中数据颜色处理
    三个<li>元素放一行
    php中bootstrap框架.popover弹出框,鼠标移动到上面自动显示,离开自动消失
    锚点,自动跳转到某处
    WdatePicker控件Javascript取得当前时间、取得减30分钟时间
    (mysql数据库报错)The user specified as a definer ('root'@'%') does not exist
    c# tcp协议发送数据
    c# 键值对的方式post提交
  • 原文地址:https://www.cnblogs.com/zbtrs/p/8277291.html
Copyright © 2011-2022 走看看