[题目链接]
http://poj.org/problem?id=3171
[算法]
线段树 + dp
[代码]
#include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include <cwchar> #include <cwctype> #include <stack> #include <limits.h> using namespace std; #define MAXN 100010 const int INF = 2e9; struct info { int l,r,s; } a[MAXN]; int i,n,m,e; struct SegmentTree { struct Node { int l,r; int mn; } Tree[MAXN<<2]; inline void build(int index,int l,int r) { int mid; Tree[index].l = l; Tree[index].r = r; Tree[index].mn = INF; if (l == r) return; mid = (l + r) >> 1; build(index<<1,l,mid); build(index<<1|1,mid+1,r); } inline void pushup(int index) { Tree[index].mn = min(Tree[index<<1].mn,Tree[index<<1|1].mn); } inline void update(int index,int pos,int val) { int mid; if (Tree[index].l == Tree[index].r) { Tree[index].mn = min(Tree[index].mn,val); return; } mid = (Tree[index].l + Tree[index].r) >> 1; if (mid >= pos) update(index<<1,pos,val); else update(index<<1|1,pos,val); pushup(index); } inline int query(int index,int l,int r) { int mid; if (Tree[index].l == l && Tree[index].r == r) return Tree[index].mn; mid = (Tree[index].l + Tree[index].r) >> 1; if (mid >= r) return query(index<<1,l,r); else if (mid + 1 <= l) return query(index<<1|1,l,r); else return min(query(index<<1,l,mid),query(index<<1|1,mid+1,r)); } } T; inline bool cmp(info a,info b) { return a.r < b.r; } int main() { scanf("%d%d%d",&n,&m,&e); for (i = 1; i <= n; i++) scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].s); sort(a+1,a+n+1,cmp); T.build(1,m-1,e); T.update(1,m-1,0); for (i = 1; i <= n; i++) { if (a[i].r < m) continue; T.update(1,a[i].r,T.query(1,a[i].l-1,a[i].r-1)+a[i].s); } if (T.query(1,e,e) != INF) printf("%d\n",T.query(1,e,e)); else printf("-1\n"); return 0; }