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;
    }
  • 相关阅读:
    UWP中实现大爆炸效果(二)
    UWP中实现大爆炸效果(一)
    c# 【电影搜索引擎】采集电影站源码
    安利一个聚合搜索导航站,及怎么样设置成默认的搜索引擎
    女朋友经常问影视剧, 答不上来怎么办?
    宝塔linux面板, 服务器日志分析与流量统计这款插件的mysql版优化。
    苹果cms自动采集,重复执行遇到“上次执行时间: --跳过”的解决办法
    苹果cms, 后台设置保存不了的解决办法
    解决 C:WINDOWSsystem32inetsrv ewrite.dll 未能加载。返回的数据为错误.
    img error 图片加载失败的最佳方案
  • 原文地址:https://www.cnblogs.com/zbtrs/p/8277291.html
Copyright © 2011-2022 走看看