Man Down

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2030    Accepted Submission(s): 743

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

/*
hdu 3016 dp+线段树
感觉一直没什么思路
由于每次我们只能从两边跳下来,倒推的答案是唯一的
于是先按照高度排个序,然后就类似于叠木板,先判断当前的两端下面是哪个木板
然后取较大值再加上当前值即可
study~~~
hhh-2016-02-29 22:54:23
*/
#include <algorithm>
#include <cmath>
#include <queue>
#include <iostream>
#include <cstring>
#include <map>
using namespace std;
typedef long long ll;
const int maxn = 100050;
const int inf = 0x3f3f3f3f;
struct node
{
int l,r,mid;
int cov;
} tree[maxn<<2]; struct segment
{
int l,r,h;
int val;
void get()
{
scanf("%d%d%d%d",&h,&l,&r,&val);
}
} seg[maxn];
int dp[maxn];
bool cmp(segment a,segment b)
{
return a.h < b.h;
} void push_up(int r)
{
} void build(int i,int l,int r)
{
tree[i].l = l;
tree[i].r = r;
tree[i].cov = 0;
tree[i].mid = (l +r)>>1;
if(l == r)
{
return ;
}
build(i<<1,l,tree[i].mid);
build(i<<1|1,tree[i].mid+1,r);
push_up(i);
} void push_down(int r)
{
int lson = r<<1,rson = r<<1|1;
if(tree[r].cov != -1)
{
tree[lson].cov = tree[r].cov;
tree[rson].cov = tree[r].cov;
tree[r].cov = -1;
}
} void update(int i,int l,int r,int c)
{
if(tree[i].l >= l && tree[i].r <= r)
{
tree[i].cov = c;
return ;
}
push_down(i);
if(l <= tree[i].mid)
update(i<<1,l,r,c);
if(r > tree[i].mid)
update(i<<1|1,l,r,c);
push_up(i);
} int query(int i,int k)
{
if(tree[i].cov != -1)
{
return tree[i].cov;
}
push_down(i);
if(k <= tree[i].mid)
return query(i<<1,k);
else
return query(i<<1|1,k);
push_up(i);
} int main()
{
int n;
while(scanf("%d",&n) != EOF)
{
build(1,1,maxn);
for(int i =1; i <= n; i++)
{
seg[i].get();
}
memset(dp,0,sizeof(dp));
sort(seg+1,seg+n+1,cmp);
int l=0,r=0;
for(int i = 1; i <= n; i++)
{
if(i > 1)
{
l = query(1,seg[i].l);
r = query(1,seg[i].r);
}
dp[i] = max(dp[l],dp[r])+seg[i].val;
update(1,seg[i].l,seg[i].r,i);
}
dp[n] += 100;
if(dp[n] <= 0)
printf("-1\n");
else
printf("%d\n",dp[n]);
}
return 0;
}

  

hdu 3016 dp+线段树的更多相关文章

  1. HDU 3698 DP+线段树

    给出N*M矩阵.每一个点建立灯塔有花费.每一个点的灯塔有连接范围,求每一行都建立一个灯塔的最小花费,要求每相邻两行的灯塔能够互相连接.满足 |j-k|≤f(i,j)+f(i+1,k) DP思路,dp[ ...

  2. ZOJ 3349 Special Subsequence 简单DP + 线段树

    同 HDU 2836 只不过改成了求最长子串. DP+线段树单点修改+区间查最值. #include <cstdio> #include <cstring> #include ...

  3. hdu 4031 attack 线段树区间更新

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

  4. hdu 4288 离线线段树+间隔求和

    Coder Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  5. cf834D(dp+线段树区间最值,区间更新)

    题目链接: http://codeforces.com/contest/834/problem/D 题意: 每个数字代表一种颜色, 一个区间的美丽度为其中颜色的种数, 给出一个有 n 个元素的数组, ...

  6. Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)

    Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...

  7. 题解 HDU 3698 Let the light guide us Dp + 线段树优化

    http://acm.hdu.edu.cn/showproblem.php?pid=3698 Let the light guide us Time Limit: 5000/2000 MS (Java ...

  8. HDU 4719 Oh My Holy FFF(DP+线段树)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)

    Description N soldiers from the famous "*FFF* army" is standing in a line, from left to ri ...

  9. HDU 3698 Let the light guide us(DP+线段树)(2010 Asia Fuzhou Regional Contest)

    Description Plain of despair was once an ancient battlefield where those brave spirits had rested in ...

随机推荐

  1. 第十二条:考虑实现Comparable接口

    与前面讨论的方法不同,compareTo()方法并没有在Object类中定义.相反,它是Comparable接口中唯一的方法. 一个类的实例对象要想是可以比较大小的,那么这个类需要实现Comparab ...

  2. 一句话了解JAVA与大数据之间的关系

    大数据无疑是目前IT领域的最受关注的热词之一.几乎凡事都要挂上点大数据,否则就显得你OUT了.如果再找一个可以跟大数据并驾齐驱的IT热词,JAVA无疑是跟大数据并驾齐驱的一个词语.很多人在提到大数据的 ...

  3. SQL常用语句,随时用随时更新

    更多详细说明文档查询 http://www.postgres.cn/docs/9.5/infoschema-columns.html 1.1通过表名查询表的属性 SELECT * FROM sys.s ...

  4. Document Object Model

    什么是DOM W3C制定的书写HTML分析器的标准接口规范 全称 Document Object Model 文档对象模型DOM为HTML文档提供的一个API(接口) 可以操作HTML文档 <! ...

  5. EasyUI 动态创建对话框Dialog

    // 拒绝审批通过 function rejectApproval() { // 创建填写审批意见对话框 $("<div id='reject-comment'> </di ...

  6. Mego开发文档 - 建模高级主题

    建模高级主题 在建模过程中我们还有许多其他情况,这里列出本框架中的有用特性来用于解决此类问题. 函数映射 我们可以将指定的CLR函数映射到数据库中的系统函数或自定义函数,该特性用于补充框架中未提供的数 ...

  7. WPF 自定义RadioButton样式

    一.RadioButton基本样式 RadioButton基本样式包含两种状态,这里也是使用两张图片来代替两种状态,当然你也可以通过IconFont或Path来替换这两种状态. 效果如下: 样式代码如 ...

  8. 阿里云API网关(18)请求报文和响应报文

    网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...

  9. groovy入门(2-1)Groovy的Maven插件安装:Plugin execution not covered by lifecycle configuration

    参考链接:http://www.cnblogs.com/rightmin/p/4945797.html 1.引入groovy的jar包 2.引入groovy编译插件 3.遇到问题 Plugin exe ...

  10. MySql入门(2-1)windows下安装mysql的两种方式

    一.下载mysql 1.下载解压MySQL 登录oracle主页,需要用户名和口令: lshengqi@netease.com/1wsx**** 下载路径:: https://dev.mysql.co ...