HDU 1540 (interval merger)

Source: Internet
Author: User

#include <stdio.h>#include<stdlib.h>#include<string.h>#defineMAXN 50010#defineLson root*2#defineRson root*2+1#defineMax (A, B) (A&GT;B?A:B)intAC[MAXN];structndoe{//The lsum represents the continuity of the left interval, the rsum represents the continuous in the right interval, and sum indicates the continuity of the intermediate interval;    intL,r; intlsum,rsum,sum; intMid () {return(r+l)/2; }    intLen () {return(r-l+1); }} A[MAXN*4];voidBuildtree (intRootintLintR) {A[root]. L=l,a[root]. R=R; A[root].lsum= A[root].rsum = A[root].sum =A[root].len (); if(L==R)return ; Buildtree (Lson,l,a[root].    Mid ()); Buildtree (Rson,a[root]. Mid ()+1, R);}voidPushup (introot) {A[root].lsum= a[lson].lsum, a[root].rsum = a[rson].rsum;//The maximum continuous of the left interval is the maximal continuity of the child nodes, and the right interval is the same;    if(A[lson].lsum = = A[lson].len ())//if the maximum continuous of a child node equals its length, then its maximum continuous can be added to the left continuous of the right interval;A[root].lsum = A[lson].lsum +a[rson].lsum; if(A[rson].rsum = =A[rson].len ()) A[root].rsum= A[rson].rsum +a[lson].rsum; A[root].sum= Max (A[root].lsum, Max (A[root].rsum, a[lson].rsum+a[rson].lsum));//The maximum value of the middle interval is three number (left continuous, right continuous, left continuous of right continuous + right interval of child node left interval)}voidUpdateintRootintKinte) {    if(A[root]. L = =A[root]. R) {a[root].lsum= A[root].rsum = A[root].sum =e; return ; }    if(k <=A[root].    Mid ()) Update (Lson, K, E); ElseUpdate (Rson, K, E); Pushup (root);}intQuery (intRootintk) {    if(A[root].sum = =0 )        return 0; if(K < A[root]. L +a[root].lsum)//determine whether it is in the left interval, K < left endpoint + left continuous;        returna[root].lsum; if(k > A[root]. R-a[root].rsum)//determine if it is in the right interval; k > Right end point-right continuous;        returna[root].rsum; if(k > (A[lson]. R-a[lson].rsum) && K < (A[rson]. L + a[rson].lsum))//Judging whether in the middle; Right endpoint-right continuous < K < Left end + left continuous;        returnA[rson].lsum +a[lson].rsum; if(k<=A[root]. Mid ())returnQuery (lson,k); Else        returnQuery (rson,k);}intMain () {intn,m,k,w; Charstr[Ten];  while(~SCANF ("%d%d",&n,&m)) {k=0; Buildtree (1,1, N);  while(m--) {scanf ("%s", str); if(str[0]=='D') {scanf ("%d",&W); AC[K]=W; Update (1W0); K++; }            if(str[0]=='R') {k--; W=Ac[k]; Update (1W1); }            if(str[0] =='Q') {scanf ("%d",&W); printf ("%d\n", Query (1, W)); }        }    }    return 0;}

HDU 1540 (interval merger)

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.