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;
    }
  • 相关阅读:
    SQL Server 2012 联机丛书安装
    SQL Server 2012 联机丛书离线安装
    无法删除对象 '产品',因为该对象正由一个 FOREIGN KEY 约束引用。
    System.Data.SqlClient.SqlError:无法对过程'XXX' 执行 删除,因为它正用于复制。消息 3724,级别 16
    在与SQL Server建立连接时出现于网络相关的或特定于实例的错误
    SQL Server如何删除多余tempDB文件
    无法用sysadmin权限的登录名登陆,sa密码忘了,管理员被锁在外面
    Linux----------nginx基础
    Linux----------varnish缓存加速
    Linux----------nginx日志分析
  • 原文地址:https://www.cnblogs.com/zbtrs/p/8277291.html
Copyright © 2011-2022 走看看