HDU 4521-xiaoming sequence (line tree good question)

Source: Internet
Author: User

Test instructions

Sequence of n numbers, finding the longest ascending subsequence of each digit interval greater than D

Analysis:

The most basic DP but the large data volume O (n^2) must timeout before dp[i] for the longest ascending subsequence is by the first dp[1]---dp[i-d-1] meet the maximum value of the condition, we can use the segment tree to maintain DP[1]---dp[i-d-1] maximum

#include <map>#include<Set>#include<list>#include<cmath>#include<queue>#include<stack>#include<cstdio>#include<vector>#include<string>#include<cctype>#include<complex>#include<cassert>#include<utility>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespaceStd;typedef pair<int,int>Pii;typedefLong Longll;#defineLson l,m,rt<<1#definePi ACOs (-1.0)#defineRson m+1,r,rt<<1|1#defineAll 1,n,1#defineRead Freopen ("In.txt", "R", stdin)#defineN 100010Constll infll =0x3f3f3f3f3f3f3f3fll;Const intinf=0x7ffffff;Const intMoD =1000000007;intmaxv[n*4],a[n],dp[n];voidPushup (intRT) {Maxv[rt]=max (maxv[rt<<1],maxv[rt<<1|1]);}voidBuildintLintRintRT) {Maxv[rt]=0; if(l==R) {        return; }    intM= (l+r) >>1;    Build (Lson); Build (Rson);}voidUpdateintPosintNumintLintRintRT) {    if(l==R) {Maxv[rt]=Max (maxv[rt],num); return; }    intM= (l+r) >>1; if(pos<=m) update (Pos,num,lson); ElseUpdate (Pos,num,rson); Pushup (RT);}intQueryintLintRintLintRintRT) {    if(l<=l&&r>=R) {        returnMaxv[rt]; }    intM= (l+r) >>1; intans=0; if(l<=m) ans=Max (Ans,query (L,r,lson)); if(r>m) ans=Max (Ans,query (L,r,rson)); returnans;}intMain () {intn,d;  while(~SCANF ("%d%d",&n,&d)) {            intMaxl=0;  for(intI=1; i<=n;++i) {scanf ("%d",&A[i]); Maxl=Max (maxl,a[i]); } memset (DP,0,sizeof(DP)); Build (0, Maxl,1);//make a range of values        intmaxans=0;  for(intI=1; i<=n;++i) {            if(i-d-1>0) {Update (A[i-D1],dp[i-d-1],0, Maxl,1);//Update Point            }            if(a[i]==0) {Dp[i]=1; }            ElseDp[i]=query (0, a[i]-1,0, Maxl,1)+1;//value <a[i] and dp[1]---dp[i-d-1] max .maxans=Max (maxans,dp[i]); } printf ("%d\n", Maxans); }return 0;}

HDU 4521-xiaoming sequence (line tree good question)

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.