Pku oj 2104(划分树)

来源:互联网 发布:配眼镜需要的数据 编辑:程序博客网 时间:2024/06/03 18:09
K-th Number
Time Limit: 20000MS Memory Limit: 65536KTotal Submissions: 48842 Accepted: 16489Case 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.

划分树模板题,区间第K大

#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <cstdlib>#include <cmath>#define lson(rt) rt<<1#define rson(rt) rt<<1|1#define cl(a) memset(a,0,sizeof(a))#define ss(a) scanf("%d",&a)using namespace std;const int N=100100;struct seg_tree{    int left;    int right;    int midd()    {        return (left+right)>>1;    }}tree[N<<3];int val[20][N],toleft[20][N],a[N];void build(int l,int r,int d,int rt){    int i;    tree[rt].left=l;    tree[rt].right=r;    if (l==r) return;    int mid=tree[rt].midd();    int lsame=mid-l+1,same=0;    for (i=l;i<=r;i++)        if (val[d][i]<a[mid]) lsame--;    int lpos=l,rpos=mid+1;    for (i=l;i<=r;i++)    {        int x=val[d][i];        if (i==l) toleft[d][i]=0;        else toleft[d][i]=toleft[d][i-1];        if (val[d][i]<a[mid])        {            toleft[d][i]++;            val[d+1][lpos++]=x;        }        else if (val[d][i]>a[mid])        {            val[d+1][rpos++]=x;        }        else if (same<lsame)        {            same++;            toleft[d][i]++;            val[d+1][lpos++]=x;        }        else val[d+1][rpos++]=x;    }    build(l,mid,d+1,lson(rt));    build(mid+1,r,d+1,rson(rt));}int query(int k,int l,int r,int d,int rt){    if (l==r) return val[d][l];    int s,ss,mid,le,newl,newr;    mid=tree[rt].midd();    le=tree[rt].left;    if (l==le)    {        ss=0;        s=toleft[d][r];    }    else    {        ss=toleft[d][l-1];        s=toleft[d][r]-toleft[d][l-1];    }    if (s>=k)    {        newl=le+ss;        newr=le+ss+s-1;        return query(k,newl,newr,d+1,lson(rt));    }    else    {        int bb=l-le-ss;        int b=r-l+1-s;        newl=mid+bb+1;        newr=mid+bb+b;        return query(k-s,newl,newr,d+1,rson(rt));    }}int main(){    int n,m,i;    while (~scanf("%d%d",&n,&m))    {        cl(a);cl(val);cl(toleft);        for (i=1;i<=n;i++)        {            scanf("%d",&a[i]);            val[0][i]=a[i];        }        sort(a,a+n+1);        build(1,n,0,1);        for (i=1;i<=m;i++)        {            int le,ri,k;            scanf("%d%d%d",&le,&ri,&k);            int res=query(k,le,ri,0,1);            printf("%d\n",res);        }    }    return 0;}


0 0
原创粉丝点击