# BZOJ1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚

Portal
Description

Farmer John’s cows, pampered since birth, have reached new heights of fastidiousness. They now require their barn to be immaculate. Farmer John, the most obliging of farmers, has no choice but hire some of the cows to clean the barn. Farmer John has N (1 <= N <= 10,000) cows who are willing to do some cleaning. Because dust falls continuously, the cows require that the farm be continuously cleaned during the workday, which runs from second number M to second number E during the day (0 <= M <= E <= 86,399). Note that the total number of seconds during which cleaning is to take place is E-M+1. During any given second M..E, at least one cow must be cleaning. Each cow has submitted a job application indicating her willingness to work during a certain interval T1..T2 (where M <= T1 <= T2 <= E) for a certain salary of S (where 0 <= S <= 500,000). Note that a cow who indicated the interval 10..20 would work for 11 seconds, not 10. Farmer John must either accept or reject each individual application; he may NOT ask a cow to work only a fraction of the time it indicated and receive a corresponding fraction of the salary. Find a schedule in which every second of the workday is covered by at least one cow and which minimizes the total salary that goes to the cows.

Input

• Line 1: Three space-separated integers: N, M, and E. * Lines 2..N+1: Line i+1 describes cow i’s schedule with three space-separated integers: T1, T2, and S.
第1行：3个正整数N，M，E，用空格隔开．
第2到N+1行：第i+l行给出了编号为i的奶牛的工作计划，即3个用空格隔开的正整数Ti，T2，S．
Output

• Line 1: a single integer that is either the minimum total salary to get the barn cleaned or else -1 if it is impossible to clean the barn.
输出一个整数，表示约翰需要为牛棚清理工作支付的最少费用．如果清理工作不可能完成，
那么输出-1.
Sample Input
3 0 4 //三头牛,要打扫从0到4号stall

0 2 3 //一号牛,从0号stall打扫到2号,工资为3

3 4 2

0 0 1

INPUT DETAILS:

FJ has three cows, and the barn needs to be cleaned from second 0 to second

1. The first cow is willing to work during seconds 0, 1, and 2 for a total

salary of 3, etc.

Sample Output
5

HINT

Source

Silver

f[i] $f[i]$表示到i时刻的最小代价

f[i1]+c $f[i-1]+c$更新区间 lr $l-r$

## 【代码】

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define N 10005
#define M 86400
#define mod 100007
#define INF 1000000001
using namespace std;
typedef long long ll;

{
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}

int n,S,T;

class Cattle{
public:
int x,y,z;
}E[N];

class Seg_Tree{
public:
int l,r;
ll tag,mn;
}e[M<<2];

bool operator <(Cattle a,Cattle b){
return a.x<b.x;
}

void Build(int p,int l,int r)
{
e[p].l=l,e[p].r=r,e[p].mn=e[p].tag=INF;
if(l==r)
return ;
int mid=l+r>>1;
Build(p<<1,l,mid);Build(p<<1|1,mid+1,r);
}

void Pushup(int p){
e[p].mn=min(e[p<<1].mn,e[p<<1|1].mn);
}

void Pushdown(int p)
{
if(e[p].l==e[p].r) return;
if(e[p].tag!=INF)
{
e[p<<1].tag=min(e[p<<1].tag,e[p].tag);
e[p<<1|1].tag=min(e[p<<1|1].tag,e[p].tag);
e[p<<1].mn=min(e[p<<1].mn,e[p].tag);
e[p<<1|1].mn=min(e[p<<1|1].mn,e[p].tag);
e[p].tag=INF;
}
}

void Update(int p,int x,int y,ll z)
{
int l=e[p].l,r=e[p].r,mid=l+r>>1;
if(l==x&&y==r)
{
e[p].tag=e[p].mn=min(e[p].mn,z);
return;
}
Pushdown(p);
if(y<=mid) Update(p<<1,x,y,z);
else if(x>mid) Update(p<<1|1,x,y,z);
else Update(p<<1,x,mid,z),Update(p<<1|1,mid+1,y,z);
//Pushup(p);
}

ll Query(int p,int x)
{
if(x<S) return 0;
int l=e[p].l,r=e[p].r,mid=l+r>>1;
if(l==x&&x==r)
return e[p].mn;
Pushdown(p);
if(x<=mid) return Query(p<<1,x);
return Query(p<<1|1,x);
}

void Input_Init()
{
sort(E+1,E+1+n);
Build(1,S,T);
}

void Solve()
{
for(int i=1;i<=n;i++)
{
if(E[i].x>T||E[i].y<S) continue;
E[i].x=max(E[i].x,S);E[i].y=min(E[i].y,T);
ll tmp=Query(1,E[i].x-1);
if(tmp==INF) {
printf("-1\n");
return;
}
Update(1,E[i].x,E[i].y,tmp+E[i].z);
}
ll tmp=Query(1,T);
printf("%lld\n",tmp==INF?-1:tmp);
}

int main()
{
Input_Init();
Solve();
return 0;
}
06-24 234

01-18 197
07-18 321