HDOJ3016Man Down(线段树(区间更新,单点查询)+DP)

Man Down

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


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
 

Recommend
gaojie
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 1000000+10;
int dp[maxn];
struct plank{
	int h,xl,xr,val;
	int x,y;
	int id;
	friend bool operator < (plank a,plank b){
		return a.h < b.h;
	}
}pp[maxn];
struct node{
	int lson,rson,id;
	int mid(){
		return lson+(rson-lson)/2;
	}
}tree[maxn*4];
int n;
void init(){
	memset(dp,0,sizeof(dp));
}
void pushdown(int pos){
	int Ls = pos*2,Rs = pos*2+1;
	int id = tree[pos].id;
	if(id != -1 && tree[pos].lson != tree[pos].rson){
		tree[Ls].id = id;
		tree[Rs].id = id; 
		tree[pos].id = -1;
	}
}
void buildtree(int l,int r,int pos){
	tree[pos].rson = r;
	tree[pos].lson = l;
	tree[pos].id = 0;
	if(l == r)	return;
	int mid = tree[pos].mid();
	buildtree(l,mid,pos*2);
	buildtree(mid+1,r,pos*2+1);
}
void update(int L,int R,int pos,int id){
	if(tree[pos].lson >= L && tree[pos].rson <= R){
		tree[pos].id = id;
	}else{
		pushdown(pos);
		int mid = tree[pos].mid();
		if(R <= mid){
			update(L,R,pos*2,id);
		}
		else if(L > mid){
			update(L,R,pos*2+1,id);
		}else{
			update(L,mid,pos*2,id);
			update(mid+1,R,pos*2+1,id);
		}
	}
}
int query(int pos,int ind){
	if(tree[ind].id != -1) return tree[ind].id;
	else{
		int mid = tree[ind].mid();
		if(pos <= mid) return query(pos,ind*2);
		else return query(pos,ind*2+1);
	}
}
void input(){
	int my=0;
	for(int i = 1; i <= n; i++){
    	scanf("%d%d%d%d",&pp[i].h,&pp[i].xl,&pp[i].xr,&pp[i].val);
    	my = max(my,pp[i].xr);
    }
    sort(pp+1,pp+1+n);
	buildtree(1,my,1);
}
void solve(){
	for(int i = 1; i <= n; i++){
		pp[i].x = query(pp[i].xl,1);
		pp[i].y = query(pp[i].xr,1);
		update(pp[i].xl,pp[i].xr,1,i);
	}
	dp[n] = 100+pp[n].val;
	for(int i = n; i >= 1; i--){
		dp[pp[i].x] = max(dp[pp[i].x],dp[i]+pp[pp[i].x].val);
		dp[pp[i].y] = max(dp[pp[i].y],dp[i]+pp[pp[i].y].val);
	}
	if(dp[0]>0) cout<<dp[0]<<endl;
	else cout<<"-1"<<endl;
	
}
int main(){
    while(~scanf("%d",&n)){
    	init();
    	input();
    	solve();
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值