In poj 2104, the topic of the K largest chair tree in the search sequence

Source: Internet
Author: User

Given a sequence of n, m queries are asked each time to find the number K in the l-R.

Only ask, not modified

You can use the merge tree and divide tree (I have never learned .. Token)

I specifically directed at the Chairman tree.

I have a little understanding of the Building Method of the Chairman tree, but I still don't understand why I did this in the Chair tree. Today I have to go to multiple schools after I typed it in the template.

Study again

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=100010;const int maxm=maxn*30;int n,m,tot,s;int A[maxn],t[maxn],T[maxn];int c[maxm],lson[maxm],rson[maxm];int build(int l,int r){    int rt=++tot;    c[rt]=0;    if (l>=r) return rt;    int mid=(l+r)>>1;    lson[rt]=build(l,mid);    rson[rt]=build(mid+1,r);    return rt;}int update(int rt,int pos,int val){   int newrt=++tot,tmp=newrt;   c[newrt]=c[rt]+val;   int l=1,r=s;   while (l<r)   {       int mid=(l+r)>>1;       if (pos<=mid){           r=mid;           lson[newrt]=++tot;rson[newrt]=rson[rt];           newrt=lson[newrt];rt=lson[rt];       }       else{           l=mid+1;           rson[newrt]=++tot;lson[newrt]=lson[rt];           newrt=rson[newrt];rt=rson[rt];       }       c[newrt]=c[rt]+val;   }   return tmp;}int query(int lrt,int rrt,int k){    int l=1,r=s;    while (l<r)    {        int mid=(l+r)>>1;        if (c[lson[lrt]]-c[lson[rrt]]>=k){            r=mid;            lrt=lson[lrt];            rrt=lson[rrt];        }        else        {            l=mid+1;            k-=c[lson[lrt]]-c[lson[rrt]];            lrt=rson[lrt];            rrt=rson[rrt];        }    }    return l;}int main(){    while (scanf("%d%d",&n,&m)!=EOF)    {        tot=0;        for (int i=1;i<=n;i++) scanf("%d",&A[i]),t[i]=A[i];        sort(t+1,t+1+n);        s=unique(t+1,t+1+n)-(t+1);        T[n+1]=build(1,m);        for (int i=n;i>=1;i--){            int pos=lower_bound(t+1,t+n+1,A[i])-t;            T[i]=update(T[i+1],pos,1);        }        while (m--)        {            int l,r,k;            scanf("%d%d%d",&l,&r,&k);            int ans=query(T[l],T[r+1],k);            printf("%d\n",t[ans]);        }    }    return 0;}

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.