Miyu original, post Please note: Reprinted from __________ White House
Question address:
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3016
Description:
Man down
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 618 accepted submission (s): 197
Problem descriptionthe game "Man down 100 floors" is an famous and interesting game. You can enjoy the game from
Http://hi.baidu.com/abcdxyzk/blog/item/16398781b4f2a5d1bd3e1eed.html
We take a simplified version of this game. we have only two kinds of planks. one kind of the planks contains food and the other one contains nails. and if the man falls on the plank which contains food his energy will increase but if he falls on the plank which contains nails his energy will decrease. the man can only fall down vertically. we assume that the energy he can increase is unlimited and no borders exist on the left and the right.
First the man has total energy 100 and stands on the topmost plank of all. then he can choose to go left or right to fall down. if he falls down from the position (XI, Yi), he will fall onto the nearest plank which satisfies (XL <= xi <= xr) (XL is the leftmost position of the plank and XR is the rightmost ). if no planks satisfies that, the man will fall onto the floor and he finishes his mission. but if the man's energy is below or equal to 0, he will die and the game is over.
Now give you the height and position of all planks. and ask you whether the man can falls onto the floor successfully. if he can, try to calculate the maximum energy he can own when he is on the floor. (assuming that the floor is infinite and its height is 0, and all the planks are located at different height ).
Inputthere are multiple test cases.
For each test case, the first line contains one integer N (2 <=n <= 100,000) representing the number of planks.
Then following n lines representing n planks, each line contain 4 integers (H, XL, xr, value) (H> 0, 0 <XL <XR <100,000, -1000 <= value <= 1000), h Represents the plank's height, XL is the leftmost position of the plank and XR is the rightmost position. value represents the energy the man will increase by (if value> 0) or decrease by (if value <0) when he falls onto this plank.
Outputif the man can falls onto the floor successfully just output the maximum energy he can own when he is on the floor. but if the man can not fall down onto the floor anyway, just output "-1" (not including the quote)
Sample Input
410 5 10 105 3 6-1004 7 11 202 2 1000 10
Sample output
140
/*
Description:
There are different flat plates at different heights. If you jump to the flat panel, the blood volume changes,
When a person starts from the top Board, he or she can turn left or right,
Jump vertically to the bottom of the board, find the maximum blood volume that falls to the ground, or-1.
Line Segment tree + dp
You need to query the line segment tree to find the board to which the two endpoints of each Board fall behind;
Then we can start with the highest DP.
DP [I] = max (DP [I], DP [I ^]. v) // DP [I ^] represents the line segment that can walk to I
/*
/*
Mail to: miyubai@gamil.com
Link: http://www.cnblogs.com/MiYu | http://www.cppblog.com/MiYu
Author by: miyu
Test: 1
Complier: G ++ mingw32-3.4.2
Program: hdu_3016
Doc name: Man down
*/
// # Pragma warning (Disable: 4789)
# Include <iostream>
# Include <fstream>
# Include <sstream>
# Include <algorithm>
# Include <string>
# Include <set>
# Include <map>
# Include <utility>
# Include <queue>
# Include <stack>
# Include <list>
# Include <vector>
# Include <cstdio>
# Include <cstdlib>
# Include <cstring>
# Include <cmath>
# Include <ctime>
Using namespace STD;
Struct seg_tree {
Int ID, left, right;
Int mid () {return (left + right)> 1 ;}
} Seg [333333];
Inline void creat (int x, int y, int RT = 1 ){
SEG [RT]. Left = X;
SEG [RT]. Right = y;
// 0 indicates other natural numbers on the ground, indicating the number of boards on each layer.-1 indicates that multiple lines are covered.
SEG [RT]. ID = 0;
If (x = y) return;
Int mid = seg [RT]. mid ();
Creat (x, mid, RT <1 );
Creat (Mid + 1, Y, RT <1 | 1 );
}
Inline void modify (int x, int y, int ID, int RT = 1 ){
// Locate the line segment and directly modify the ID to overwrite it
If (SEG [RT]. Left = x & seg [RT]. Right = y ){
SEG [RT]. ID = ID;
Return;
}
Int LL = RT <1, RR = RT <1 | 1, mid = seg [RT]. mid ();
// If no return is returned, the line segment must be overwritten and marked as-1 after being marked as-1.
If (SEG [RT]. ID! =-1 ){
SEG [ll]. ID = seg [RR]. ID = seg [RT]. ID;
SEG [RT]. ID =-1;
}
If (Y <= mid) Modify (X, Y, ID, LL); // modify segments
Else if (x> mid) Modify (X, Y, ID, RR );
Else {
Modify (x, mid, ID, LL );
Modify (Mid + 1, Y, ID, RR );
}
}
Inline int query (INT POs, int RT = 1) {// query the ID of the line segment where the POS is located
If (SEG [RT]. ID! =-1) return seg [RT]. ID; // returns the ID directly if the line segment is overwritten.
Int LL = RT <1, RR = RT <1 | 1, mid = seg [RT]. mid ();
If (Pos <= mid) return query (Pos, LL); // segmented Query
Else return query (Pos, RR );
}
Inline bool scan_d (Int & num) // integer input
{
Char in; bool isn = false;
In = getchar ();
If (in = EOF) return false;
While (in! = '-' & (In <'0' | in> '9') in = getchar ();
If (in = '-') {isn = true; num = 0 ;}
Else num = In-'0 ';
While (in = getchar (), in> = '0' & in <= '9 '){
Num * = 10, num + = In-'0 ';
}
If (ISN) num =-num;
Return true;
}
Struct plank {
Int X, Y, H, V, left, right;
// Sort by height
Friend bool operator <(const plank & A, const plank & B ){
Return A. H <B. h;
}
} PK [1, 100010];
Int DP [100010];
Int main ()
{
Int n, m;
Creat (1, 100000 );
While (scan_d (N )){
M =-1;
For (INT I = 1; I <= N; ++ I ){
Scan_d (PK [I]. H); scan_d (PK [I]. X); scan_d (PK [I]. Y); scan_d (PK [I]. V );
If (PK [I]. Y> m) M = PK [I]. Y; // the maximum value of the record interval.
}
Modify (1, m, 0 );
Sort (PK + 1, PK + n + 1); // sort by high
Memset (DP, 0, sizeof (DP ));
DP [N] = 100 + PK [N]. V;
// Update the line segment from bottom to top, and record the line segment ID that can be reached by the left and right endpoints of each line segment
For (INT I = 1; I <= N; ++ I ){
Int x = PK [I]. Left = query (PK [I]. X );
Int y = PK [I]. Right = query (PK [I]. y );
Modify (PK [I]. X, PK [I]. Y, I );
}
Int res =-1;
// Top-down DP [I] = max (DP [I], DP [I ^]. V)
// DP [I ^] indicates the line segment that can go to I
For (INT I = N; I> = 1; -- I ){
If (DP [PK [I]. Left] <DP [I] + PK [PK [I]. Left]. V)
DP [PK [I]. Left] = DP [I] + PK [PK [I]. Left]. V;
If (DP [PK [I]. Right] <DP [I] + PK [PK [I]. Right]. V)
DP [PK [I]. Right] = DP [I] + PK [PK [I]. Right]. V;
}
Printf ("% d \ n", DP [0]> 0? DP [0]:-1 );
}
Return 0;
}