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;
    }
  • 相关阅读:
    [转载]DB2数据库移植罕见成绩片面理睬(4)
    [转载]接续刷新Sybase数据库后果大搜聚1
    [转载]DB2数据库移植罕见结果周全理会(3)
    [转载]有关PB/Sybase斥地过程中数据库转移引见 (2)
    [转载]如何将sybase的sa暗码重置为空
    [转载]DB2数据库移植罕有成绩片面解析(1)
    [转载]精益求精Sybase数据库题目成果年夜征求8
    [转载]精益求精Sybase数据库标题大包括6
    [转载]千锤百炼Sybase数据库题目大大搜罗7
    [转载]DB2数据库移植罕有结果片面解析(2)
  • 原文地址:https://www.cnblogs.com/zbtrs/p/8277291.html
Copyright © 2011-2022 走看看