zoukankan      html  css  js  c++  java
  • POJ 2104 K-TH NUMBER

    K-th Number
    Time Limit: 20000MS   Memory Limit: 65536K
    Total Submissions: 52338   Accepted: 17981
    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
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<algorithm>
     5 #define maxn 100005
     6 #define maxm 10000010
     7 using namespace std;
     8 int root[maxn],ls[maxm],rs[maxm],cnt[maxm],tot;
     9 int sz[maxn],hash[maxn];
    10 void build(int &cur,int l,int r){
    11     cur=tot++;cnt[cur]=0;
    12     if(l!=r){
    13         int mid=(l+r)/2;
    14         build(ls[cur],l,mid);
    15         build(rs[cur],mid+1,r);
    16     }
    17 }
    18 void update(int pre,int ps,int &cur,int l,int r){
    19     cur=tot++;cnt[cur]=cnt[pre]+1;
    20     ls[cur]=ls[pre];rs[cur]=rs[pre];
    21     if(l==r) return ;
    22     int mid=(l+r)/2;
    23     if(ps<=mid) update(ls[pre],ps,ls[cur],l,mid);
    24     else update(rs[pre],ps,rs[cur],mid+1,r);
    25 }
    26 int query(int lt,int rt,int l,int r,int k){
    27     if(l==r) return l;
    28     int mid=(l+r)/2,cha=cnt[ls[rt]]-cnt[ls[lt]];
    29     if(k<=cha) return query(ls[lt],ls[rt],l,mid,k);
    30     else return query(rs[lt],rs[rt],mid+1,r,k-cha);
    31 }
    32 int main()
    33 {
    34     int n,m,k,l,r;
    35     while(scanf("%d%d",&n,&m)==2){
    36         for(int i=1;i<=n;i++){
    37             scanf("%d",sz+i);
    38             hash[i]=sz[i];
    39         }
    40         sort(hash+1,hash+n+1);
    41         int siz=unique(hash+1,hash+n+1)-hash-1;
    42         for(int i=1;i<=n;i++)
    43           sz[i]=lower_bound(hash+1,hash+1+siz,sz[i])-hash;
    44         tot=0;
    45         build(root[0],1,siz);
    46         for(int i=1;i<=n;i++)
    47           update(root[i-1],sz[i],root[i],1,siz);
    48         while(m--){
    49             scanf("%d%d%d",&l,&r,&k);
    50             printf("%d
    ",hash[query(root[l-1],root[r],1,siz,k)]);
    51         }
    52     }
    53     return 0;
    54 }

    主席树 模板题

  • 相关阅读:
    Y+的一些讨论
    MATLAB中FFT的使用方法
    插入排序、冒泡排序、选择排序——转载
    输出控制中时间延迟的几种方法
    模拟通信调制方式与通信设备
    模拟通信主要特点
    模拟通信数字信号
    模拟通信
    传真存储变换设备与入网方式
    静止图像通信
  • 原文地址:https://www.cnblogs.com/suishiguang/p/6293015.html
Copyright © 2011-2022 走看看