设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. 2014-4-5安装python以及基础知识

    1.下载安装 从python官网下载python2.7.6 https://www.python.org/download/releases/2.7.6 建议用迅雷下载 会比较快 2.交互式解释器 运 ...

  2. C语言不容易识别的坑

    1.重复两次定义 #include<stdio.h> #include<stdlib.h> #include<string.h> int a,b; void fun ...

  3. 面向对象:元类、异常处理(try...except...)

    元类: python中一切皆对象,意味着: 1. 都可以被引用,如 x = obj 2. 都可以被当做函数的参数传入 3. 都可以被当做函数的返回值 4. 都可以当做容器类的元素(列表.字典.元祖.集 ...

  4. python爬虫实现原理

    前言 简单来说互联网是由一个个站点和网络设备组成的大网,我们通过浏览器访问站点,站点把HTML.JS.CSS代码返回给浏览器,这些代码经过浏览器解析.渲染,将丰富多彩的网页呈现我们眼前: 一.爬虫是什 ...

  5. cakephp2.3.8中何为component

    大胆假设,小心求证         记得这句话是以前读高三的时候,一个数学老师(圆头,有点胖胖的老师,不是很记得),经常挂在嘴边的一句话, 对于我们不理解,或者说是无法确定的东西,我们不妨先大胆地去假 ...

  6. owncloud

    owncloud https://dl.iuscommunity.org/pub/ius/stable/CentOS/6/x86_64/ [root@n1 ~]# rpm -Uvh ius-relea ...

  7. [TypeScript] Define Custom Type Guard Functions in TypeScript

    One aspect of control flow based type analysis is that the TypeScript compiler narrows the type of a ...

  8. Android自己主动提示文本框(AutoCompleteTextView)

    自己主动提示文本框(AutoCompleteTextView)能够加强用户体验,缩短用户的输入时间(百度的搜索框就是这个效果). 首先.在xml中定义AutoCompleteTextView控件: a ...

  9. 【Linux多线程】三个经典同步问题

    在了解了<同步与互斥的区别>之后,我们来看看几个经典的线程同步的例子.相信通过具体场景可以让我们学会分析和解决这类线程同步的问题,以便以后应用在实际的项目中. 一.生产者-消费者问题 问题 ...

  10. Hdu2111

    <span style="color:#6600cc;">/* J - Saving HDU Time Limit:1000MS Memory Limit:32768K ...