CF 269D Maximum Waterfall (line segment tree, DP)

Source: Internet
Author: User

Question: some walls are provided. The water flows from high to low and can only reach one wall at a time. Select a path to maximize the traffic on the path. First of all, http://codeforces.com/problemset/problem/269/D is mainly about understanding the meaning of the question. In this case, 1 can flow to 2, and 1 can flow to 3. I started to understand that 1 cannot flow to 2, because there is a 3 blocking in the middle, but it is actually only for the public part. In this case, 1 cannot flow to 2, because the public part of 1 and 2 has 3 blocks. After understanding this, it is easy to do. After sorting by height, all coordinates are discretization. Insert a line segment to the line segment tree by height. Maintain the label of the highest line segment in the line segment tree, that is, the maximum value of the line segment. After a query of Line Segment 3, it is found that the last coverage of this section is Line Segment 2, but Line Segment 3 is not completely covered by Line Segment 2. You can continue to query the green section. As shown in the left figure, the query result should be line 1, SO 3 can flow to 2 or 1. here we can use dp to make an optimal solution. But pay attention to the above picture, query the green section to get Line Segment 1, but the right side of Line Segment 1 exceeds the right side of the green section. According to our previous query, the right side of the green section has been overwritten by 2, therefore, we need to mark the two ends at this time, indicate that the endpoint cannot exceed [cpp] # include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # define test puts ("OK "); # define inf 1000000000 # define lson (step <1) # define rson (step <1 | 1) using namespace std; const int N = 100005; struct Wall {int h, l, r; Wall () {} Wall (int _ h, int _ l, int _ r): h (_ h), l (_ l ), r (_ r) {} bool operator <(co Nst Wall w) const {return h <w. h ;}} wall [N]; struct Seg_Tree {int left, right; int val, all;} L [N <3]; int n, t; int x [N <1], cnt; // discretization int dp [N] = {0}; void Bulid (int step, int l, int r) {L [step]. left = l; L [step]. right = r; L [step]. val = L [step]. all =-1; if (l = r) return; int m = (l + r)> 1; Bulid (lson, l, m); Bulid (rson, m + 1, r);} void Push_Down (int step) {if (L [step]. all! =-1) {L [lson]. all = L [rson]. all = L [step]. all; L [lson]. val = L [rson]. val = L [step]. all; L [step]. all =-1 ;}} void Push_Up (int step) {L [step]. val = max (L [lson]. val, L [rson]. val);} void Update (int step, int l, int r, int id) {if (L [step]. left = l & L [step]. right = r) {L [step]. val = L [step]. all = id; return;} Push_Down (step); int m = (L [step]. left + L [step]. right)> 1; if (r <= m) Update (lson, l, r, id); else if (l> m) Update (rson, l, R, id); else {Update (lson, l, m, id); Update (rson, m + 1, r, id);} Push_Up (step );} int Query (int step, int l, int r) {if (L [step]. left = l & L [step]. right = r) return L [step]. val; int m = (L [step]. left + L [step]. right)> 1; Push_Down (step); if (r <= m) return Query (lson, l, r); else if (l> m) return Query (rson, l, r); else return max (Query (lson, l, m), Query (rson, m + 1, r);} void slove (int l, int r, int who, int can_l, int can_r ){ If (l> r) return; int id = Query (1, l, r); if (id =-1) {Update (1, l, r, who ); dp [who] = x [r + 1]-x [l]; return;} int pre_l = wall [id]. l, pre_r = wall [id]. r; if (pre_l> = l | can_l) & (pre_r <= r | can_r) {dp [who] = max (dp [who], min (dp [id], x [min (pre_r, r) + 1]-x [max (pre_l, l)]);} if (l <pre_l) slove (l, pre_l-1, who, can_l, 0); if (r> pre_r) slove (pre_r + 1, r, who, 0, can_r);} int main () {scanf ("% d", & n, & t); for (int I = 0; I <n; I ++) {int h, l, R; scanf ("% d", & h, & l, & r); wall [I] = Wall (h, l, r ); x [I * 2] = l; x [I * 2 + 1] = r;} x [n * 2] =-inf; x [n * 2 + 1] = inf; sort (x, x + 2 * n + 2); cnt = unique (x, x + 2 * n + 2) -x; wall [n ++] = Wall (0,-inf, inf); wall [n ++] = Wall (t,-inf, inf ); sort (wall, wall + n); for (int I = 0; I <n; I ++) {wall [I]. l = lower_bound (x, x + cnt, wall [I]. l)-x; wall [I]. r = lower_bound (x, x + cnt, wall [I]. r)-X-1;} Bulid (, cnt-1); for (int I = 0; I <n; I ++) {int l = wall [I]. l, r = wall [I]. r; Slove (l, r, I, 1, 1); Update (1, l, r, I);} printf ("% d \ n", dp [n-1]); return 0 ;}# include <iostream >#include <cstdio >#include <cstring >#include <algorithm> # define test puts ("OK "); # define inf 1000000000 # define lson (step <1) # define rson (step <1 | 1) using namespace std; const int N = 100005; struct Wall {int h, l, r; Wall () {} Wall (int _ h, int _ l, int _ r): h (_ h), l (_ l ), r (_ r) {} bool operator <(const Wall w) const {return h <w. H ;}} wall [N]; struct Seg_Tree {int left, right; int val, all;} L [N <3]; int n, t; int x [N <1], cnt; // discretization int dp [N] = {0}; void Bulid (int step, int l, int r) {L [step]. left = l; L [step]. right = r; L [step]. val = L [step]. all =-1; if (l = r) return; int m = (l + r)> 1; Bulid (lson, l, m); Bulid (rson, m + 1, r);} void Push_Down (int step) {if (L [step]. all! =-1) {L [lson]. all = L [rson]. all = L [step]. all; L [lson]. val = L [rson]. val = L [step]. all; L [step]. all =-1 ;}} void Push_Up (int step) {L [step]. val = max (L [lson]. val, L [rson]. val);} void Update (int step, int l, int r, int id) {if (L [step]. left = l & L [step]. right = r) {L [step]. val = L [step]. all = id; return;} Push_Down (step); int m = (L [step]. left + L [step]. right)> 1; if (r <= m) Update (lson, l, r, id); else if (l> m) Update (rson, l, r, id); else {Update (lson, l, m, id); Update (rson, m + 1, r, id);} Push_Up (step );} int Query (int step, int l, int r) {if (L [step]. left = l & L [step]. right = r) return L [step]. val; int m = (L [step]. left + L [step]. right)> 1; Push_Down (step); if (r <= m) return Query (lson, l, r); else if (l> m) return Query (rson, l, r); else return max (Query (lson, l, m), Query (rson, m + 1, r);} void slove (int l, int r, int who, int can_l, int can_r) {if (l> r) return; int id = Query (1, l, r); if (id =-1) {Update (1, l, r, who); dp [who] = x [r + 1]-x [l]; return;} int pre_l = wall [id]. l, pre_r = wall [id]. r; if (pre_l> = l | can_l) & (pre_r <= r | can_r) {dp [who] = max (dp [who], min (dp [id], x [min (pre_r, r) + 1]-x [max (pre_l, l)]);} if (l <pre_l) slove (l, pre_l-1, who, can_l, 0); if (r> pre_r) slove (pre_r + 1, r, who, 0, can_r);} int main () {scanf ("% d", & n, & t); for (int I = 0; I <n; I ++) {int h, l, r; scanf ("% d", & h, & l, & r); wall [I] = Wall (h, l, r ); x [I * 2] = l; x [I * 2 + 1] = r;} x [n * 2] =-inf; x [n * 2 + 1] = inf; sort (x, x + 2 * n + 2); cnt = unique (x, x + 2 * n + 2) -x; wall [n ++] = Wall (0,-inf, inf); wall [n ++] = Wall (t,-inf, inf ); sort (wall, wall + n); for (int I = 0; I <n; I ++) {wall [I]. l = lower_bound (x, x + cnt, wall [I]. l)-x; wall [I]. r = lower_bound (x, x + cnt, wall [I]. r)-X-1;} Bulid (, cnt-1); for (int I = 0; I <n; I ++) {int l = wall [I]. l, r = wall [I]. r; slove (l, r, I, 1, 1); Update (1, l, r, I);} printf ("% d \ n", dp [n-1]); 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.