Http://poj.org/problem? Id = 3171
This is a data structure question:
FJ wants to clean the interval [M. E], there are n cows, they clean the interval [T1, T2] cost S. Ask the minimum cost to clean the interval [M. E]. If it cannot be cleaned, output-1.
This is a simple data structure question, but it uses the DP idea a little.
First, dimension m by the first keyword according to the cleaning interval, and the second keyword e is sorted from small to large. Then traverse these cleaning intervals; first query [M-1, E], find the minimum spend min for cleaning to a point in the interval. Then, the value min + S is inserted at the location of E in the online segment, indicating that it takes min + S to fix E. The query and insert operations can be completed using the line segment tree. Note that there will be interruptions, that is, there will be no cattle cleaning in a certain range.
#include<stdio.h>#include<string.h>#include<algorithm>#define dmid int mid=(p.left+p.right)>>1using namespace std;int n,m,e,tot;struct node{int left,right;int lc,rc;int m;}tree[180000];struct Cow{int left,right,s; bool operator<(const Cow &a) const { return left<a.left||left==a.left&&right<a.right; }}cow[100010];void build(int root,int l,int r){node &p=tree[root];p.left=l;p.right=r;p.m=-1;if(l==r) return;dmid;build(p.lc=++tot,l,mid);build(p.rc=++tot,mid+1,r);}void insert(int root,int pos,int v){node &p=tree[root];p.m=p.m==-1?v:min(v,p.m);if(p.left==p.right) return;dmid;if(pos<=mid) insert(p.lc,pos,v); else insert(p.rc,pos,v);}int deal(int a,int b){if(a==-1&&b==-1) return -1;if(a==-1) return b;if(b==-1) return a;return a<b?a:b;}int query(int root,int l,int r){node &p=tree[root];if(p.left==l&&p.right==r) return p.m; dmid; if(mid>=r) return query(p.lc,l,r); else if(mid<l) return query(p.rc,l,r); else { int a=query(p.lc,l,mid); int b=query(p.rc,mid+1,r); return deal(a,b); }}main(){//freopen("in.txt","r",stdin);bool flag;int cur=0,mm;while(scanf("%d%d%d",&n,&m,&e)!=EOF){flag=true;for(int i=1;i<=n;++i) scanf("%d%d%d",&cow[i].left,&cow[i].right,&cow[i].s);sort(cow+1,cow+1+n);tot=0;build(0,m-1,e);insert(0,cur=m-1,0);for(int i=1;i<=n;++i){if(cow[i].left>cur+1){printf("-1\n");flag=false;break;}mm=query(0,cow[i].left-1,cow[i].right);if(mm==-1) continue;insert(0,cow[i].right,mm+cow[i].s);cur=max(cur,cow[i].right);}if(!flag) continue;mm=query(0,e,e);printf("%d\n",mm);}}