K-th Number

题目链接:http://poj.org/problem?id=2104

K-th Number
Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 74919   Accepted: 26808
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.

Source

Northeastern Europe 2004, Northern Subregion
 
题目大意:求区间第K小的数
思路:主席树入门经典题
看代码:
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int maxn=1e5+5;
vector<int>v;
int cnt,root[maxn],a[maxn];
int sz;
struct Tree
{
    int l,r,sum;//左右儿子 多少个数
}T[maxn<<5];
void Init()
{
    cnt=0;
    T[cnt].l=T[cnt].r=T[cnt].sum=0;
    root[cnt]=0;
    v.clear();
}
int getid(int x)//去重后排第几
{
    return lower_bound(v.begin(),v.end(),x)-v.begin()+1;
}
void Update(int l,int r,int &x,int y,int pos)//x是新树 y是上一颗树
{
    T[++cnt]=T[y],T[cnt].sum++,x=cnt;//复制上一颗树 节点个数加一 记下根节点
    if(l==r) return ;//到了叶子节点
    int mid=(l+r)>>1;
    //pos是当前数在原数列中的大小排序
    //小于等于中间值 往左放
    if(pos<=mid) 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)>>1;
    int sum=T[T[y].l].sum-T[T[x].l].sum;//两者之间小于中间值的个数
    //个数大于等于K个 证明答案在小于mid的一边 也就是左边
    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()
{
    Init();
    int N,M;
    scanf("%d%d",&N,&M);
    for(int i=1;i<=N;i++)
    {
        scanf("%d",&a[i]);v.push_back(a[i]);
    }
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());//去重
    sz=v.size();
    for(int i=1;i<=N;i++) Update(1,sz,root[i],root[i-1],getid(a[i]));//按照输入顺序插入 建树
    int l,r,k;
    while(M--)
    {
        scanf("%d%d%d",&l,&r,&k);
        printf("%d\n",v[Query(1,sz,root[l-1],root[r],k)-1]);
    }
    return 0;
}

 

 另一种形式代码:

#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int maxn=1e5+5;
vector<int>v;
int sz;
int cnt,root[maxn],a[maxn];
struct Tree
{
    int l,r,sum;//左右儿子 多少个数
}T[maxn<<5];
void Init()
{
    cnt=0;
    T[cnt].l=T[cnt].r=T[cnt].sum=0;
    root[cnt]=0;
    v.clear();
}
int getid(int x)//去重后排第几
{
    return lower_bound(v.begin(),v.end(),x)-v.begin()+1;
}
int Update(int l,int r,int y,int pos)//x是新树 y是上一颗树
{
    int p=++cnt;
    T[p]=T[y],T[cnt].sum++;//复制上一颗树 节点个数加一 记下根节点
    if(l==r) return p;//到了叶子节点
    int mid=(l+r)>>1;
    //pos是当前数在原数列中的大小排序
    //小于等于中间值 往左放
    if(pos<=mid) T[p].l=Update(l,mid,T[y].l,pos);
    else T[p].r=Update(mid+1,r,T[y].r,pos);
    return p;
}
int Query(int l,int r,int x,int y,int k)
{
    if(l==r) return l;//到了叶子节点 答案肯定是叶子节点
    int mid=(l+r)>>1;
    int sum=T[T[y].l].sum-T[T[x].l].sum;//两者之间小于中间值的个数
    //个数大于等于K个 证明答案在小于mid的一边 也就是左边
    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()
{
    Init();
    int N,M;
    scanf("%d%d",&N,&M);
    for(int i=1;i<=N;i++)
    {
        scanf("%d",&a[i]);v.push_back(a[i]);
    }
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());//去重
    sz=v.size();

    for(int i=1;i<=N;i++) root[i]=Update(1,sz,root[i-1],getid(a[i]));//按照输入顺序插入 建树
    int l,r,k;
    while(M--)
    {
        scanf("%d%d%d",&l,&r,&k);
        printf("%d\n",v[Query(1,sz,root[l-1],root[r],k)-1]);
    }
    return 0;
}

 

 

posted @ 2019-07-29 19:30  执||念  阅读(169)  评论(0编辑  收藏  举报