POJ 1823 Hotel line segment tree + lazy label

Source: Internet
Author: User

Some rooms have three operations: one is continuous room occupancy, and the other is continuous room vacancy, the third is to ask how long the longest consecutive room is.
Idea: it is obviously the question of the Line Segment tree. lazy thought is used in the middle. A difficult question, which requires careful consideration. The key to this question is that after lazy is used to update down, the information of the parent node needs to be changed based on the information of the child node. That is to say, you also need to update from bottom up.
Code:
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <string. h>
# Include <algorithm>
Using namespace std;
 
Const int n= 16050;
Struct tree
{
Int lp, rp, llen, rlen, mlen, flag;
Int getmid ()
{
Return (lp + rp)/2;
}
} Tt [N * 4];
Void built_tree (int lp, int rp, int pos)
{
Tt [pos]. lp = lp;
Tt [pos]. rp = rp;
Tt [pos]. flag = 0;
If (lp = rp)
{
Tt [pos]. llen = tt [pos]. rlen = tt [pos]. mlen = 1;
Return;
}
Int mmid = tt [pos]. getmid ();
Built_tree (lp, mmid, pos * 2 );
Built_tree (mmid + 1, rp, pos * 2 + 1 );
Tt [pos]. llen = tt [pos]. rlen = tt [pos]. mlen = tt [pos * 2]. mlen + tt [pos * 2 + 1]. mlen;
}
Int max (int a, int B)
{
Return a> B? A: B;
}
Void update (int pos)
{
If (! Tt [pos * 2]. flag)
Tt [pos]. llen = tt [pos * 2]. mlen + tt [pos * 2 + 1]. llen;
Else
Tt [pos]. llen = tt [pos * 2]. llen;
If (! Tt [pos * 2 + 1]. flag)
Tt [pos]. rlen = tt [pos * 2]. rlen + tt [pos * 2 + 1]. mlen;
Else
Tt [pos]. rlen = tt [pos * 2 + 1]. rlen;
Int max1 = max (tt [pos]. llen, tt [pos]. rlen );
Int max2 = tt [pos * 2]. rlen + tt [pos * 2 + 1]. llen;
Int max3 = max (tt [pos * 2]. mlen, tt [pos * 2 + 1]. mlen );
Tt [pos]. mlen = max (max1, max (max2, max3 ));
If (tt [pos * 2]. flag = tt [pos * 2 + 1]. flag)
Tt [pos]. flag = tt [pos * 2]. flag;
}
Void insert (int lp, int rp, int pos)
{
If (tt [pos]. lp = lp & tt [pos]. rp = rp)
{
Tt [pos]. flag = 2;
Tt [pos]. llen = tt [pos]. rlen = tt [pos]. mlen = 0;
Return;
}
If (! Tt [pos]. flag)
{
Tt [pos]. flag = 1;
Tt [pos * 2]. flag = tt [pos * 2 + 1]. flag = 0;
Tt [pos * 2]. llen = tt [pos * 2]. rlen = tt [pos * 2]. mlen = tt [pos * 2]. rp-tt [pos * 2]. lp + 1;
Tt [pos * 2 + 1]. llen = tt [pos * 2 + 1]. rlen = tt [pos * 2 + 1]. mlen = tt [pos * 2 + 1]. rp-tt [pos * 2 + 1]. lp + 1;
}
Int mmid = tt [pos]. getmid ();
If (rp <= mmid)
{
Insert (lp, rp, pos * 2 );
}
Else if (lp> mmid)
{
Insert (lp, rp, pos * 2 + 1 );
}
Else
{
Insert (lp, mmid, pos * 2 );
Insert (mmid + 1, rp, pos * 2 + 1 );
}
Update (pos );
}
Void rem (int lp, int rp, int pos)
{
If (tt [pos]. lp = lp & tt [pos]. rp = rp)
{
Tt [pos]. flag = 0;
Tt [pos]. llen = tt [pos]. rlen = tt [pos]. mlen = tt [pos]. rp-tt [pos]. lp + 1;
Return;
}
If (tt [pos]. flag = 2)
{
Tt [pos]. flag = 1;
Tt [pos * 2]. flag = tt [pos * 2 + 1]. flag = 2;
Tt [pos * 2]. llen = tt [pos * 2]. rlen = tt [pos * 2]. mlen = 0;
Tt [pos * 2 + 1]. llen = tt [pos * 2 + 1]. rlen = tt [pos * 2 + 1]. mlen = 0;
}
Int mmid = tt [pos]. getmid ();
If (rp <= mmid)
Rem (lp, rp, pos * 2 );
Else if (lp> mmid)
Rem (lp, rp, pos * 2 + 1 );
Else
{
Rem (lp, mmid, pos * 2 );
Rem (mmid + 1, rp, pos * 2 + 1 );
}
Update (pos );
}
Int main ()
{
// Freopen ("1.txt"," r ", stdin );
Int n, m;
While (scanf ("% d", & n, & m )! = EOF)
{
Int id, x, y;
Built_tree (1, n, 1 );
While (m --)
{
Scanf ("% d", & id );
If (id = 3)
Printf ("% d \ n", tt [1]. mlen );
Else if (id = 1)
{
Scanf ("% d", & x, & y );
Insert (x, x + Y-1, 1 );
}
Else
{
Scanf ("% d", & x, & y );
Rem (x, x + Y-1, 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.