[CF115E]Linear Kingdom Races

题目大意:

有\(n(n\le10^5)\)个物品,编号为\(1\sim n\)。选取第\(i\)个物品需要\(c_i\)的代价。另外有\(m(m\le10^5)\)个条件,表示若\(l_i\sim r_i\)间的物品全部选择,可以获得\(p_i\)的收益。求最大收益。

思路:

用\(f[i]\)表示考虑完前\(i\)个物品是否选取能获得的最大收益。

转移方程为\(f[i]=\max\{f[j]-\texttt{cost}(j+1,i)+\texttt{profit}(j+1,i)\}\)。

使用线段树优化即可。

时间复杂度\(\mathcal O((n+m)\log n)\)。

源代码:

#include<cstdio>
#include<cctype>
#include<climits>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef long long int64;
const int N=2e5+1,M=2e5;
int c[N];
int64 f[N];
struct Segment {
int l,r,p;
bool operator < (const Segment &rhs) const {
return r<rhs.r;
}
};
Segment s[M];
class SegmentTree {
#define _left <<1
#define _right <<1|1
#define mid ((b+e)>>1)
private:
int64 val[N<<2],tag[N<<2];
void push_up(const int &p) {
val[p]=std::max(val[p _left],val[p _right]);
}
void push_down(const int &p) {
if(tag[p]==0) return;
tag[p _left]+=tag[p];
tag[p _right]+=tag[p];
val[p _left]+=tag[p];
val[p _right]+=tag[p];
tag[p]=0;
}
public:
void modify(const int &p,const int &b,const int &e,const int &l,const int &r,const int64 &x) {
if(b==l&&e==r) {
val[p]+=x;
tag[p]+=x;
return;
}
push_down(p);
if(l<=mid) modify(p _left,b,mid,l,std::min(mid,r),x);
if(r>mid) modify(p _right,mid+1,e,std::max(mid+1,l),r,x);
push_up(p);
}
int64 query(const int &p,const int &b,const int &e,const int &l,const int &r) {
if(b==l&&e==r) return val[p];
push_down(p);
int64 ret=0;
if(l<=mid) ret=std::max(ret,query(p _left,b,mid,l,std::min(mid,r)));
if(r>mid) ret=std::max(ret,query(p _right,mid+1,e,std::max(mid+1,l),r));
return ret;
}
#undef _left
#undef _right
#undef mid
};
SegmentTree t;
int main() {
const int n=getint(),m=getint();
for(register int i=1;i<=n;i++) c[i]=getint();
for(register int i=0;i<m;i++) {
const int l=getint(),r=getint(),p=getint();
s[i]=(Segment){l,r,p};
}
std::sort(&s[0],&s[m]);
for(register int i=1,j=0;i<=n;i++) {
t.modify(1,0,n,0,i-1,-c[i]);
for(;j<m&&s[j].r<=i;j++) {
t.modify(1,0,n,0,s[j].l-1,s[j].p);
}
f[i]=std::max(f[i-1],t.query(1,0,n,0,i-1));
t.modify(1,0,n,i,i,f[i]);
}
printf("%lld\n",f[n]);
return 0;
}

[CF115E]Linear Kingdom Races的更多相关文章

  1. 【CF115E】Linear Kingdom Races 题解(线段树优化DP)

    前言:前辈讲课时设的状态还是有些繁琐,感觉题解设的状态更简洁. -------------- 题目链接 题目大意:给定$n$条道路和$m$场比赛,每个道路修建需要$c_i$,每场比赛需要使用$[l_i ...

  2. Linear Kingdom Races CodeForces - 115E (线段树优化dp)

    大意: n条赛道, 初始全坏, 修复第$i$条花费$a_i$, m场比赛, 第$i$场比赛需要占用$[l_i,r_i]$的所有赛道, 收益为$w_i$, 求一个比赛方案使得收益最大. 设$dp[i]$ ...

  3. [Codeforces 115E]Linear Kingdom Races

    题目大意: 有n块地,初始是荒地.你可以把某些荒地开垦(需要花费相应的价值\(a_i\)(正整数)),然后这些荒地就可以种田. 现在有m年,每年要在l到r区间内种田,获得p(正整数)的价值(必须保证l ...

  4. CodeForces - 115E:Linear Kingdom Races (DP+线段树+lazy)

    pro: 从左到有有N个车道,都有一定程度损坏,所以有不同的修理费a[]: 有M场比赛,每场比赛的场地是[Li,Ri],即如果这个区间的车道都被修理好,则可以举办这个比赛,并且收益是Pi.问最多得到多 ...

  5. C - Tram

    Problem description Linear Kingdom has exactly one tram line. It has n stops, numbered from 1 to n i ...

  6. blade and soul races guide

    Race Four races are available for those who wish to choose the path of martial arts: the careful Gon ...

  7. 萌新笔记——Cardinality Estimation算法学习(二)(Linear Counting算法、最大似然估计(MLE))

    在上篇,我了解了基数的基本概念,现在进入Linear Counting算法的学习. 理解颇浅,还请大神指点! http://blog.codinglabs.org/articles/algorithm ...

  8. Java实现线性阈值模型(Linear Threshold Model)

    影响力传播的线性阈值模型: 网络中连接任意两个节点u,v之间的边都有权重,任意一个节点它的各个邻居节点的边的权重之和为1,即 N(v):neighbors of v. 网络中的节点分为已激活节点和未激 ...

  9. 广义线性模型(Generalized Linear Models)

    前面的文章已经介绍了一个回归和一个分类的例子.在逻辑回归模型中我们假设: 在分类问题中我们假设: 他们都是广义线性模型中的一个例子,在理解广义线性模型之前需要先理解指数分布族. 指数分布族(The E ...

随机推荐

  1. 【leetcode 简单】 第五十一题 有效电话号码

    给定一个包含电话号码列表(一行一个电话号码)的文本文件 file.txt,写一个 bash 脚本输出所有有效的电话号码. 你可以假设一个有效的电话号码必须满足以下两种格式: (xxx) xxx-xxx ...

  2. 【leetcode 简单】 第七题 合并两个有序链表

    将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1->2- ...

  3. 2、Web基本介绍及Html语法介绍

    1.1 Web基本介绍 1.web就是world wide web的缩写.称之为全球广域网,俗称www.2.我们可以将web理解为就是当前的一种互联网.对于我们来说更多的就是网站服务.3.网站我们可以 ...

  4. java实现ftp文件上传下载,解决慢,中文乱码,多个文件下载等问题

    //文件上传 public static boolean uploadToFTP(String url,int port,String username,String password,String ...

  5. mysql cursor游标的使用,实例

    mysql被oracle收购后,从mysql-5.5开始,将InnoDB作为默认存储引擎,是一次比较重大的突破.InnoDB作为支持事务的存储引擎,拥有相关的RDBMS特性:包括ACID事务支持,数据 ...

  6. VMware-workstation-6.5.2-156735.exe

    480HD-KPZ2X-TA56C-4YTQQ VMware 12 专业版永久许可证密钥 5A02H-AU243-TZJ49-GTC7K-3C61N

  7. strptime和strptime函数理解

    #include <stdio.h> #include <time.h> int main() { struct tm tm; char buf[255]; strptime( ...

  8. Spring mvc知识点总结——面试篇

    一.MVC思想MVC(Model-View-Controller)三元组的概念:1.Model(模型):数据模型,提供要展示的数据,因此包含数据和行为,可以认为是领域模型或JavaBean组件(包含数 ...

  9. 【hdoj_2566】统计硬币(母函数?)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2566 本题可以借鉴母函数(组合数学)的思想. 题目可以这样理解:给一堆硬币,分别有1,2,5元的各无数个, ...

  10. 洛谷P1094纪念品分组 题解

    题目传送门 首先的思路就是贪心.先将所有的纪念品按照价格从低到高进行排序.在分别从左到右.从右到左合并纪念品.如果两端纪念品价格超过了上上限,那么就将较大的那一个纪念品独自放入.否则将两个纪念品一起放 ...