Bzoj 3173 [Tjoi2013] Longest ascending sub-sequence Treap+lis

Source: Internet
Author: User

Test Instructions:Link Method:Treap+lis parsing:Just saw the question also blindfolded for a while, do not know AH--! later thinking to find the law. Drawing examples and the two sets of data have been made up to find point problems ... The answer increments? And then the reaction comes--when we get into the big insert, the post-insertion situation does not affect the pre-insertion. So we can get the whole sequence out first? After a LIS, for each number, he has an LIS value, of course, this may not be the best solution after he inserted, because the previous value may be greater than after he inserted. So we need to sweep the array again and update the answer. As for the first part to find the sequence, it is very advantageous to realize with Treap. What about the second part? N^2 's practice is obviously not here, so Nlogn is good. Large complexity O (NLOGN) Code:
#include <ctime>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#define N 100010using namespace Std;intSize,n,root;struct node{intL,r,rnd,siz,w,v;}TR[N];void Pushup (intRT) {TR[rt].siz=TR[TR[rt].l].siz+TR[TR[rt].r].siz+1; }void Lturn (int&AMP;RT) {intt=TR[RT].R;TR[rt].r=TR[T].l;TR[T].l=rt;TR[t].siz=TR[Rt].siz;    Pushup (RT); rt=t;} void Rturn (int&AMP;RT) {intt=TR[Rt].l;TR[rt].l=TR[T].R;TR[T].r=rt;TR[t].siz=TR[Rt].siz;    Pushup (RT); rt=t;} void Insert (int&rt,intV) {if(!RT) {rt=++size;TR[rt].l=TR[rt].r=0,TR[rt].siz=1;TR[Rt].v=size,TR[rt].rnd=Rand();return; }TR[rt].siz++;if(v<=TR[TR[rt].l].siz+1) {Insert (TR[Rt].l,v];if(TR[TR[rt].l].rnd<TR[Rt].rnd) Rturn (RT); }Else{Insert (TR[rt].r,v-TR[TR[rt].l].siz-1);if(TR[TR[rt].r].rnd<TR[Rt].rnd) Lturn (RT); }}intTotintSeQ[n]; void Getseq (int rt){if(TR[rt].l!=0) GetseQ (TR[RT].L); SeQ[++tot]=TR[RT].V;if(TR[rt].r!=0) GetseQ (TR[RT].R);}intF[n],d[n],ans[n];int Print[N]; void Getlis () {memset (d,0x3f, sizeof (d)); d[0]=0; d[1]=seQ[1], f[1]=1;intmx=1; for(intI=2; i<=n;i++) {if(SEQ[i]&GT;D[MX]) {f[i]=++mx; D[mx]=min (D[mx],seQ[i]); }Else{intL=0, r=mx,ans=0; while(L&LT;=R) {intMid= (l+r) >>1;if(D[mid]<seQ[i]) ans=mid,l=mid+1;Elser=mid-1; } f[i]=ans+1; d[ans+1]=min (d[ans+1],seQ[i]); }    }}intMain () {scanf ("%d", &n); for(intI=1; i<=n;i++) {int x; scanf"%d",&x);x++; Insert (Root,x); } GetseQ (Root); Getlis (); for(intI=1; i<=n;i++)Print[SEQ[i]]=f[i]; for(intI=1; i<=n;i++)Print[I]=max (Print[I],Print[I1]); for(intI=1; i<=n;i++)printf("%d\ n",Print[i]);}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Bzoj 3173 [Tjoi2013] Longest ascending sub-sequence Treap+lis

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.