"POJ" "2104" interval K-Large

Source: Internet
Author: User

Can persist segment tree

A durable segment tree is a magical data structure that differs from our usual line-of-segment tree, where each update does not change the original data, but rather opens a new node, maintaining its historical version and achieving "persistence". (Of course, depending on the situation will also need to modify the time)

There are many applications for the persistence of segment trees, and this data structure is introduced only in the simple question of the interval K-large.

Our original line-segment tree is a representation of the interval, or the maintenance of "location", the existence of a variety of information at each location. Its advantage is to satisfy the interval addition, but does not satisfy the interval subtraction, so we have to change a way of doing this: for each interval [1,i] to establish a weight segment tree. The function of this segment tree is actually the same as the prefix and the same as the prefixes and satisfies the interval subtraction! But we keep sum when we ask for the prefix, and the weight segment tree saves all the values.

Here's how it's preserved: for [1,x] This node, it needs to maintain a CNT value, which indicates the number of CNT in the [1,x] domain.

For a chestnut, we now have a sequence {1,2,3,4,5,2,3,3,3,3}

Then for the segment tree that represents the interval [1,10], its nodes are built like this

      

As you can see, the value in [1,5] has 10 numbers, and there are 3 numbers in [] ... And so on

So when we query the size K, we can be like the balance tree! If the left son's cnt>=k on the left to find, otherwise on the right side, then we can successfully query to K big ~

So the question is: what should I do if I want to query the 3rd-largest number on this interval [3,7]? (This place is easy to faint, be sure to distinguish between the original sequence and the range, although the square brackets are expressed in the interval ...) If you look at this sentence more dizzy, then forget it.

So we'll go back to what we said before, "prefix and" come up, we used to quickly seek [l,r] interval and, is the use of prefix and [1,l-1] and [1,r] interval subtraction fast calculation, in the same way, we can also use [1,l-1] and [1,r] two segment tree to do the interval k large query. Go down at the same time on both trees! See the code.

1 //POJ 21042#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <iostream>6#include <algorithm>7 #defineRep (i,n) for (int i=0;i<n;++i)8 #defineF (i,j,n) for (int i=j;i<=n;++i)9 #defineD (i,j,n) for (int i=j;i>=n;--i)Ten using namespacestd; One Const intn=100086; A //#define DEBUG -  - structnode{ the     intX,num,rank; - }a[n]; - BOOLcmpx (node A,node b) { -     returna.x<b.x; + } - BOOLCMPN (node A,node b) { +     returna.num<B.num; A } at  - structtree{ -     intCnt,l,r; -}t[n* -]; - intRoot[n],cnt=0, n,m; -  in #defineMid (L+r>>1) - voidUpdata (int&o,intLintRintPOS) { toT[++cnt]=t[o], o=cnt, + +t[o].cnt; +     if(L==R)return; -     if(pos<=mid) Updata (T[o].l,l,mid,pos); the     ElseUpdata (t[o].r,mid+1, R,pos); * #ifdef Debug $printf"%d %d%d%d\n", O,l,r,pos);Panax Notoginseng     #endif - } the  + intQueryintIintJintrank) { Ai=root[i],j=Root[j]; the     intL=1, r=N; +      while(l!=R) { -         if(T[t[j].l].cnt-t[t[i].l].cnt>=rank)//go down together on two trees $r=mid,i=t[i].l,j=T[J].L; $         Else{ -rank-=t[t[j].l].cnt-t[t[i].l].cnt; -L=mid+1, i=t[i].r,j=T[J].R; the         } -     }Wuyi     returnl; the } - #undefMid Wu  - intMain () { AboutFreopen ("file.in","R", stdin); $scanf"%d%d",&n,&m); -     intx=0; -F (I,1, n) {scanf ("%d", &a[i].x); a[i].num=i;} -Sort (A +1, a+n+1, cmpx); AF (I,1, N) a[i].rank=i; +Sort (A +1, a+n+1, CMPN); theF (I,1, N) { -root[i]=root[i-1]; $Updata (Root[i],1, N,a[i].rank);//This can not be understood first ... the         //Simply put: To save space, we don't really need to build a complete line tree for each interval . the         //It is possible to perform new maintenance on the original basis (that is, the original version of history) the     } theSort (A +1, a+n+1, cmpx); -F (I,1, M) { in         intl,r,k; thescanf"%d%d%d",&l,&r,&k); theprintf"%d\n", A[query (l1, R,k)]. x); About     } the     return 0; the}
View Code

"POJ" "2104" interval K-Large

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.