BC ZYB ' s premutation (segment tree + binary search)

Source: Internet
Author: User

Title Summary: There is a full permutation, always the inverse logarithm of each prefix interval, to restore this arrangement.

Fi Records in reverse order logarithm, PI records the position value, then k=fi-f (i-1) indicates the number of previous i-1 is larger than PI, then as long as the remaining elements in order to find the number of i-k numbers can be.

Segment tree + binary search, Segment tree bit[i] records the rank order of the remaining elements in I.

1 /*******************************2 3 date:2015-12-06 19:49:594 AUTHOR:WQJ ([email protected])5 Link:http://www.cnblogs.com/a1225234/6 Name:7 8 ********************************/9#include <iostream>Ten#include <cstdio> One#include <algorithm> A#include <cmath> -#include <cstring> -#include <string> the#include <Set> -#include <vector> -#include <queue> -#include <stack> + #defineLL Long Long - using namespacestd; + inta[ the+Ten]; A intbit[ the+Ten]; at intans[ the+Ten]; - intN; - intLowbit (inti) - { -     returni&-i; - } in intSuminti) - { to     ints=0; +      while(i>0) -     { thes+=Bit[i]; *i-=lowbit (i); $     }Panax Notoginseng     returns; - } the voidAddintIinta) + { A      while(i<=N) the     { +bit[i]+=A; -i+=lowbit (i); $     } $ } - intMain () - {     theFreopen ("In.txt","R", stdin); -     inti,j;Wuyi     intT; thescanf"%d",&T); -      while(t--) Wu     { -scanf"%d",&n); About          for(i=1; i<=n;i++) scanf ("%d",&A[i]);  $a[0]=0; -memset (bit,0,sizeof(bit)); -          for(i=1; i<=n;i++) Add (i,1);/*record the size of I in a tree-like array*/ -          for(i=n;i>=1; i--) A         { +             inttemp=a[i]-a[i-1]; theTemp=i-temp;//number of temp in the first row -             intL=1, r=N,mid; $             intk=R; the              while(r>=l) the             { theMid= (r+l)/2; the                 if(Sum (mid) >=temp) {k=mid;r=mid-1;} -                 ElseL=mid+1; in             } theans[i]=K; theAdd (ans[i],-1); About         } the          for(i=1; i<=n;i++) theprintf"%d%c", Ans[i],i==n?'\ n':' '); the     } +     return 0; -}

BC ZYB ' s premutation (segment tree + binary search)

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.