Problem Description
The 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).

Input
There 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.
Output
If 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
4 10 5 10 10 5 3 6 -100 4 7 11 20 2 2 1000 10
Sample Output
140
Source
2009 Multi-University Training Contest 12 - Host by FZU

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define M 100005
#define inf 0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int tree[M<<2],dp[M][2];
int n;
struct node{int h,l,r,v;bool operator <(const node & obj) const{return h>obj.h;}
}blank[M];
inline void pushdown(int rt) //懒惰标记,用父节点更新子节点
{if(tree[rt]!=-1){tree[rt<<1]=tree[rt<<1|1]=tree[rt];tree[rt]=-1;}
}
void update(int ql,int qr,int v,int l,int r,int rt)
{if(ql<=l&&qr>=r){tree[rt]=v;return;}pushdown(rt);int m=(l+r)>>1;if(qr<=m)update(ql,qr,v,lson);else if(ql>m)update(ql,qr,v,rson);else{update(ql,qr,v,lson);update(ql,qr,v,rson);}
}
int query(int pos,int l,int r,int rt)
{if(l==r){return tree[rt];}pushdown(rt);int m=(l+r)>>1;if(pos<=m)return query(pos,lson);elsereturn query(pos,rson);
}
int main()
{int i;while(scanf("%d",&n)!=EOF){memset(tree,-1,sizeof(tree)); //注意数组是一个[l,r]的区间memset(dp,-1,sizeof(dp));int l=inf,r=-inf;for(i=1;i<=n;i++){scanf("%d%d%d%d",&blank[i].h,&blank[i].l,&blank[i].r,&blank[i].v);l=min(l,blank[i].l);r=max(r,blank[i].r);}sort(blank+1,blank+1+n); //将木板从高到的低排序for(i=n;i>=1;i--)  //这里用逆推,从低到上求值{int j=query(blank[i].l,l,r,1); //j为点blank[i].l下方木板的下标if(j!=-1) //下方有木板,那么下降到木板j时,选着从j的哪一端下降dp[i][0]=max(dp[j][0],dp[j][1])+blank[j].v;else  //下方为空,直接降落到地面dp[i][0]=0;j=query(blank[i].r,l,r,1);if(j!=-1)dp[i][1]=max(dp[j][0],dp[j][1])+blank[j].v;elsedp[i][1]=0;update(blank[i].l,blank[i].r,i,l,r,1); //为板blank[i]赋下标为i}int ans=max(dp[1][0],dp[1][1])+100+blank[1].v;if(ans<=0) ans=-1;printf("%d\n",ans);}return 0;
}

hdu-3016-Man Down(线段树)相关推荐

  1. HDU 3016 Man Down——线段树

    传送门 Problem Description The Game "Man Down 100 floors" is an famous and interesting game.Y ...

  2. hdu 3397 Sequence operation(线段树,lazy,区间合并)

    hdu 3397 Sequence operation 线段树lazy和区间合并结合的一个题,相当于几个题集中到一起嘛,分开想就好了 0,1,2操作都要lazy,2的异或操作找到每一只含1或只含0的区 ...

  3. 2016 Multi-University Training Contest 10 [HDU 5861] Road (线段树:区间覆盖+单点最大小)...

    HDU 5861 题意 在n个村庄之间存在n-1段路,令某段路开放一天需要交纳wi的费用,但是每段路只能开放一次,一旦关闭将不再开放.现在给你接下来m天内的计划,在第i天,需要对村庄ai到村庄bi的道 ...

  4. hdu 2871 Memory Control(线段树)

    题目链接:hdu 2871 Memory Control 题目大意:模拟一个内存分配机制. Reset:重置,释放全部空间 New x:申请内存为x的空间,输出左地址 Free x:释放地址x所在的内 ...

  5. HDU 4031 Attack(线段树/树状数组区间更新单点查询+暴力)

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Sub ...

  6. HDU 4262 Juggler (模拟+线段树优化)

    转载请注明出处,谢谢http://blog.csdn.net/acm_cxlove/article/details/7854526       by---cxlove http://acm.hdu.e ...

  7. HDU 5669 Road(线段树建树)(分层图最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5669 [分析]线段树建树+分层图最短路 #include <cstdio> #includ ...

  8. HDU - I Hate It(线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 Time Limit: 9000/3000 MS (Java/Others) Memory Li ...

  9. HDU - 3333 Turing Tree 线段树区间不同值和+详解+思想

    传送门 首先第一次做这种求不同元素和的线段树题,猜想是个裸题.但是题目中有一句话显然给题目降低了很大的难度,就是 想想其实它就是在暗示你这道题你要结合多次询问来处理,也就是所谓的离线,而不是一次一次的 ...

  10. HDU 3333-Turing Tree(线段树解决离线询问)

    题意: 给定一个长度为n的序列,给定m个查询,每次查询区间[L,R]范围内不同元素的和. 题解: x,yx,yx,y为查询的区间左右端点 用一个数组left[i]left[i]left[i],表示左边 ...

最新文章

  1. java 两份文档相似性_两个数据集之间的相似百分比
  2. 北京科技大学转专业到计算机,北科大学生全可转专业
  3. CMT learning
  4. php ayui表格,layui表格使用
  5. 某游戏在华为鸿蒙,华为鸿蒙系统运行安卓游戏出现新状况!安卓换皮论被彻底打脸?...
  6. RMQ问题-ST表倍增处理静态区间最值
  7. asp.net EF+MVC2实战2
  8. A-Night at the Museum 水题
  9. 关于引用lightbox源码
  10. Quartz和OpenGL绘图基本概念
  11. Hadoop学习笔记一:单节点安装
  12. 记录:添加trace_event埋点并调用
  13. 像中文的罗马音字体复制_帮我把日语的罗马音弄成汉字!
  14. python3 打开网页方法
  15. 辣条君写爬虫4【帮小姐姐删垃圾邮件】
  16. 【NOI1999、LOJ#10019】生日蛋糕(搜索、最优化剪枝、可行性剪枝)
  17. 我在阿里做数据分析师,一位阿里数据分析师的日常
  18. Code Project精彩系列(转)
  19. mj评[杜拉拉升职记]-8.5分
  20. 移动应用崛起新契机:超级app+轻应用

热门文章

  1. inline-block什么意思中文_block,inline和inline-block概念和区别
  2. android设备唯一标识符_安卓设备唯一标识,用什么来获取》?
  3. word2016开机后首次打开非常慢_自从用了这些设置方法,电脑开机瞬间提速了50%,辛亏早知道...
  4. vue自定义一个指令实现el-input-number组件显示千分号,但实际拿到的值是number类型
  5. 机器人出魔切还是三相_LOL11月17日更新内容汇总 国服5.22版S6季前赛上线
  6. typedef struct student与 struct student的区别
  7. 练习(二)——威斯康辛乳腺癌数据集
  8. 灵活用工适合哪些行业
  9. 编程语言win10电源管理在哪里
  10. ios开发循环网络请求_GitHub - JadenTeng/ResourceX: iOS网络请求,网络泛型编程,工具类的封装,基于AFNetworking 实现, NSCache数据缓存...