Appointment Schedule---hdu4553 (segment tree, Trouble Zone overlay)

Source: Internet
Author: User

title Link: http://acm.hdu.edu.cn/showproblem.php?pid=4553As poj3667 's enhanced version, two line-segment trees are established, one is the DS interval and the other is the NS interval. Then according to test instructions, if it is the DS request, then first find the DS interval, if there is a satisfied interval to update the DS interval, the NS interval does not need to be updated. In the case of NS requests, first look at whether the DS interval has a satisfied interval, otherwise find the NS interval, if any, update both the DS and NS intervals. It can be concluded that, as long as the request is NS, the two line segments are updated at the same time, or only the DS segment tree is updated. Note that the output is to be copied from output, otherwise it will WA, and the If order in the UP function does not change; these two wrote a day, but value, of course, if not because there are seniors blog in, I can't write a day, seniors are seniors;
#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespacestd;#defineINF 0XFFFFFFF#defineN 110050#defineLson r<<1#defineRson r<<1|1structsegmenttree{intL, R; BOOLNS, DS, study;///0 Delegates    intlsum[2], rsum[2], sum[2];///0 is the cock silk, 1 goddess;///Lsum Range Left (starting from L) maximum non-overwritten value///rsum interval to the right (introduction to R) Continuous maximum non-overwritten value///sum entire interval continuous maximum non-overwritten value    intMid () {return(l+r) >>1;} intLen () {returnr-l+1; } voidClearplan (BOOLX///empty;    {        if(x) {lsum[0] = lsum[1] =Len (); rsum[0] = rsum[1] =Len (); sum[0] = sum[1] =Len (); NS= ds =false; Study =true; }    }    voidNS (BOOLX///Goddess;    {        if(x) {lsum[0] = lsum[1] =0; rsum[0] = rsum[1] =0; sum[0] = sum[1] =0; NS=true; DS =false; }    }    voidDS (BOOLX///Dick Silk;    {        if(x) {lsum[0] = rsum[0] = sum[0] =0; DS=true; } }} a[n<<2];voidBuild (intRintLintR) {A[r]. L= L, A[r]. R =R; A[r].clearplan (true);///empty;A[R].NS = A[r].ds = A[r].study =false; if(L = = R)return ; Build (Lson, L, A[r].    Mid ()); Build (Rson, A[r]. Mid ()+1, R);}voidUp (intRintWho ) {    if(A[r]. L! =A[r]. R) {a[r].lsum[who]=a[lson].lsum[who]; A[r].rsum[who]=a[rson].rsum[who]; if(a[lson].lsum[who] = =A[lson].len ()) a[r].lsum[who]+=a[rson].lsum[who]; if(a[rson].rsum[who] = =A[rson].len ()) a[r].rsum[who]+=a[lson].rsum[who]; A[r].sum[who]= Max (a[lson].rsum[who] +a[rson].lsum[who], Max (a[lson].sum[who], a[rson].sum[who])); }}voidDown (intR///This order cannot be reversed{    if(a[r].study) {A[lson].clearplan (true); A[rson].clearplan (true); A[r].study=false; }    if(A[R].NS) {A[lson]. NS (true); A[rson]. NS (true); A[r].ns= A[r].ds =false; }    if(A[r].ds) {A[lson]. DS (true); A[rson]. DS (true); A[r].ds=false; }}voidUpdate (intRintLintRintflag) {    if(A[r]. L = = L && a[r]. R = =R) {if(Flag = =3) A[r]. DS (true); if(Flag = =2) A[r]. NS (true); if(Flag = =1) A[r].clearplan (true); return;    } down (R); if(R <=A[r].    Mid ()) Update (Lson, L, R, flag); Else if(L >A[r].    Mid ()) Update (Rson, L, R, flag); Else{Update (Lson, L, A[r].        Mid (), flag); Update (Rson, A[r]. Mid ()+1, R, flag); } up (R,0); Up (R,1);}intQuery (intRintNumintWho )    {Down (R); if(a[r].sum[who] < num)return 0; if(a[r].lsum[who] >= num)returnA[r].    L if(a[lson].sum[who] >= num)returnQuery (Lson, Num, who); if(A[lson].rsum[who]+a[rson].lsum[who] >=num)returnA[lson]. R-a[lson].rsum[who] +1; returnQuery (Rson, Num, who);}intMain () {intN, M, T, t =1, R, L, time; scanf ("%d", &T);  while(t--) {scanf ("%d%d", &n, &m); Build (1,1, N); printf ("Case %d:\n", t++);  while(m--)        {            Chars[1100]; scanf ("%s", s); if(s[0] =='D') {scanf ("%d", &Time ); L= Query (1, Time,0);///cock Silk is 0;                if( ! L) printf ("Fly with yourself\n"); Else{Update (1, L, l+time-1,3); printf ("%d,let ' s fly\n", L); }            }            Else if(s[0] =='N') {scanf ("%d", &Time ); L= Query (1, Time,0);///Find in the time of the cock Silk, when there is no time in the cock silk time, in the goddess time;                if( ! L) L = Query (1, Time,1);///the Goddess is 1;                if( ! L) printf ("Wait for me\n"); Else{Update (1, L, l+time-1,2); printf ("%d,don ' t put my gezi\n", L); }            }            Else{scanf ("%d%d", &l, &R); Update (1, L, R,1); printf ("I am The hope of Chinese chengxuyuan!! \ n"); }        }    }    return 0;}
View Code

Appointment Schedule---hdu4553 (segment tree, Trouble Zone overlay)

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.