poj2182 Lost Cows (segment tree)

Source: Internet
Author: User

Transmission Door
The main idea: there is a 1-n arrangement, the data given from the second to the nth number of each number in front of a few numbers smaller than this number, required to restore the arrangement of this 1-n
Idea: Recently do this kind of problem seems to feel a little, it is natural to think, we can from the last number to reverse, has been launched the first after the end can be finished. In that case, it's easy to write a N2 The algorithm. But as a oier, we ask for progress, so we will think, can optimize it. In fact, we found that there are several operations in the front that are smaller than the target value are redundant, we can consider using line tree to optimize. Then we'll be able to O(N Log N ) To find out the whole sequence in time.

Code:

#include <cstdio>#define MAXN 8005structnode{intL, R, Len;} T[MAXN *4];voidBuild (intIintLintr) {t[i].l = L, T[I].R = R; T[i].len = r-l+1;if(L = = r)return;intMid = (L + r) >>1; Build (i<<1, L, mid); Build (i<<1|1, Mid +1, r);}intQ (intIintnum) {--T[i].len;if(T[I].L = = T[I].R)returnT[I].L;if(Num > t[i<<1].len)returnQ (i<<1|1, num-t[i<<1].len);Else returnQ (i<<1, num);}intN, A[maxn], ANS[MAXN];intMain () {scanf("%d", &n); Build (1,1, n); for(inti =2; I <= N; i + +)scanf("%d", &a[i]); for(inti = n; i >0; I--) ans[i] = Q (1, A[i] +1); for(inti =1; I <= N; i + +)printf("%d\n", Ans[i]);}

Copyright NOTICE: Please feel free to reprint O (∩_∩) o

poj2182 Lost Cows (segment 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.