【POJ 2104】K-th Number 题意&题解&代码(c++)

来源:互联网 发布:可以标记的地图软件 编辑:程序博客网 时间:2024/04/29 12:46

K-th Number


Time Limit: 20000MS Memory Limit: 65536K
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.


中文题意


非常简洁:给出一数组a,然后要求查询从 a[i] 到 a[j] 的第k大的数。


题解:需要运用到主席树这种坑爹算法。可以到网上了解一下。
我的代码中也有相应的注释。

推荐网站站:
http://www.cnblogs.com/AbandonZHANG/archive/2012/09/27/2706000.html


代码:


#include<algorithm>#include<iostream>#include<stdio.h>#include<map>using namespace std;int tot=0,root[100005];//tot记录节点的编号,root[i]表示第i棵线段树根节点的编号map<int,int>hash;map<int,int>mp;//了解主席树之后可以知道主席树所占用的内存是很大的因此需要hash后建树//这样可以尽可能的减少空间的使用struct edge{    int l;int r;//表示线段树上这个节点所代表区间l~r    int ls;int rs;//表示这个节点左儿子和右儿子的编号    int val;//这个节点所代表的区间中有多少个数}tree[2000005];int build_tree(int l,int r)//建最原始的空树{    tot++;    int k=tot;    tree[k].l=l;    tree[k].r=r;    tree[k].val=0;    if (l==r) return k;    int mid=(l+r)/2;    tree[k].ls=build_tree(l,mid);    tree[k].rs=build_tree(mid+1,r);    return k;}int add_tree(int id,int x,int v)//建新树{    tot++;    int k=tot;    tree[k]=tree[id];    //每次只改变 从需要更改的节点 到 根结点 的一条链    //因此 另一个儿子 可以直接等于 前一个线段树 的 儿子    //这样可以减少许多的空间    tree[k].val+=v;    if ( tree[id].r==x && tree[id].l==x )    return k;    int mid=(tree[id].l+tree[id].r)/2;    if (x<=mid)//判断需要改哪条链    tree[k].ls=add_tree(tree[id].ls,x,v);    else    tree[k].rs=add_tree(tree[id].rs,x,v);    return k;}int query_tree(int ne,int no,int w){    int tmp=tree[tree[no].ls].val-tree[tree[ne].ls].val;    //建好n棵线段树之后可以发现第i棵线段树与第i+1棵线段树的区别是    //是否在线段树中加了第i+1个点    //因此要 求区间l和r中所出现的数字 可以用tree[r]与tree[l-1]做差    //找到做差后第k个数字即可    if (tree[no].l==tree[no].r)    return tree[no].l;    if (tmp>=w)    return query_tree(tree[ne].ls,tree[no].ls,w);    else    return query_tree(tree[ne].rs,tree[no].rs,w-tmp);}int n,m,p[200005],s[200005];int main(){    scanf("%d",&n);    scanf("%d",&m);    for (int i=1;i<=n;i++)    {        scanf("%d",&p[i]);        s[i]=p[i];    }    sort(p+1,p+1+n);    int num=0;    for (int i=1;i<=n;i++)    {        if (hash[p[i]]==0)        {            num++;            hash[p[i]]=num;            mp[num]=p[i];        }       }    root[0]=build_tree(1,num);    for (int i=1;i<=n;i++)    {        int cha=hash[s[i]];        root[i]=add_tree(root[i-1],cha,1);    }    for (int i=1;i<=m;i++)    {        int a,b,c;        scanf("%d%d%d",&a,&b,&c);        int ans=query_tree(root[a-1],root[b],c);        printf("%d\n",mp[ans]);    }}
0 0