POJ_2104_KTH (Chairman Tree)

Source: Internet
Author: User
Tags repetition

Describe

http://poj.org/problem?id=2104

Give a number of n number of series, M inquiry, each query to find the interval [l,r] in the K small numbers, no modification operation.

Analysis

Static chairman tree with bare title.

First consider discretization of the data, so that a total number of n, respectively,,..., n-1,n (if there is no repetition) (if there is repetition in the title and repeat the number of the same position, go to the weight is good). Use N=n line segment tree to represent the current situation of an interval, where node a[k] The expression is within this interval, belonging to [A[k]. L,A[K]. R] number. So that the operation of the K decimal in this interval is similar to the operation on the balance tree, to a node, if the number of left child a[a[k].l].s>=k, then the number of K is in the left child range, Otherwise, it's in the right child zone. Such a segment tree can represent an interval in a sequence. That requires any interval, each interval has its own line of tree? It is not necessary. Similar to the value of arbitrary interval, you can use the prefix and the idea that each segment tree represents the interval from the first number to the number of numbers in a series, This requires only n tree segments. The shape, size, and meaning of each segment tree is the same, so it belongs to [a[k] in the sequence range [L,r]. L,A[K]. R] is the number of numbers in the series interval [1,r] minus the number in the sequence interval [1,l-1], so the two prefixes and segment tree subtraction can be obtained in the number of series interval [L,r].

The above is the basic idea to solve the problem.

But the N-tree segment requires n^2-level space, and the MLE is the Chairman tree (which can be persisted). (In fact, I don't quite understand the meaning of this name)

For the segment tree that represents the sequence interval [1,i], it is more than a number a[i for the segment tree that represents the sequence interval [1,i-1], that is, the S value is +1 on the node of A[i], and to update upward, all nodes that contain a[i] are updated, so that the update is from top to bottom a whole chain , and the other points do not change, so there is no time to update the Logn point only (the first time is also). So the N-times to build a total need to nlogn points, space is enough (open space before you calculate the log).

P.S.

1. My writing is to change its relevant values before entering a node, you can also use the reference in the parameters of the function, after entering a node and then modify, more concise, but beginners or the following is a better understanding of the wording.

2. Learning a new discretization, you can also use a struct to put the ID array and a array together, the effect is the same, but this is more concise. The complexity of the preprocessing is O (n), of course, if there are repeated words written in the structure is good to weight. Of course, you can also directly sort the weight, then use O (nlogn Time to find it again, so you don't need an array of IDs.

#include <cstdio>#include<algorithm>using namespacestd;Const intmaxn=100000+5;intn,m,cnt;intA[MAXN],B[MAXN],ID[MAXN],ROOT[MAXN];structnode{intL,r,x; }t[maxn* -];voidUpdateintLintRintNowintPreintx) {    if(L==R)return; intmid=l+ (r-l)/2; if(x<=mid) {T[NOW].L=++CNT; T[NOW].R=T[PRE].R; t[t[now].l].x=t[t[pre].l].x+1;    Update (L,MID,T[NOW].L,T[PRE].L,X); }    Else{T[NOW].L=T[PRE].L; T[NOW].R=++CNT; t[t[now].r].x=t[t[pre].r].x+1; Update (Mid+1, r,t[now].r,t[pre].r,x); }}intQueryintLintRintXintYintk) {    if(L==R)returnl; intmid=l+ (r-l)/2; ints=t[t[y].l].x-t[t[x].l].x; if(k<=s)returnquery (L,MID,T[X].L,T[Y].L,K); Else returnQuery (mid+1, r,t[x].r,t[y].r,k-s);}BOOLcmpintXintY) {returna[x]<a[y];}intMain () {scanf ("%d%d",&n,&m);  for(intI=1; i<=n;i++) scanf ("%d", &a[i]), id[i]=i; Sort (ID+1, id+n+1, CMP);  for(intI=1; i<=n;i++) b[id[i]]=i;  for(intI=1; i<=n;i++) {Root[i]=++CNT; Update (1, n,cnt,root[i-1],b[i]); }     while(m--){        intx,y,k; scanf ("%d%d%d",&x,&y,&k); printf ("%d\n", A[id[query (1, n,root[x-1],root[y],k)]); }    return 0;}
View Code

P.S.

1. Suddenly think of the second kind of discretization method used in the original was to think out of ... Although not very easy to use, but Noip used to study and practice has been using their own things, the most naïve LCA algorithm is also their own thinking, and later found that the book is exactly the same. Now Thusc and Noi near, have been thinking of time to learn a little more, this idea is right, to seize the time, Not too lazy, but sometimes it is too utilitarian, in fact, they are aware of their basic lack of hope, but this is after all, they love and want to do things. Even if the last prize is not, again and again soy sauce what? His own strength is really far away, there is a dream is right, but not too utilitarian, I should be lucky to be able to keep chasing dreams. I should not have been so impetuous and quiet. I should seize the time to enjoy my own oi, to do what I want to do, while I still have the opportunity. So important: don't give up thinking.

POJ_2104_KTH (Chairman Tree)

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.