poj K-th Number(主席树入门题)

来源:互联网 发布:电气控制仿真设计软件 编辑:程序博客网 时间:2024/06/08 08:51

题目链接
K-th Number
Time Limit: 20000MS Memory Limit: 65536KTotal Submissions: 52450 Accepted: 18020Case 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 31 5 2 6 3 7 42 5 34 4 11 7 3

Sample Output

563

Hint

This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.

Source

Northeastern Europe 2004, Northern Subregion


题意:

给出n和m,然后一行是n个数,接着m次询问,每个询问是l,r,k,输出区间[l,r]第k大的数。


#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#include<queue>#include<stack>using namespace std;#define rep(i,a,n) for (int i=a;i<n;i++)#define per(i,a,n) for (int i=n-1;i>=a;i--)#define pb push_back#define fi first#define se secondtypedef vector<int> VI;typedef long long ll;typedef pair<int,int> PII;const int inf=0x3fffffff;const ll mod=1000000007;const int maxn=100000+100;int n,m,l,r,k,cnt,root[maxn],nn;int a[maxn],h[maxn];struct node{    int l,r,sum;  //l,r分别是左右子树根节点,sum为个数}T[maxn*40];int getid(int x){return (int)(lower_bound(h,h+nn,x)-h+1);}void update(int l,int r,int &x,int y,int pos){    T[++cnt]=T[y];T[cnt].sum++;x=cnt;    if(l==r) return;    int mid=(l+r)/2;    if(mid>=pos) update(l,mid,T[x].l,T[y].l,pos);    else update(mid+1,r,T[x].r,T[y].r,pos);}int query(int l,int r,int x,int y,int k){    if(l==r) return l;    int mid=(l+r)/2;    int sum=T[T[y].l].sum-T[T[x].l].sum;    if(sum>=k) return query(l,mid,T[x].l,T[y].l,k);    else return query(mid+1,r,T[x].r,T[y].r,k-sum);}int main(){    scanf("%d%d",&n,&m);    rep(i,1,n+1) scanf("%d",&a[i]),h[i-1]=a[i];    sort(h,h+n);    nn=(int)(unique(h,h+n)-h);    rep(i,1,n+1) update(1,n,root[i],root[i-1],getid(a[i]));    while(m--)    {        scanf("%d%d%d",&l,&r,&k);        printf("%d\n",h[query(1,n,root[l-1],root[r],k)-1]); //    }        return 0;}


0 0
原创粉丝点击