zoukankan      html  css  js  c++  java
  • POJ 2104 K-th Number(主席树)

    K-th Number
    Time Limit: 20000MS   Memory Limit: 65536K
    Total Submissions: 57427   Accepted: 19856
    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<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<queue>
     6 #include<algorithm>
     7 using namespace std;
     8 const int MAXN=10000001;
     9 void read(int &n)
    10 {
    11     char c='+';int x=0;bool flag=0;
    12     while(c<'0'||c>'9')
    13     {c=getchar();if(c=='-')flag=1;}
    14     while(c>='0'&&c<='9')
    15     {x=x*10+c-48,c=getchar();}
    16     flag==1?n=-x:n=x;
    17 }
    18 int n,m;
    19 int a[MAXN];
    20 int hash[MAXN];
    21 int root[MAXN/2];
    22 int now,tot;// 所有节点的总出现次数 
    23 int ls[MAXN],rs[MAXN],cnt[MAXN];
    24 void build(int &cur,int l,int r)
    25 {
    26     cur=tot++;// 总的节点数量 
    27     cnt[cur]=0; 
    28     if(l!=r)
    29     {
    30         int mid=(l+r)/2;
    31          build(ls[cur],l,mid);
    32          build(rs[cur],mid+1,r);    
    33     }
    34 }
    35 void update(int pre,int pos,int &cur,int l,int r)
    36 {
    37     cur=tot++;
    38     cnt[cur]=cnt[pre]+1;
    39     ls[cur]=ls[pre];rs[cur]=rs[pre];
    40     if(l==r)
    41         return ;
    42     int mid=(l+r)/2;
    43     if(pos<=mid)
    44         update(ls[pre],pos,ls[cur],l,mid);
    45     else    
    46         update(rs[pre],pos,rs[cur],mid+1,r);
    47 }
    48 int query(int lt,int rt,int l,int r,int k)
    49 {
    50     if(l==r)
    51         return l;
    52     int now=cnt[ls[rt]]-cnt[ls[lt]];
    53     int mid=(l+r)/2;
    54     if(k<=now)
    55         return query(ls[lt],ls[rt],l,mid,k);
    56     else 
    57         return query(rs[lt],rs[rt],mid+1,r,k-now);
    58 }
    59 int main()
    60 {
    61     while(scanf("%d%d",&n,&m)==2)
    62     {
    63         for(int i=1;i<=n;i++)
    64         {
    65             read(a[i]);
    66             hash[i]=a[i];
    67         }
    68         sort(hash+1,hash+n+1);
    69         int size=unique(hash+1,hash+n+1)-hash-1;
    70         for(int i=1;i<=n;i++)
    71             a[i]=lower_bound(hash+1,hash+size+1,a[i])-hash;// 排序+离散化 
    72         
    73         tot=0;
    74         build(root[0],1,size);
    75         
    76         for(int i=1;i<=n;i++)
    77             update(root[i-1],a[i],root[i],1,size);// 建出所有的树 
    78         
    79         for(int i=1;i<=m;i++)
    80         {
    81             int x,y,z;
    82             read(x);read(y);read(z);
    83             printf("%d
    ",hash[query(root[x-1],root[y],1,size,z)]);
    84         }    // 查询 
    85     }
    86     
    87     return 0;
    88 }
     
  • 相关阅读:
    hive sql 解析json
    解决华为手机无法安装未签名apk问题(该安装包未包含任何证书)
    对马尔科夫决策过程的代码补充解释
    对马尔科夫决策过程MDP(Markov Decision Processes)的一点理解
    使用Web在PC和安卓之间传输文件(Transfer files via wifi)
    记录下自己的生活状态,昏昏沉沉的半年,迷茫的未来
    __repr__和pass在python中的含义
    LaTeX基础调节,调节行距,字体大小,字体,页边距
    LaTeX怎么让一行中的一部分右对齐
    Tkinter主动刷新(强制刷新)
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/7137467.html
Copyright © 2011-2022 走看看