设f[i]为i时刻最小花费

把牛按l升序排列,每头牛能用f[l[i]-1]+c[i]更新(l[i],r[i])的区间min,所以用线段树维护f,用排完序的每头牛来更新,最后查询E点即可

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=10005;
const long long inf=1e18;
int n,st,ed;
struct xds
{
int l,r;
long long lz,mn;
}t[2000005];
struct qwe
{
int l,r,v;
}a[N],s[N];
bool cmp(const qwe &a,const qwe &b)
{
return a.l<b.l;
}
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void pd(int ro)
{
if(t[ro].lz!=inf)
{
t[ro<<1].mn=min(t[ro<<1].mn,t[ro].lz);
t[ro<<1].lz=min(t[ro<<1].lz,t[ro].lz);
t[ro<<1|1].mn=min(t[ro<<1|1].mn,t[ro].lz);
t[ro<<1|1].lz=min(t[ro<<1|1].lz,t[ro].lz);
t[ro].lz=inf;
}
}
void build(int ro,int l,int r)
{
t[ro].l=l,t[ro].r=r,t[ro].mn=t[ro].lz=inf;
if(l==r)
return;
int mid=(l+r)>>1;
build(ro<<1,l,mid);
build(ro<<1|1,mid+1,r);
}
long long ques(int ro,int p)
{
if(p<st)
return 0;
if(t[ro].l==t[ro].r)
return t[ro].mn;
pd(ro);
int mid=(t[ro].l+t[ro].r)>>1;
if(p<=mid)
return ques(ro<<1,p);
else
return ques(ro<<1|1,p);
}
void update(int ro,int l,int r,long long v)
{
if(t[ro].l==l&&t[ro].r==r)
{
t[ro].mn=min(t[ro].mn,v);
t[ro].lz=min(t[ro].lz,v);
return;
}
pd(ro);
int mid=(t[ro].l+t[ro].r)>>1;
if(r<=mid)
update(ro<<1,l,r,v);
else if(l>mid)
update(ro<<1|1,l,r,v);
else
update(ro<<1,l,mid,v),update(ro<<1|1,mid+1,r,v);
t[ro].mn=min(t[ro<<1].mn,t[ro<<1|1].mn);
}
int main()
{
n=read(),st=read(),ed=read();
for(int i=1;i<=n;i++)
a[i].l=read(),a[i].r=read(),a[i].v=read();
sort(a+1,a+1+n,cmp);
build(1,st,ed);
for(int i=1;i<=n;i++)
{
long long nw=ques(1,a[i].l-1);
if(nw==inf)
{
puts("-1");
return 0;
}
update(1,a[i].l,a[i].r,a[i].v+nw);
}
long long ans=ques(1,ed);
printf("%lld\n",ans==inf?-1ll:ans);
return 0;
}
/*
3 0 4
0 2 3
3 4 2
0 0 1
*/

bzoj 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚【dp+线段树】的更多相关文章

  1. 【bzoj1672】[USACO2005 Dec]Cleaning Shifts 清理牛棚 dp/线段树

    题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now ...

  2. BZOJ 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚

    题目 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farm ...

  3. [Usaco2005 Dec]Cleaning Shifts 清理牛棚 (DP优化/线段树)

    [Usaco2005 Dec] Cleaning Shifts 清理牛棚 题目描述 Farmer John's cows, pampered since birth, have reached new ...

  4. 【BZOJ】1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚(dp/线段树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1672 dp很好想,但是是n^2的..但是可以水过..(5s啊..) 按左端点排序后 f[i]表示取第 ...

  5. 洛谷P4644 [USACO2005 Dec]Cleaning Shifts 清理牛棚 [DP,数据结构优化]

    题目传送门 清理牛棚 题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness ...

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

    1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 414  Solved: ...

  7. BZOJ_1672_[Usaco2005 Dec]Cleaning Shifts 清理牛棚_动态规划+线段树

    BZOJ_1672_[Usaco2005 Dec]Cleaning Shifts 清理牛棚_动态规划+线段树 题意:  约翰的奶牛们从小娇生惯养,她们无法容忍牛棚里的任何脏东西.约翰发现,如果要使这群 ...

  8. P4644 [Usaco2005 Dec]Cleaning Shifts 清理牛棚

    P4644 [Usaco2005 Dec]Cleaning Shifts 清理牛棚 你有一段区间需要被覆盖(长度 <= 86,399) 现有 \(n \leq 10000\) 段小线段, 每段可 ...

  9. 【BZOJ1672】[Usaco2005 Dec]Cleaning Shifts 清理牛棚 动态规划

    [BZOJ1672][Usaco2005 Dec]Cleaning Shifts Description Farmer John's cows, pampered since birth, have ...

随机推荐

  1. 61. mybatic insert异常:BindingException: Parameter 'name' not found【从零开始学Spring B】

    mybatic insert异常:BindingException: Parameter 'name' not found [从零开始学习Spirng Boot-常见异常汇总] 异常信息如下: nes ...

  2. WebLoad 解析服务器返回的XML格式内容

    Parsing the XML Response get the root node:  var rootNode = document.wlXmls[0].XMLDocument.documentE ...

  3. 【BZOJ3697】采药人的路径(点分治)

    题意:采药人的药田是一个树状结构,每条路径上都种植着同种药材.采药人以自己对药材独到的见解,对每种药材进行了分类.大致分为两类,一种是阴性的,一种是阳性的.采药人每天都要进行采药活动.他选择的路径是很 ...

  4. PatentTips – EMC Virtual File System

    BACKGROUND OF THE INVENTION 1. Field of the Invention The present invention generally relates to net ...

  5. POJ 3468_A Simple Problem with Integers(线段树)

    题意: 给定序列及操作,求区间和. 分析: 线段树,每个节点维护两个数据: 该区间每个元素所加的值 该区间元素和 可以分为"路过"该区间和"完全覆盖"该区间考虑 ...

  6. Ubuntu 16.04安装VirtualBox 5.1实现无缝模式

    个人电脑版的虚拟机推荐使用VirtualBox,因为其免费,比起VMware到处要找破解强得多,且最重要的一点是无缝模式,让其感觉不出再用两个操作系统. 下载: wget http://downloa ...

  7. http转https

    1.先用jdk生成证书 先跳转到jdk的bin目录下:E:\Program Files\Java\jdk1.8.0_91\bin>keytool -genkey -alias tomcat -k ...

  8. SpringBoot 基于jjwt快速实现token授权

    1.添加maven依赖注解 <!--JJWT库--> <dependency> <groupId>io.jsonwebtoken</groupId> & ...

  9. mysql 经常使用命令整理总结

    #改动字段类型 alter table `table_name` modify column ip varchar(50); #添加字段 alter table `table_name` add ip ...

  10. Myeclipse的优化方法

    近期在实习,公司给分配了新的电脑,可是不知道怎么弄得,总是弄得非常卡,没办法仅仅有自己好好整理一下电脑了,另外.为了提高编程的效率.顺便也把Myeclipse也优化了一下. 第一步: 取消自己主动va ...