zoukankan      html  css  js  c++  java
  • POJ 2104 K-th Number

    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 10 9 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.
     
    题意:很明显,前面的阐述十分冗余,只需要后面一些就可以了,就是求一个第k大的值。
    题解:这道题分块可以做,就是一般说的主席树,就是处理前缀和(个人觉得十分类似),然后就是模板的操作了。
    这道题作为主席树的模板十分合适。
    代码量不大,这段代码不支持修改,修改操作还等继续再等下一篇blog。
     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cmath>
     5 #include<cstring>
     6 #define N 100007
     7 
     8 using namespace std;
     9 
    10 struct Node
    11 {
    12     int l,r,rs,ls,sum;
    13 }tree[N*20];
    14 int rt[N],pos,cnt;
    15 int a[N],b[N];
    16 
    17 void build(int &node,int l,int r)
    18 {
    19     node=++cnt;//动态分配空间。 
    20     tree[node].l=l,tree[node].r=r;
    21     if (l==r) return;
    22     int mid=(l+r)>>1;
    23     build(tree[node].ls,l,mid);
    24     build(tree[node].rs,mid+1,r);
    25 }
    26 void insert(int pre,int &node)
    27 {
    28     node=++cnt;
    29     tree[node].ls=tree[pre].ls,tree[node].rs=tree[pre].rs;
    30     tree[node].l=tree[pre].l,tree[node].r=tree[pre].r;
    31     tree[node].sum=tree[pre].sum+1;
    32     if (tree[node].l==tree[node].r) return;
    33     int mid=(tree[node].l+tree[node].r)>>1;
    34     if (pos<=mid) insert(tree[pre].ls,tree[node].ls);//如果当前节点在左边,则只需要修改左子树就可以了。 
    35     else insert(tree[pre].rs,tree[node].rs);//如果当前节点在右边,则只需要修改右子树上的信息。 
    36 }
    37 int query(int pre,int node,int k)
    38 {
    39     if (tree[node].ls==tree[node].rs) return b[tree[node].l];//当左儿子等于右儿子时,就可返回唯一确定的值了。 
    40     int cmp=tree[tree[node].ls].sum-tree[tree[pre].ls].sum;//表示左子树中有多少节点。 
    41     if (k<=cmp) return query(tree[pre].ls,tree[node].ls,k);//左边节点数大于k,就到左子树中找。 
    42     else return query(tree[pre].rs,tree[node].rs,k-cmp);//否则就在右边。 
    43 }
    44 int main()
    45 {
    46     int n,q;
    47     scanf("%d%d",&n,&q);
    48     for (int i=1;i<=n;i++)
    49     {
    50         scanf("%d",&a[i]);
    51         b[i]=a[i];
    52     }
    53     sort(b+1,b+n+1);
    54     build(rt[0],1,n);
    55     for (int i=1;i<=n;i++)
    56     {
    57         pos=lower_bound(b+1,b+n+1,a[i])-b;//pos表示a[i]排名第几。
    58         insert(rt[i-1],rt[i]);//不断加入当前节点,来更新最新的一颗子树。 
    59     }
    60     int x,y,k;
    61     for (int i=1;i<=q;i++)
    62     {
    63         scanf("%d%d%d",&x,&y,&k);
    64         printf("%d
    ",query(rt[x-1],rt[y],k));//类似前缀和这样的方式去找区间第k大的值,很神奇、 
    65     }
    66 }
  • 相关阅读:
    列出python中可变数据类型和不可变数据类型,并简述原理
    python 字典操作
    Python的is和==
    python2和python3区别
    下面的代码在Python2中的输出是什么?解释你的答案
    编程用sort进行排序,然后从最后一个元素开始判断,去重
    如何在一个function里面设置一个全局的变量?
    用Python匹配HTML tag的时候,<.>和<.?>有什么区别?
    请写出一段Python代码实现删除一个list里面的重复元素
    什么是lambda函数?它有什么好处?
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/7531338.html
Copyright © 2011-2022 走看看