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

    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

    趁热赶紧来道主席树裸题,感觉有些明白了
    主席树,可持久化线段树,核心思想就是维护线段树的历史状态
    在这道题中,我们以数字大小为位置,位置上存的是出没出现过(1/0),统计加和
    我们依次将数字加入线段树,这样就会形成n个历史版本
    再加上我们维护的数据具有可减性,所以对于(l,r),每个节点位置上只要用r这颗线段树减去l线段树对应的值,就是相当于只维护了(l,r)这个区间内的元素的线段树,自然就可以在上面愉快的跳跃了~
    这次的主席树大部分是自己写的了2333,看看吧~
     1 #include<cstdio>
     2 #include<algorithm>
     3 #define N 100005
     4 using namespace std;
     5 int n,m,siz,tot;
     6 int a[N],b[N],rt[N];
     7 struct node{
     8     int l,r,sum;
     9 }t[20*N];
    10 int insert(int k,int x,int l,int r){
    11     t[++tot]=t[k];k=tot;
    12     if(l==r){
    13         t[k].l=t[k].r=0;
    14         t[k].sum++;
    15         return tot;
    16     }
    17     int mid=(l+r)>>1;
    18     if(x<=mid) t[k].l=insert(t[k].l,x,l,mid);
    19     else t[k].r=insert(t[k].r,x,mid+1,r);
    20     t[k].sum=t[t[k].l].sum+t[t[k].r].sum;
    21     return k;
    22 }
    23 int query(int lk,int rk,int x,int l,int r){
    24     if(l==r)return l;
    25     int mid=(l+r)>>1;
    26     if(t[t[rk].l].sum-t[t[lk].l].sum>=x)return query(t[lk].l,t[rk].l,x,l,mid);
    27     else return query(t[lk].r,t[rk].r,x-(t[t[rk].l].sum-t[t[lk].l].sum),mid+1,r);
    28 }
    29 int main(){
    30     scanf("%d%d",&n,&m);
    31     for(int i=1;i<=n;i++)scanf("%d",&a[i]),b[i]=a[i];
    32     sort(b+1,b+1+n);
    33     siz=unique(b+1,b+1+n)-b-1;
    34     for(int i=1;i<=n;i++)a[i]=lower_bound(b+1,b+1+n,a[i])-b;
    35     tot=0;
    36     for(int i=1;i<=n;i++)
    37         rt[i]=insert(rt[i-1],a[i],1,n);
    38     for(int i=1,l,r,k;i<=m;i++){
    39         scanf("%d%d%d",&l,&r,&k);
    40         printf("%d
    ",b[query(rt[l-1],rt[r],k,1,n)]);
    41     }
    42     return 0;
    43 }
    View Code
  • 相关阅读:
    android selector下的设置背景属性值
    成功必备的15种心态
    saveInstallState参数使用详解(android activity状态保存和恢复)
    14个坏习惯可能让你丢掉工作
    如何找到好书?有什么技巧或建议?
    [转]Git详解之一 Git起步
    程序员技术练级攻略
    Sina微博OAuth2框架解密
    程序员的八个级别
    Android中的Layout_weight详解
  • 原文地址:https://www.cnblogs.com/2017SSY/p/10179406.html
Copyright © 2011-2022 走看看