Poj 1823 Hotel line segment tree. Pay attention to the lazy flag. If it is not marked, the drops will time out.

Source: Internet
Author: User

[Cpp]
The answer is 491 accepted, which is quite scary. Don't be scared. Actually, I am scared. Hey, it's also a tragedy to do this, however, when I look at the category, it is a medium question, so I am so angry that I am a third.
We need to use ml, mr, len on the online segment to indicate several consecutive vacancies on the left, several consecutive vacancies on the right, and several consecutive vacancies in the middle.
Pay attention to updating rules when merging
[Cpp]
A [I]. len = max (a [I * 2]. mr + a [I * 2 + 1]. ml, max (a [I * 2]. len, a [I * 2 + 1]. len); // update the len value.
If (a [I * 2 + 1]. ml = (a [I * 2 + 1]. r-a [I * 2 + 1]. l + 1) // If the ml of the child on the right is equal to the length of the Line Segment, that is, the child on the right is empty, then a [I]. ml is a [I * 2]. the entire length on the right side of mr +
A [I]. mr = a [I * 2]. mr + a [I * 2 + 1]. ml;
Else
A [I]. mr = a [I * 2 + 1]. mr; // if the right side of the child is empty, then a [I]. long mr is equal to a [I * 2 + 1]. mr
If (a [I * 2]. mr = (a [I * 2]. r-a [I * 2]. l + 1) // same as above
A [I]. ml = a [I * 2 + 1]. ml + a [I * 2]. mr;
Else
A [I]. ml = a [I * 2]. ml ;//

There is also a lazy flag. If it is not marked, it will time out.
[Cpp]
# Include <iostream>
# Include <cstdio>
Using namespace std;
# Define N 16005
Struct node {
Int l, r, ml, mr, mm, len, sign;
} A [N * 4];
Void build (int I, int left, int right ){
A [I]. l = left;
A [I]. r = right;
A [I]. len = a [I]. ml = a [I]. mr = right-left + 1;
A [I]. mm = 0;
A [I]. sign = 0; // indicates whether the edge is used.
If (left = right) return;
Int mid = (a [I]. l + a [I]. r)> 1;
Build (I * 2, left, mid );
Build (I * 2 + 1, mid + 1, right );
}
Void insert (int I, int left, int right, int sign ){
// Cout <a [I]. l <"" <a [I]. r <"****" <left <"" <right <endl;
If (a [I]. l> = left & a [I]. r <= right ){
A [I]. sign = 1;
If (sign = 1 ){
A [I]. len = a [I]. ml = a [I]. mr = 0;
} Else {
A [I]. len = a [I]. ml = a [I]. mr = (a [I]. r-a [I]. l + 1 );
}
Return;
}
If (a [I]. sign = 1 & a [I]. ml = 0 & a [I]. mr = 0 & a [I]. len = 0) {// lazy mark
A [I * 2 + 1]. ml = a [I * 2 + 1]. mr = a [I * 2 + 1]. len = 0;
A [I * 2]. ml = a [I * 2]. mr = a [I * 2]. len = 0;
A [I * 2 + 1]. sign = a [I * 2]. sign = 1;
A [I]. sign = 0;
}
If (a [I]. sign = 1 & a [I]. ml = (a [I]. r-a [I]. l + 1) & a [I]. mr = (a [I]. r-a [I]. l + 1) & a [I]. len = (a [I]. r-a [I]. l + 1) {// lazy mark
A [I * 2 + 1]. ml = a [I * 2 + 1]. mr = a [I * 2 + 1]. len = (a [I * 2 + 1]. r-a [I * 2 + 1]. l + 1 );
A [I * 2]. ml = a [I * 2]. mr = a [I * 2]. len = (a [I * 2]. r-a [I * 2]. l + 1 );
A [I * 2 + 1]. sign = a [I * 2]. sign = 1;
A [I]. sign = 0;
}
Int mid = (a [I]. l + a [I]. r)> 1;
If (right <= mid) insert (I * 2, left, right, sign );
Else if (left> mid) insert (I * 2 + 1, left, right, sign );
Else {
Insert (I * 2, left, mid, sign );
Insert (I * 2 + 1, mid + 1, right, sign );
}
A [I]. len = max (a [I * 2]. mr + a [I * 2 + 1]. ml, max (a [I * 2]. len, a [I * 2 + 1]. len ));
If (a [I * 2 + 1]. ml = (a [I * 2 + 1]. r-a [I * 2 + 1]. l + 1 ))
A [I]. mr = a [I * 2]. mr + a [I * 2 + 1]. ml;
Else
A [I]. mr = a [I * 2 + 1]. mr;
If (a [I * 2]. mr = (a [I * 2]. r-a [I * 2]. l + 1 ))
A [I]. ml = a [I * 2 + 1]. ml + a [I * 2]. mr;
Else
A [I]. ml = a [I * 2]. ml;
// Cout <"l =" <a [I]. l <"r =" <a [I]. r <"" <a [I]. ml <"" <a [I]. len <"" <a [I]. mr <endl;
}
 
Int main (){
Int n, p, m, x, y;
While (~ Scanf ("% d", & n, & p )){
Build (1, 1, n );
While (p --){
Scanf ("% d", & m );
If (m = 1 ){
Scanf ("% d", & x, & y );
// Cout <"x + Y-1 =" <x + Y-1 <endl;
Insert (1, x, x + Y-1, 1 );
} Else if (m = 2 ){
Scanf ("% d", & x, & y );
Insert (1, x, x + Y-1, 0 );
}
Else
Printf ("% d \ n", max (a [1]. ml, max (a [1]. len, a [1]. mr )));
}
 
}
}


Author: youngyangyang04

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.