HDU 4521 Xiao Ming series problems-Xiao Ming sequence (line segment tree maintenance DP)

Source: Internet
Author: User

HDU 4521 Xiao Ming series problems-Xiao Ming sequence (line segment tree maintenance DP)

Address: HDU 4521

The basic idea is DP. When finding the maximum value of the preceding number, you can use the line segment tree to save time.

Because the interval must be greater than d. Therefore, you can use a queue to delay the update to ensure that all requests are sent before d.

The Code is as follows:

#include 
 
  #include 
  
   #include 
   
    #include 
    
     #include 
     
      #include 
      
       #include 
       
        #include 
        
         #include 
         #include 
          
           #include using namespace std;#define lson l, mid, rt<<1#define rson mid+1, r, rt<<1|1#define LL __int64const int INF=0x3f3f3f3f;struct node{ int maxv, num;};int maxv[400000], a[1100000];int q_maxv;void PushUp(int rt){ maxv[rt]=max(maxv[rt<<1],maxv[rt<<1|1]);}void Update(int p, int x, int l, int r, int rt){ if(l==r) { maxv[rt]=p; return ; } int mid=l+r>>1; if(x<=mid) Update(p,x,lson); else Update(p,x,rson); PushUp(rt);}void Query(int ll, int rr, int l, int r, int rt){ if(ll<=l&&rr>=r) { q_maxv=max(q_maxv,maxv[rt]); return ; } int mid=l+r>>1; if(ll<=mid) Query(ll, rr, lson); if(rr>mid) Query(ll,rr,rson);}int main(){ int n, d, i, j, top; node tmp, now; while(scanf("%d%d",&n,&d)!=EOF) { queue
           
            q; for(i=0;i
            
             =d) { tmp=q.front(); q.pop(); Update(tmp.maxv+1,tmp.num,0,100000,1); } } while(!q.empty()) { tmp=q.front(); q.pop(); Update(tmp.maxv+1,tmp.num,0,100000,1); } printf("%d\n",maxv[1]); } return 0;}
            
           
          
        
       
      
     
    
   
  
 


Related Article

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.