poj 3171

题意:给n个区间及其代价值,问要覆盖[M,E]区间至少要花费多少代价;

注意初始化足够的数组,

#include <iostream>
#include <cstdio>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <stack>

#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define ull unsigned long long
#define uint unsigned int
#define l(x) ((x)<<1)
#define r(x) ((x)<<1|1)
#define lowbit(x) ((x)&(-(x)))
#define abs(x) ((x)>=0?(x):(-(x)))
#define ms(a,b) memset(a,b,sizeof(a))

using namespace std;

const int maxn = 911111;

struct node {
	int beg, end, val;
	friend bool operator<(const node &a, const node &b) {
		if (a.beg == b.beg)return a.end < b.end;
		return a.beg < b.beg;
	}
};

int n, dat[2 * maxn];
void init(int n_) {
	n = 1;
	while (n < n_) n *= 2;
	ms(dat,INF);
}

void update(int k, int a) {
	k += n - 1; 
	dat[k] = min(a,dat[k]);
	while (k > 0) {
		k = (k - 1) / 2;
		dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
	}
}

//query(a,b,0,0,n)
int query(int a, int b, int k, int l, int r) {
	if (r <= a || b <= l) return INF;
	if (a <= l && r <= b) return dat[k];

	return min(query(a, b, k * 2 + 1, l, (l + r) / 2),
		query(a, b, k * 2 + 2, (l + r) / 2, r));
}

int get(int k) {
	return dat[k + n];
}

int m, e,len;
node demo[maxn];

int main(){
	ios::sync_with_stdio(false);

	cin >> len >> m >> e;
	e -= m;
	init(e+1);
	update(0, 0);
	for (int i = 0; i < len; i++) {
		cin >> demo[i].beg >> demo[i].end >> demo[i].val;
		demo[i].beg = max(0, demo[i].beg - m) + 1;
		demo[i].end = max(0, demo[i].end - m) + 1;
	}
	sort(demo, demo + len);
	for (int i = 0; i < len; i++) {
		int a = query(demo[i].beg-1, demo[i].end, 0, 0, n);
		update(demo[i].end, a+demo[i].val);
	}
	
	cout << (get(e) == INF ? -1 : get(e)) << "\n";
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值