【POJ

来源:互联网 发布:js判断是否出现滚动条 编辑:程序博客网 时间:2024/06/05 13:26

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.

分析: 刚学的,多打几遍增强理解。

#include<algorithm>#include<cstdio>#include<cstring>#include<iostream> using namespace std;typedef pair<int,int>pii;#define first fi#define second se#define  LL long long#define fread() freopen("in.txt","r",stdin)#define fwrite() freopen("out.txt","w",stdout)#define CLOSE() ios_base::sync_with_stdio(false)const int MAXN = 100001+11;const int MAXM = 1e6;const int mod = 1e9+7;const int inf = 0x3f3f3f3f;int root[MAXN<<5],lson[MAXN<<5],rson[MAXN<<5],sum[MAXN<<5],sz;void Build(int &rt,int le,int ri){    rt=++sz; sum[rt]=0;    if(le==ri) return ;    int mid=(le+ri)>>1;    Build(lson[rt],le,mid);    Build(rson[rt],mid+1,ri);}void Update(int pre,int &rt,int le,int ri,int val){    rt=++sz;    sum[rt]=sum[pre]+1,lson[rt]=lson[pre],rson[rt]=rson[pre];    if(le==ri) return ;    int mid=(le+ri)>>1;    if(val<=mid) Update(lson[pre],lson[rt],le,mid,val);    else Update(rson[pre],rson[rt],mid+1,ri,val); }int Query(int st,int ed,int le,int ri,int k){    if(le==ri) return le;    int mid=(le+ri)>>1;    int t=sum[lson[ed]]-sum[lson[st]];    if(k<=t) Query(lson[st],lson[ed],le,mid,k);    else Query(rson[st],rson[ed],mid+1,ri,k-t);}int arr[MAXN],X[MAXN],cnt;int main(){    CLOSE();//  fread();//  fwrite();    int n,m;    while(scanf("%d%d",&n,&m)!=EOF){        cnt=0;        for(int i=1;i<=n;i++) {            scanf("%d",&arr[i]);            X[cnt++]=arr[i];         }         sort(X,X+cnt);cnt=unique(X,X+cnt)-X;         //for(int i=0;i<cnt;i++) printf("%d %d\n",X[i],arr[i]);         sz=0; Build(root[0],1,cnt);         for(int i=1;i<=n;i++){            int t=lower_bound(X,X+cnt,arr[i])-X+1;            Update(root[i-1],root[i],1,cnt,t);         }         while(m--){            int a,b,c;scanf("%d%d%d",&a,&b,&c);            int ans=Query(root[a-1],root[b],1,cnt,c);            printf("%d\n",X[ans-1]);         }     }    return 0;}
原创粉丝点击