zoukankan      html  css  js  c++  java
  • 主席树:POJ2104 K-th Number (主席树模板题)

    K-th Number
    Time Limit: 20000MS   Memory Limit: 65536K
    Total Submissions: 44952   Accepted: 14951
    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.
     
      这里要求序列的区间第K大,没有修改操作,于是作主席树模板打了~~~
      引用主席树介绍:
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 using namespace std;
     5 struct Node{
     6     int a,b,rs,ls,sum;
     7 }tr[2000010];
     8 int a[100010],b[100010];
     9 int rt[100010],pos,cnt;
    10 void Build(int &node,int a,int b)
    11 {
    12     node=++cnt;
    13     tr[node].a=a;
    14     tr[node].b=b;
    15     if(a==b)return;
    16     int mid=(a+b)>>1;
    17     Build(tr[node].ls,a,mid);
    18     Build(tr[node].rs,mid+1,b);
    19 }
    20 
    21 void Insert(int pre,int &node)
    22 {
    23     node=++cnt;
    24     tr[node].ls=tr[pre].ls;
    25     tr[node].rs=tr[pre].rs;
    26     tr[node].a=tr[pre].a;
    27     tr[node].b=tr[pre].b;
    28     tr[node].sum=tr[pre].sum+1;
    29     if(tr[node].a==tr[node].b)return;
    30     int mid=(tr[node].a+tr[node].b)>>1;
    31     if(mid>=pos)Insert(tr[pre].ls,tr[node].ls);
    32     else Insert(tr[pre].rs,tr[node].rs);
    33 }
    34 int Query(int pre,int node,int k)
    35 {
    36     if(tr[node].ls==tr[node].rs)return b[tr[node].a];
    37     int cmp=tr[tr[node].ls].sum-tr[tr[pre].ls].sum;
    38     if(cmp>=k)return Query(tr[pre].ls,tr[node].ls,k);
    39     else return Query(tr[pre].rs,tr[node].rs,k-cmp);
    40 }
    41 int main()
    42 {
    43     int n,q;
    44     scanf("%d%d",&n,&q);
    45     for(int i=1;i<=n;b[i]=a[i],i++)
    46         scanf("%d",&a[i]);
    47     sort(b+1,b+n+1);
    48     Build(rt[0],1,n);
    49     for(int i=1;i<=n;i++)
    50     {
    51         pos=lower_bound(b+1,b+n+1,a[i])-b;
    52         Insert(rt[i-1],rt[i]);
    53     }    
    54     int l,r,k;
    55     for(int i=1;i<=q;i++)
    56     {
    57         scanf("%d%d%d",&l,&r,&k);
    58         printf("%d
    ",Query(rt[l-1],rt[r],k));
    59     }
    60     return 0;
    61 }
    尽最大的努力,做最好的自己!
  • 相关阅读:
    linux 查看僵尸进程
    apache 修改最大连接数
    ng压缩后,形参被简化问题解决:
    $rootScope 与 $scope 区别:$rootScope对象是“单例的”——一个模块中只有一个$rootScope;
    vue+webpack在“双十一”导购产品的技术实践
    过滤指令:number currency lowercase(转为小写)/uppercase(转为大写)
    ng-show ng-disabled ng-show ng-hide 值的布尔值改变时,状态发生变化
    二,数据双向绑定
    一、angular:module-Controller-Model-View (模块-控制器-模型-视图);异步请求$http
    5、面向对象
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5202042.html
Copyright © 2011-2022 走看看