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

    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 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.       
    分析:可持久化线段树,即主席树;
       n棵线段树,每棵前缀线段树记录的是一段区间元素出现的次数;
       要求区间第K大,只需求出L-1棵线段树<=x的个数,R棵线段树<=x的个数,比较差值与K的大小,二分x即可;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, rt<<1
    #define Rson mid+1, R, rt<<1|1
    const int maxn=1e5+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    int n,m,k,t,sz;
    int a[maxn],b[maxn],ls[maxn*100],rs[maxn*100],s[maxn*100],root[maxn];
    void insert(int l,int r,int x,int &y,int v)
    {
        y=++sz;
        s[y]=s[x]+1;
        if(l==r)return;
        ls[y]=ls[x],rs[y]=rs[x];
        int mid=l+r>>1;
        if(v<=mid)insert(l,mid,ls[x],ls[y],v);
        else insert(mid+1,r,rs[x],rs[y],v);
    }
    int query(int l,int r,int x,int y,int k)
    {
        if(l==r)return l;
        int mid=l+r>>1;
        if(s[ls[y]]-s[ls[x]]>=k)return query(l,mid,ls[x],ls[y],k);
        else return query(mid+1,r,rs[x],rs[y],k-(s[ls[y]]-s[ls[x]]));
    }
    int main()
    {
        int i,j;
        scanf("%d%d",&n,&m);
        rep(i,1,n)scanf("%d",&a[i]),b[i]=a[i];
        sort(b+1,b+n+1);
        int num=unique(b+1,b+n+1)-b-1;
        rep(i,1,n)a[i]=lower_bound(b+1,b+num+1,a[i])-b;
        rep(i,1,n)insert(1,num,root[i-1],root[i],a[i]);
        rep(i,1,m)
        {
            int c,d,e;
            scanf("%d%d%d",&c,&d,&e);
            printf("%d
    ",b[query(1,num,root[c-1],root[d],e)]);
        }
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    Python的Crypto模块使用:自动输入Shell中的密码
    算法之动态规划初步(Java版)
    基于ZXing的二维码,你可以这样改造它
    Java字符编码的转化问题
    第一个Hadoop程序——WordCount
    Hadoop的学习前奏(二)——Hadoop集群的配置
    Linux下的一些问题收集及解决方法(二)
    Python的捕虫笔记
    Hadoop的学习前奏(一)——在Linux上安装与配置Hadoop
    Android SnackBar:你值得拥有的信息提示控件
  • 原文地址:https://www.cnblogs.com/dyzll/p/5919701.html
Copyright © 2011-2022 走看看