HDU2665 Kth Number (partition tree template)

Source: Internet
Author: User

Test instructions: Give you a number, each time to give you an interval, to find the number of K-Large section

#include <bits/stdc++.h>using namespacestd;Const intmaxn=100010;inttree[ -][MAXN];//value that represents each position in each layerintSORTED[MAXN];//the number that has been sorted wellinttoleft[ -][MAXN];//Toleft[p][i] Indicates that layer I has several points from 1 to I into the leftvoidBuildintLintRintDEP) {    if(L==R)return; intMid= (l+r) >>1; intsame=mid-l+1;//represents equal to the middle value and is divided into the left number     for(intI=l; i<=r; i++)//Note that L, not one.        if(Tree[dep][i]<sorted[mid]) same--; intlpos=l; intRpos=mid+1;  for(intI=l; i<=r; i++)    {        if(Tree[dep][i]<sorted[mid]) tree[dep+1][lpos++]=Tree[dep][i]; Else if(tree[dep][i]==sorted[mid]&&same>0) {TREE[DEP+1][lpos++]=Tree[dep][i]; Same--; }        Elsetree[dep+1][rpos++]=Tree[dep][i]; Toleft[dep][i]=toleft[dep][l-1]+lpos-l; } Build (L,mid,dep+1); Build (Mid+1, r,dep+1);}//query interval k large number, [L,r] is a large interval, [l,r] is to query the inter-communityintQueryintLintRintLintRintDepintk) {    if(L==R)returnTree[dep][l]; intMid= (l+r) >>1; intcnt=toleft[dep][r]-toleft[dep][l-1]; if(cnt>=k) {intnewl=l+toleft[dep][l-1]-toleft[dep][l-1]; intnewr=newl+cnt-1; returnQuery (l,mid,newl,newr,dep+1, K); }    Else    {        intnewr=r+toleft[dep][r]-Toleft[dep][r]; intnewl=newr-(r-l-CNT); returnQuery (mid+1, r,newl,newr,dep+1, K-CNT); }}intMain () {//freopen ("In.txt", "R", stdin);    intT; scanf ("%d",&t);  while(t--)    {        intn,m;  while(~SCANF ("%d%d",&n,&m) {memset (tree,0,sizeof(tree));  for(intI=1; i<=n; i++) {scanf ("%d", &tree[0][i]); Sorted[i]=tree[0][i]; } sort (sorted+1, sorted+n+1); Build (1N0); ints,t,k;  while(m--) {scanf ("%d%d%d",&s,&t,&k); printf ("%d\n", Query (1, N,s,t,0, K)); }        }    }    return 0;}

HDU2665 Kth Number (partition tree template)

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.