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)