Zoj 3650 toy blocks (line segment tree + dp)

Source: Internet
Author: User

Reprinted please indicate the source, thank you http://blog.csdn.net/acm_cxlove/article/details/7854526
By --- cxlove

Question: In a number axis, there are some dominoes in some locations. If you want to push at least a few items that haven't fallen, you can push them all down.

Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 4840

This topic mainly refers to the practice of cwj, which is too weak.

I feel that, even if I have an idea, I cannot write this question, and it is easy to mess up.

Http://edward-mj.com /? P = 615

First of all, there may be multiple locations, so it is obvious that the maximum value is retained, which is worth attention.

For each Domino, the leftmost position and rightmost position that can be pushed down are pre-processed.

This step is divided into two steps. First, you can obtain the leftmost position that can be pushed down by the current block.

The second step is that when a block falls down, a chain reaction is triggered. The leftmost position that can be indirectly pushed down is obtained through the maximum value of the segment tree.

Then there is the DP process, which mainly involves two transfers: push to the left and push to the right. For details, see the code.

# Include <iostream> # include <cstdio> # include <map> # include <cstring> # include <cmath> # include <vector> # include <algorithm> # include <set> # include <string> # include <queue> # define INF 1 <30 # define M 6000005 # define n 110005 # define maxn 300005 # define EPS 1e-8 # define zero () FABS (a) <EPS # define min (A, B) (a) <(B )? (A) :( B) # define max (A, B) (a)> (B )? (A) :( B) # define Pb (a) push_back (a) # define MEM (a, B) memset (a, B, sizeof ()) # define ll long # define lson step <1 # define rson step <1 | 1 using namespace STD; Class seg_tree {public: struct node {int left, right, MX, mn, cover;} l [N * 4]; // update void push_up (INT step) {If (L [STEP]. left = L [STEP]. right) return; L [STEP]. MX = max (L [lson]. MX, L [rson]. MX); L [STEP]. mn = min (L [lson]. mn, L [rson]. mn);} // downward update latency tag, minimum value updated previously // The downward update operation is mainly used for the last Interval Update. Therefore, the maximum value does not need to be void push_down (INT step) {If (L [STEP]. Cover! = Inf) {L [STEP]. Mn = min (L [STEP]. Cover, L [STEP]. mn); If (L [STEP]. Left! = L [STEP]. right) {L [lson]. cover = min (L [STEP]. cover, L [lson]. cover); L [rson]. cover = min (L [STEP]. cover, L [rson]. cover);} l [STEP]. cover = inf ;}} void bulid (INT step, int L, int R, int Val) {L [STEP]. left = L; L [STEP]. right = r; L [STEP]. cover = inf; If (L = r) {L [STEP]. MX = L [STEP]. mn = val; return;} int M = (L + r)/2; bulid (lson, L, M, Val); bulid (rson, m + 1, R, val); push_up (STEP);} // update void Update (INT step, int POs, int Val) {If (L [STEP]. left = POS & L [STEP]. right = POS) {L [STEP]. MX = L [STEP]. mn = val; return;} int M = (L [STEP]. left + L [STEP]. right)/2; If (Pos <= m) Update (lson, POs, Val); else if (Pos> m) Update (rson, POs, Val ); push_up (STEP);} // segment update, which uses the void Update (INT step, int L, int R, int Val) {If (L [STEP]. left = L & L [STEP]. right = r) {L [STEP]. cover = min (L [STEP]. cover, Val); return;} int M = (L [STEP]. left + L [STEP]. right)/2; If (r <= m) Update (lson, L, R, Val); else if (L> m) Update (rson, L, R, Val); else {Update (lson, L, M, val); Update (rson, m + 1, R, Val);} push_up (STEP);} int query_max (INT step, int L, int R) {push_down (STEP); If (L [STEP]. left = L & L [STEP]. right = r) {return l [STEP]. MX;} int M = (L [STEP]. left + L [STEP]. right)/2; If (r <= m) return query_max (lson, L, R); else if (L> m) return query_max (rson, L, R ); else return max (query_max (lson, l, m), Qu Ery_max (rson, m + 1, R);} int query_min (INT step, int L, int R) {push_down (STEP); If (L [STEP]. left = L & L [STEP]. right = r) {return l [STEP]. mn;} int M = (L [STEP]. left + L [STEP]. right)/2; If (r <= m) return query_min (lson, L, R); else if (L> m) return query_min (rson, L, R ); else return min (query_min (lson, l, m), query_min (rson, m + 1, R) ;}} seg; Map <int, int> m; int L [N], R [N], X [N], H [N], CNT; int DP [N], n; int dp () {// note that the initialization here is INF And then update the minimum seg every time. bulid (, cnt-1, INF); For (INT I = 0; I <CNT; I ++) {// push this block, you can push down the left, update to 1 // update the IF (L [I] = 0) seg pushed to the left. update (1, I, I, 1); else seg. update (1, I, I, SEG. query_min (1, L [I]-1, I-1) + 1); // DP [I-1] is 0-(I-1) are disposed, then update the nth push to the right int pre = I? (DP [I-1] + 1): 1; seg. update (1, I, R [I], pre); DP [I] = seg. query_min (1, I, I);} return DP [cnt-1];} int main () {While (scanf ("% d", & N )! = EOF) {M. clear (); For (INT I = 0; I <n; I ++) {int X, H; scanf ("% d", & X, & H); If (M. find (x) = m. end () m [x] = H; else if (h> M [x]) m [x] = H;} Map <int, int>: iterator it; CNT = 0; // map discretization. because there may be multiple blocks at each position, the highest for (IT = m. begin (); it! = M. end (); It ++) {x [CNT] = it-> first; H [CNT ++] = it-> second;} l [0] = 0; for (INT I = 1; I <CNT; I ++) {int low = 0, high = I, mid, ans; // binary search, the leftmost while (low <= high) {mid = (low + high)> 1; if (X [I]-X [Mid] <= H [I]) {ans = mid; high = mid-1;} else low = Mid + 1 ;} L [I] = ans;} seg. bulid (, cnt-1, 0); For (INT I = 0; I <CNT; I ++) {seg. update (1, I, L [I]); // find the leftmost part of the range that can be pushed down directly, that is, the leftmost L [I] = seg that can be pushed down indirectly. query_min (1, L [I], I); seg. update (1, I, L [I]);} // the rightmost handle that can be pushed down, same as R [cnt-1] = cnt-1; For (INT I = cnt-2; i> = 0; I --) {int low = I, high = cnt-1, mid, ans; while (low <= high) {mid = (low + high)> 1; if (X [Mid]-X [I] <= H [I]) {ans = mid; Low = Mid + 1;} else high = mid-1 ;} R [I] = ans;} seg. bulid (, cnt-1, 0); For (INT I = cnt-1; I> = 0; I --) {seg. update (1, I, R [I]); R [I] = seg. query_max (1, I, R [I]); seg. update (1, I, R [I]);} printf ("% d \ n", dp ();} 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.