POJ3667 [Line Segment tree, maximum continuous interval]

Source: Internet
Author: User

The last question of the Line Segment tree is now. The line segment tree can do a lot of work, and it has higher requirements for itself when combined with DP. What you have to do is still far away ~ This question has been debugged for a long time... 1a: ask whether there is a continuous length of a empty room, if there is to live in the leftmost 2a B: [a, a + b-1] Room clearing ideas: the longest line segment tree operation in the record interval: update: interval replacement query: the leftmost breakpoint that meets the condition is queried. The main difficulty lies in void pushup (int rt, int m) {lsum [rt] = lsum [rt <1]; rsum [rt] = rsum [rt <1 | 1]; // process these steps! If (lsum [rt] = (m-m/2) lsum [rt] + = lsum [rt <1 | 1]; if (rsum [rt] = m/2) rsum [rt] + = rsum [rt <1]; msum [rt] = max (rsum [rt <1] + lsum [rt <1 | 1], max (msum [rt <1], msum [rt <1 | 1]);} Understood ~ By the way, I made another mistake on pushdown. pushdown is passed to the child node.

# Include <iostream> # include <cstdio> # include <cstring> using namespace std; # define lson rt <1, l, m # define rson rt <1 | 1, m + 1, r const int maxn = 50005; // classic practice // msum maximum continuous interval lsum, rsum from left, int cover [maxn <8], msum [maxn <8], lsum [maxn <8], rsum [maxn <8], and rsum [maxn <8]; void build (int rt, int l, int r) {cover [rt] =-1; msum [rt] = lsum [rt] = rsum [rt] = r-l + 1; if (l = r) return; int m = (l + r)> 1; build (lson); build (rson);} voi D pushdown (int rt, int m) {if (cover [rt]! =-1) {cover [rt <1] = cover [rt <1 | 1] = cover [rt]; msum [rt <1] = lsum [rt <1] = rsum [rt <1] = cover [rt]? 0: m-(m> 1 ); msum [rt <1 | 1] = lsum [rt <1 | 1] = rsum [rt <1 | 1] = cover [rt]? 0: (m> 1); cover [rt] =-1 ;}} void pushup (int rt, int m) {lsum [rt] = lsum [rt <1]; rsum [rt] = rsum [rt <1 | 1]; // process these steps! If (lsum [rt] = (m-m/2) lsum [rt] + = lsum [rt <1 | 1]; if (rsum [rt] = m/2) rsum [rt] + = rsum [rt <1]; msum [rt] = max (rsum [rt <1] + lsum [rt <1 | 1], max (msum [rt <1], msum [rt <1 | 1]);} void update (int ql, int qr, int ch, int rt, int l, int r) {if (ql <= l & qr> = r) {cover [rt] = ch; if (ch = 0) msum [rt] = lsum [rt] = rsum [rt] = r-l + 1; else if (ch = 1) msum [rt] = lsum [rt] = rsum [rt] = 0; return;} pushdown (rt, r-l + 1 ); int m = (l + r)> 1; if (ql <= m) updat E (ql, qr, ch, lson); if (qr> m) update (ql, qr, ch, rson); pushup (rt, r-l + 1 );} int query (int ch, int rt, int l, int r) {// returns the left coordinate if (l = r) return l; // haha... Pushdown (rt, r-l + 1); int m = (l + r)> 1; if (msum [rt <1]> = ch) return query (ch, lson); else if (rsum [rt <1] + lsum [rt <1 | 1])> = ch) return m-rsum [rt <1] + 1; // return the most classic else return query (ch, rson);} int main () {int n, m, a, B, c; while (scanf ("% d", & n, & m )! = EOF) {build (1, 1, n); for (int I = 1; I <= m; I ++) {scanf ("% d", & ); if (a = 1) {scanf ("% d", & B); if (msum [1] <B) // use the root node to determine whether printf ("0 \ n") is greater than B; else {int pos = query (B, 1, 1, n ); printf ("% d \ n", pos); update (pos, pos + b-1, 1, 1, n) ;}} else {scanf ("% d ", & B, & c); update (B, B + C-1, 1, n) ;}} 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.