Bestcoder round #65 && hdu 5592 ZYB ' s premutation segment tree

Source: Internet
Author: User

Time limit:2000/1000 MS (java/others) Memory limit:131072/131072 K (java/others)
Total submission (s): 175 Accepted Submission (s): 74


Problem DescriptionZYB Has a premutationP, but he is remeber the reverse log of each prefix of the premutation,now he ask you
Restore the premutation.

Pair(i,J)(i<j) is considered as a reverse log if ai>aJ is matched.

Inputin The first line there is the number of testcases T.

For each teatcase:

In the first line there is one numberN.

In the next line there isNNumbersAi , describe the number of the reverse logs of each prefix,

The input is correct.

1≤T≤5,1≤N≤50000

Outputfor each testcase,print the ans.

Sample Input130 1 2

Sample OUTPUT3 1 2

Sourcebestcoder Round #65

Idea: It is easy to think k = (A[i]-a[i-1] + 1) is the number of the original sequence I the left of a larger number, can be calculated from right to left

Using the line segment Tree implementation: Find the sequence of K positive number corresponding to the position of the subscript, did not find a, is equivalent to the position of the numbers into negative O (log)

#include <cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<cstdlib>#include<queue>#include<vector>#include<map>#include<Set>#include<iostream>#defineLson L, M, RT << 1#defineRson m + 1, R, RT << 1|1using namespacestd;Const intINF =0x3f3f3f3f; typedefLong Longll;Const intN =50005;intNum[n <<2], A[n], ans[n];voidUpintRT) {Num[rt] = Num[rt <<1] + num[rt <<1|1]; }voidBuildintLintRintRT) {    if(L = =R) {Num[rt]=1; return ; }    intm = (L + r) >>1;    Build (Lson);    Build (Rson); Up (RT);}voidUpdateintLintRintRtintp) {    if(L = = P && r = =p) {Num[rt]--; return ; }    intm = (L + r) >>1; if(P <=m) Update (Lson, p); ElseUpdate (Rson, p); Up (RT);}intPos;void Get(intLintRintRtintx) {    if(L = =r) {POS=l; return; }    intm = (L + r) >>1; if(Num[rt <<1] < X)Get(Rson, X-num[rt <<1]); Else Get(Lson, x);}intMain () {int_; scanf"%d", &_);  while(_ --)    {        intN scanf"%d", &N);  for(inti =1; I <= N; ++i) scanf ("%d", &A[i]); Build (1N1); intc =N;  for(inti = n; I >=2; --i) {intD = C-(A[i]-a[i-1]); Get(1N1, D); Update (1N1, POS); //cout << pos << Endl;Ans[i] =POS; C--; }        Get(1N1,1); ans[1] =POS;  for(inti =1; I < n; ++i) printf ("%d", Ans[i]); printf ("%d\n", Ans[n]); }    return 0;}
View Code

Bestcoder round #65 && hdu 5592 ZYB ' s premutation 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.