POJ 3667 Hotel Segment Tree

Source: Internet
Author: User

Topic links

This topic needs to maintain three variables, one is the maximum continuous length of the interval, one is the longest prefix, and one is the longest suffix. Need to pay attention to is the query, first check whether the left interval is satisfied, and then should check the left and right interval junction whether meet, here to see pre_max[rt<<1|1]+suf_max[rt<<1|1] is greater than equal to the length of the given, The last is the right interval.

#include <iostream>#include<vector>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<map>#include<Set>#include<string>#include<queue>using namespacestd;#definePB (x) push_back (x)#definell Long Long#defineMK (x, y) make_pair (x, y)#defineLson L, M, rt<<1#defineMem (a) memset (a, 0, sizeof (a))#defineRson m+1, R, rt<<1|1#defineMem1 (a) memset (a,-1, sizeof (a))#defineMEM2 (a) memset (a, 0x3f, sizeof (a))#defineRep (I, A, n) for (int i = A; i<n; i++)#defineull unsigned long Longtypedef pair<int,int>PLL;Const DoublePI = ACOs (-1.0);Const DoubleEPS = 1e-8;Const intMoD = 1e9+7;Const intINF =1061109567;Const intdir[][2] = { {-1,0}, {1,0}, {0, -1}, {0,1} };Const intMAXN =50005;intmaxx[maxn<<2], pre_max[maxn<<2], suf_max[maxn<<2], cover[maxn<<2];voidPushup (intRtintm) {Maxx[rt]= Max (maxx[rt<<1], maxx[rt<<1|1]); PRE_MAX[RT]= pre_max[rt<<1]; SUF_MAX[RT]= suf_max[rt<<1|1]; if(maxx[rt<<1] = = (M (m>>1)) ) Pre_max[rt]= pre_max[rt<<1] + pre_max[rt<<1|1]; if(maxx[rt<<1|1] = = (m>>1)) Suf_max[rt]= suf_max[rt<<1|1]+suf_max[rt<<1]; MAXX[RT]= Max (Maxx[rt], suf_max[rt<<1]+pre_max[rt<<1|1]);}voidBuildintLintRintRT) {Maxx[rt]= Pre_max[rt] = Suf_max[rt] = r-l+1; COVER[RT]= -1; if(L = =R)return ; intm = l+r>>1;    Build (Lson); Build (Rson);}voidPushdown (intRtintm) {if(~Cover[rt]) {Cover[rt<<1] = cover[rt<<1|1] =Cover[rt]; Maxx[rt<<1] = pre_max[rt<<1] = suf_max[rt<<1] = cover[rt]* (M-(m>>1)); Pre_max[rt<<1|1] = suf_max[rt<<1|1] = maxx[rt<<1|1] = cover[rt]* (m>>1); COVER[RT]= -1; }}voidUpdateintLintRintLintRintRtintval) {    if(l<=l&&r>=R) {Cover[rt]=Val; MAXX[RT]= Suf_max[rt] = Pre_max[rt] = val* (r-l+1); return ; } pushdown (RT, R-l+1); intm = l+r>>1; if(l<=m) Update (L, R, Lson, Val); if(r>m) Update (L, R, Rson, Val); Pushup (RT, R-l+1);}intQueryintValintLintRintRT) {    if(L = =R)returnl; Pushdown (RT, R-l+1); intm = l+r>>1; if(maxx[rt<<1]>=val)returnquery (Val, Lson); if(pre_max[rt<<1|1]+suf_max[rt<<1]>=val)returnm-suf_max[rt<<1]+1; returnquery (Val, Rson);}intMain () {intN, m, sign, x, y;  while(cin>>n>>m) {Build (1N1);  while(m--) {scanf ("%d", &Sign ); if(Sign = =1) {scanf ("%d", &x); if(maxx[1]<x) {cout<<0<<Endl; } Else {                    intpos = query (x,1N1); cout<<pos<<Endl; Update (POS, POS+x-1,1N1,0); }            } Else{scanf ("%d%d", &x, &y); Update (x, y+x-1,1N1,1); }        }    }}

POJ 3667 Hotel Segment Tree

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.