[CF115E]Linear Kingdom Races
[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的更多相关文章
- 【CF115E】Linear Kingdom Races 题解(线段树优化DP)
前言:前辈讲课时设的状态还是有些繁琐,感觉题解设的状态更简洁. -------------- 题目链接 题目大意:给定$n$条道路和$m$场比赛,每个道路修建需要$c_i$,每场比赛需要使用$[l_i ...
- Linear Kingdom Races CodeForces - 115E (线段树优化dp)
大意: n条赛道, 初始全坏, 修复第$i$条花费$a_i$, m场比赛, 第$i$场比赛需要占用$[l_i,r_i]$的所有赛道, 收益为$w_i$, 求一个比赛方案使得收益最大. 设$dp[i]$ ...
- [Codeforces 115E]Linear Kingdom Races
题目大意: 有n块地,初始是荒地.你可以把某些荒地开垦(需要花费相应的价值\(a_i\)(正整数)),然后这些荒地就可以种田. 现在有m年,每年要在l到r区间内种田,获得p(正整数)的价值(必须保证l ...
- CodeForces - 115E:Linear Kingdom Races (DP+线段树+lazy)
pro: 从左到有有N个车道,都有一定程度损坏,所以有不同的修理费a[]: 有M场比赛,每场比赛的场地是[Li,Ri],即如果这个区间的车道都被修理好,则可以举办这个比赛,并且收益是Pi.问最多得到多 ...
- C - Tram
Problem description Linear Kingdom has exactly one tram line. It has n stops, numbered from 1 to n i ...
- blade and soul races guide
Race Four races are available for those who wish to choose the path of martial arts: the careful Gon ...
- 萌新笔记——Cardinality Estimation算法学习(二)(Linear Counting算法、最大似然估计(MLE))
在上篇,我了解了基数的基本概念,现在进入Linear Counting算法的学习. 理解颇浅,还请大神指点! http://blog.codinglabs.org/articles/algorithm ...
- Java实现线性阈值模型(Linear Threshold Model)
影响力传播的线性阈值模型: 网络中连接任意两个节点u,v之间的边都有权重,任意一个节点它的各个邻居节点的边的权重之和为1,即 N(v):neighbors of v. 网络中的节点分为已激活节点和未激 ...
- 广义线性模型(Generalized Linear Models)
前面的文章已经介绍了一个回归和一个分类的例子.在逻辑回归模型中我们假设: 在分类问题中我们假设: 他们都是广义线性模型中的一个例子,在理解广义线性模型之前需要先理解指数分布族. 指数分布族(The E ...
随机推荐
- H5小游戏——看你有多色
使用了封装了canvas的create.js库来实现的. 最终效果: 工程: Rect.js /* * 方块类 */ function Rect(n,color,specialColor){ crea ...
- 【Explain】mysql之explain详解(分析索引的最佳使用)
在日常工作中,我们会有时会开慢查询去记录一些执行时间比较久的SQL语句,找出这些SQL语句并不意味着完事了,些时我们常常用到explain 这个命令来查看一个这些SQL语句的执行计划,查看该SQL语句 ...
- 【zTree】zTree的3.5.26静态树与动态树(实用)
1.静态树: 目录结构:(css与js为下载的原文件夹)
- OGG相关操作
参数文件详解: 1)truncate ogg 进程: Manager进程:manager进程是配置在源端和目标端 Extract(抽取)进程:部署在源端,用于捕获数据表或者日志中的数据文件: Pump ...
- python之自然语言处理入门(一)
前言 NTLK是著名的Python自然语言处理工具包,记录一下学习NTLK的总结. 安装nltk pip install nltk # 测试 import nltk 安装相关的包 import nlt ...
- Ubuntu连接多台Ubuntu server的问题
如果您用的是虚拟机上安装的几个Ubuntu server进行IP配置 要注意以下几点: <1>虚拟机上安装完成Ubuntu server 默认的网络连接方式是NAT ,应该改成桥接网卡 ( ...
- 美化的select下拉框
ie7浏览器以后的下拉框,给他加上边框样式,是没用的.要是想做出样式好看的下拉框需要用js或者jquery来模拟实现. 代码如下: <div class="r"> &l ...
- C# 解决窗体闪烁
C# 解决窗体闪烁 在Windows窗体上造成“闪烁”的窗体上有很多控制.造成这种闪烁的原因有两个: 1.当控件需要被绘制时,Windows发送一个控件两个消息.第一个(WM_ERASEBKGND)导 ...
- mysql 操作时间戳
1.将long显示成时间 SELECT FROM_UNIXTIME(1249488000, '%Y%m%d' ) 2.日期格式化成时间戳 SELECT UNIX_TIMESTAMP('2016-05- ...
- ajax跨域的解决办法
<!DOCTYPE HTML> <html> <head> <meta http-equiv="content-type" content ...