题目大意:

有n块地,初始是荒地。你可以把某些荒地开垦(需要花费相应的价值\(a_i\)(正整数)),然后这些荒地就可以种田。

现在有m年,每年要在l到r区间内种田,获得p(正整数)的价值(必须保证l~r都已经开荒,否则不能种田)。

问最大收益。

解题思路:

DP。

设F[i][j]表示前i块地,最后有连续的j块地已开荒的最大收益。

则\(F[i+1][0]=max\{F[i][j]\}\)。不开荒,则中间断了,所以连续的值只有0了。

F[i+1][j+1]=F[i][j]-a[i]+v。开荒,则花费价值,而且可能会有一些年份可以种田了,则加上这些收益(v是加上后能多出来的种田收益)。

于是我们要记录下以每个值作为右端点的种田个数。

答案即为\(max\{F[n][i]\}\)

发现这是个时空复杂度都是\(O(n^2)\)的东西。

首先第一维可以滚掉。

然后,考虑每个\(a[i]\)都要在整个区间减一遍,而每年种田的价值也会对一段区间有影响。

所以考虑线段树优化。

线段树每个节点记录这个节点下面的儿子的状态的最优值。

然后发现枚举i时,之前的状态都要向右偏移一格,非常麻烦。

倒着建状态就好辣~喵~o( =∩ω∩= )m

C++ Code:

#include<bits/stdc++.h>
using namespace std;
using LoveLive=long long;
const int N=2e5+5;
LoveLive d[N*4],tag[N*4],a[N];
int n,m,L,R;
LoveLive add;
vector<pair<int,LoveLive>>v[N];
inline LoveLive max(LoveLive&a,LoveLive&b){return a>b?a:b;}
inline void pd(int&o){
if(tag[o]){
int l=o<<1,r=l|1;
d[l]+=tag[o];
d[r]+=tag[o];
tag[l]+=tag[o];
tag[r]+=tag[o];
tag[o]=0;
}
}
void add_1(int l,int r,int o){
if(l==r)d[o]+=add;else{
pd(o);
int mid=l+r>>1;
if(L<=mid)add_1(l,mid,o<<1);else
add_1(mid+1,r,o<<1|1);
d[o]=max(d[o<<1],d[o<<1|1]);
}
}
void add_lot(int l,int r,int o){
if(L<=l&&r<=R)d[o]+=add,tag[o]+=add;else{
int mid=l+r>>1;
pd(o);
if(L<=mid)add_lot(l,mid,o<<1);
if(mid<R)add_lot(mid+1,r,o<<1|1);
d[o]=max(d[o<<1],d[o<<1|1]);
}
}
int main(){
ios::sync_with_stdio(0),cin.tie(0);
cin>>n>>m;
for(int i=1;i<=n;++i)cin>>a[i];
for(int i=1,l,r;i<=m;++i){
cin>>l>>r>>add;
v[r].push_back(make_pair(l,add));
}
for(int i=1;i<=n;++i){
L=n-i,add=d[1];
add_1(0,n,1);
++L,R=n,add=-a[i];
add_lot(0,n,1);
for(auto it:v[i]){
L=n-it.first+1,R=n,add=it.second;
add_lot(0,n,1);
}
}
cout<<d[1]<<endl;
return 0;
}

[Codeforces 115E]Linear Kingdom Races的更多相关文章

  1. [CF115E]Linear Kingdom Races

    [CF115E]Linear Kingdom Races 题目大意: 有\(n(n\le10^5)\)个物品,编号为\(1\sim n\).选取第\(i\)个物品需要\(c_i\)的代价.另外有\(m ...

  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 (DP+线段树+lazy)

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

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

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

  5. codeforces 613D:Kingdom and its Cities

    Description Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. Ho ...

  6. CodeForces - 687D: Dividing Kingdom II (二分图&带权并查集)

    Long time ago, there was a great kingdom and it was being ruled by The Great Arya and Pari The Great ...

  7. CodeForces - 613D:Kingdom and its Cities(虚树+DP)

    Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. However, in or ...

  8. codeforces 687D Dividing Kingdom II 带权并查集(dsu)

    题意:给你m条边,每条边有一个权值,每次询问只保留编号l到r的边,让你把这个图分成两部分 一个方案的耗费是当前符合条件的边的最大权值(符合条件的边指两段点都在一个部分),问你如何分,可以让耗费最小 分 ...

  9. C - Tram

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

随机推荐

  1. Hibernate Session操作

    1.增加 @Test public void add(){ Configuration cfg=new Configuration().configure(); SessionFactory fact ...

  2. Django入门--模型系统(一):模型基础

    1.Django的ORM介绍 对象关系映射(英语:(Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序技术,用于实现面向对象编程语言里不 ...

  3. JSP中文乱码问题的由来以及解决方法

    首先明确一点,在计算机中,只有二进制的数据! 一.java_web乱码问题的由来 1.字符集 1.1 ASCII字符集 在早期的计算机系统中,使用的字符非常少,这些字符包括26个英文字母.数字符号和一 ...

  4. FreeMarker 语法 list

    一.java 代码 @Test public void testFreeMarker() throws Exception { //1.创建一个模板文件 //2.创建一个Configuration对象 ...

  5. CF899A Splitting in Teams

    CF899A Splitting in Teams 题意翻译 n个数,只有1,2,把它们任意分组,和为3的组最多多少 题目描述 There were nn groups of students whi ...

  6. [HTML5] aria-label & aria-labelledby

    'aria-labelledby' overwrite 'aria-label' overwirte native element label. TOP-LEFT: aria-label overwr ...

  7. http格式(graph)

    http请求格式 http请求头 字段 http响应 http响应头字段

  8. ExtJs--16--Ext.override()方法专门用来重写对象的方法

    Ext.onReady(function(){ /** * Ext.override()方法专门用来重写对象的方法 */ //定义个类 Ext.define("U",{ //该类的 ...

  9. Swift String转Character数组

    通过String的characters方法,将String转Character数组 例如: let characters:Array<Character> = Array("01 ...

  10. SAM学习笔记

    SAM学习笔记 后缀自动机(模板)NSUBSTR(Caioj1471 || SPOJ 8222) [题意] 给出一个字符串S(S<=250000),令F(x)表示S的所有长度为x的子串中,出现次 ...