Man Down problem solving report (hdu3016 dynamic planning line segment tree sorting)

2018-05-30

http://acm.hdu.edu.cn/showproblem.php?pid=3016

This article is migrated from the old blog. The original link is https://lookas2001.com/man-down-%e8%a7%a3%e9%a2%98%e6%8a%a5%e5%91%8a/

Algorithm design

Without saying a word after getting the question, we first arrange these boards in order of their height from small to large, and then we number them incrementally from bottom to top from 1 (we think the lowest floor number is 0). Through such a step, it is obvious that the higher the board, the larger the number.

According to the inscription, if a person is on a board, he can only fall vertically from the left or right end of the board. We define d [i] as the maximum HP when the person reaches the ith board. If we know the value of d [i], we can deduce the corresponding value of the board to which the left or right end falls. (d [j]=d [i]+change of health value when jumping to board j). In this way, we can solve the problem recursively from top to bottom.

But how do we know where this person will fall on the left or right? We can build a line segment tree on the x-axis according to the point, and then "dye" the x-axis from bottom to top according to the board (that is, mark your own serial number on the place covered by the board). In this way, it is very convenient to find out which board a board will jump to when jumping from the left and right ends of the board.

Sample code

 # include  <cstdio>
 # include  <cstring>
 # include  <algorithm>

 const  int N = one hundred and ten thousand , L = one hundred and ten thousand , INF = 0x7fffffff ; int n; struct  a_d { int h, l, r, v; int ld, rd; // left down to } a[N]; inline  bool  cmp (a_d a, a_d b)  { return a.h < b.h; } struct  node { int l, r; int f; node *lc, *rc; } mem[ two * L]; node *memp; node *root; node * init ( int l, int r)  { node *p = memp++; p->l = l; p->r = r; p->f = zero ; if (l == r) { p->lc = p->rc = NULL ; } else { int mid = (l + r) >> one ; p->lc = init (l, mid); p->rc = init (mid + one , r); } return p; } inline  void  push_down (node *p)  { if (p->f) { p->lc->f = p->f; p->rc->f = p->f; p->f = zero ; } } void  change (node *p, int l, int r, int f)  { if (l <= p->l && p->r <= r) { p->f = f; return ; } push_down (p); int mid = (p->l + p->r) >> one ; if (l <= mid) change (p->lc, l, r, f); if (mid < r) change (p->rc, l, r, f); } int  query (node *p, int i)  { if (i == p->l && p->r == i) { return p->f; } push_down (p); int mid = (p->l + p->r) >> one ; if (i <= mid) return  query (p->lc, i); return  query (p->rc, i); } int d[N]; inline  int  max ( int a, int b)  { if (a > b) return a; else  return b; } int  main ()  { //  freopen("input.txt", "r", stdin);

     while ( scanf ( "%d" , &n) !=  EOF) { for ( int i = one ;  i <= n; ++ i) scanf ( "%d%d%d%d" , &a[i].h, &a[i].l, &a[i].r, &a[i].v); a[ zero ].v = zero ; std:: sort (a + one , a + n + one , cmp); //
 //      for (int i = 1; i <= n; ++i) printf("%d %d %d\n", i, a[i].l, a[i].r);  printf("\n"); memp = mem; root = init ( zero , one hundred thousand and ten ); for ( int i = one ;  i <= n; ++ i) { //          printf("%d %d %d\n", i, a[i].l, a[i].r); a[i].ld = query (root, a[i].l); //Note that there is no+1 - 1 a[i].rd = query (root, a[i].r); change (root, a[i].l, a[i].r, i); } //
 //      for (int i = 1; i <= n; ++i) printf("%d %d %d\n", i, a[i].ld, a[i].rd);

         for ( int i = zero ;  i <= n; ++ i) d[i] = -INF; d[n] = one hundred + a[n].v; for ( int i = n;  i; -- i) { if (d[i] <= zero ) continue ; d[a[i].ld] = max (d[a[i].ld], d[i] + a[a[i].ld].v); d[a[i].rd] = max (d[a[i].rd], d[i] + a[a[i].rd].v); } if (d[ zero ] > zero ) printf ( "%d\n" , d[ zero ]); else  printf ( "-1\n" ); } return  zero ; }
Maintenance of the website requires a certain amount of expense. If you agree with this article, please close the advertisement blocker and browse the advertisement. Thank you!
Loading

(∀) In other words, welcome to the small station of lookas!

This is the place where lookas records some things. From time to time, there may be some magical brain holes or unreliable ideas of lookas.

Anyway, let's have a look.