题意:给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;
}