Poj 3667 (merge line segment tree intervals)

Source: Internet
Author: User

Http://poj.org/problem? Id = 3667

 

Question: two operations: 1. Select the range from left to.

2. Clear the range from A to a + B.

 

Merge line segment tree intervals + lazy

 

// By caonima
// Hehe
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
# Include <vector>
# Include <cmath>
Using namespace STD;
Const int max = 1e5 + 10;
Int lsum [max <2], rsum [max <2], msum [max <2];
Int Col [max <2];

Void push_up (int o, int m ){
Lsum [O] = lsum [O <1];
Rsum [O] = rsum [O <1 | 1];
If (lsum [O] = (m-(M> 1) lsum [O] + = lsum [O <1 | 1];
If (rsum [O] = (M> 1) rsum [O] + = rsum [O <1];
Msum [O] = max (msum [O <1], msum [O <1 | 1]), rsum [O <1] + lsum [O <1 | 1]);
Return;
}
Void push_down (int o, int m ){
If (COL [O]! =-1 ){
Col [O <1] = Col [O <1 | 1] = Col [O];
Lsum [O <1] = rsum [O <1] = m
Sum [O <1] = Col [O]? 0: (m-(M> 1 ));
Lsum [O <1 | 1] = rsum [O <1 | 1] = msum [O <1 | 1] = Col [O]? 0: (M> 1 ));
Col [O] =-1;
}
}
Void build (int l, int R, int o ){
Col [O] =-1;
If (L = r ){
Lsum [O] = rsum [O] = msum [O] = 1;
Return;
}
Int mid = (L + r)> 1;
Build (L, mid, O <1 );
Build (Mid + 1, R, O <1 | 1 );
Push_up (O, R-L + 1 );
}

Int query (int l, int R, int o, int Len ){
If (L = r ){
Return L;
}
Push_down (O, R-L + 1 );
Int mid = (L + r)> 1;
If (msum [O <1]> = Len) return query (L, mid, O <1, Len );
Else if (rsum [O <1] + lsum [O <1 | 1]> = Len) return mid-rsum [O <1] + 1;
Else return query (Mid + 1, R, O <1 | 1, Len );
Push_up (O, R-L + 1 );

}

Void Update (int l, int R, int o, int ls, int RS, int Val ){
If (LS <= L & RS> = r ){
Msum [O] = lsum [O] = rsum [O] = Val? 0: (R-L + 1 );
Col [O] = val;
Return;
}
Push_down (O, R-L + 1 );
Int mid = (L + r)> 1;
If (LS <= mid) Update (L, mid, O <1, ls, RS, Val );
If (RS> mid) Update (Mid + 1, R, O <1 | 1, ls, RS, Val );
Push_up (O, R-L + 1 );
}

Int main (){
Int n, m, OP, V, ls, RS;
While (scanf ("% d", & N, & M) = 2 ){
Build (1, n, 1 );
For (INT I = 0; I <m; I ++ ){
Scanf ("% d", & OP );
If (OP = 1 ){
Scanf ("% d", & V );
If (msum [1] <v) printf ("0 \ n ");
Else {
Int res = query (1, n, 1, V );
Printf ("% d \ n", Res );
Update (1, n, 1, res, Res + V-1, 1 );
}
}
Else {
Scanf ("% d", & LS, & RS );
Update (1, n, 1, ls, LS + RS-1, 0 );
}
}
}
Return 0;

}

 

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.