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

设$dp[i]$为只考虑前$i$条赛道的最大收益, $calc(i,j)$为占用区间$[i,j]$的赛道的比赛收益和, $s$为$a$的前缀和, 有

$$dp[i]=\max\limits_{1\le j < i}(dp[j]+calc(j+1,i)+s[j])-s[i]$$

$calc$的贡献用线段树更新即可, 水题一道.

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) {REP(i,1,n) cout<<a[i]<<' ';hr;}
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head #ifdef ONLINE_JUDGE
const int N = 1e6+10;
#else
const int N = 111;
#endif int n, m, a[N];
ll s[N], dp[N], f[N];
struct _ {
int l,r,w;
bool operator < (const _ & rhs) const {
return r<rhs.r;
}
} q[N];
ll v[N<<2], tag[N<<2];
void pd(int o) {
if (tag[o]) {
v[lc]+=tag[o],tag[lc]+=tag[o];
v[rc]+=tag[o],tag[rc]+=tag[o];
tag[o]=0;
}
}
void add(int o, int l, int r, int ql, int qr, ll w) {
if (ql<=l&&r<=qr) return v[o]+=w,tag[o]+=w,void();
pd(o);
if (mid>=ql) add(ls,ql,qr,w);
if (mid<qr) add(rs,ql,qr,w);
v[o]=max(v[lc],v[rc]);
}
ll qry(int o, int l, int r, int ql, int qr) {
if (ql<=l&&r<=qr) return v[o];
pd(o);
ll ans = 0;
if (mid>=ql) ans=max(ans,qry(ls,ql,qr));
if (mid<qr) ans=max(ans,qry(rs,ql,qr));
return ans;
} int main() {
scanf("%d%d", &n, &m);
REP(i,1,n) scanf("%d", a+i),s[i]=s[i-1]+a[i];
REP(i,1,m) scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].w);
sort(q+1,q+1+m);
int now = 1;
REP(i,1,n) {
while (now<=m&&q[now].r==i) {
add(1,0,n,0,q[now].l-1,q[now].w);
++now;
}
dp[i] = max(dp[i-1], qry(1,0,n,0,i-1)-s[i]);
add(1,0,n,i,i,dp[i]+s[i]);
}
printf("%lld\n", dp[n]);
}

Linear Kingdom Races CodeForces - 115E (线段树优化dp)的更多相关文章

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

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

  2. D - The Bakery CodeForces - 834D 线段树优化dp···

    D - The Bakery CodeForces - 834D 这个题目好难啊,我理解了好久,都没有怎么理解好, 这种线段树优化dp,感觉还是很难的. 直接说思路吧,说不清楚就看代码吧. 这个题目转 ...

  3. New task CodeForces - 788E (线段树优化dp)

    比较套路的一个题, 对每个数维护一颗线段树来转移就好了. #include <iostream> #include <algorithm> #include <cstdi ...

  4. Codeforces 1603D - Artistic Partition(莫反+线段树优化 dp)

    Codeforces 题面传送门 & 洛谷题面传送门 学 whk 时比较无聊开了道题做做发现是道神题( 介绍一种不太一样的做法,不观察出决策单调性也可以做. 首先一个很 trivial 的 o ...

  5. Codeforces Round #426 (Div. 2) D 线段树优化dp

    D. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...

  6. BZOJ2090: [Poi2010]Monotonicity 2【线段树优化DP】

    BZOJ2090: [Poi2010]Monotonicity 2[线段树优化DP] Description 给出N个正整数a[1..N],再给出K个关系符号(>.<或=)s[1..k]. ...

  7. [AGC011F] Train Service Planning [线段树优化dp+思维]

    思路 模意义 这题真tm有意思 我上下楼梯了半天做出来的qwq 首先,考虑到每K分钟有一辆车,那么可以把所有的操作都放到模$K$意义下进行 这时,我们只需要考虑两边的两辆车就好了. 定义一些称呼: 上 ...

  8. 【bzoj3939】[Usaco2015 Feb]Cow Hopscotch 动态开点线段树优化dp

    题目描述 Just like humans enjoy playing the game of Hopscotch, Farmer John's cows have invented a varian ...

  9. POJ 2376 Cleaning Shifts (线段树优化DP)

    题目大意:给你很多条线段,开头结尾是$[l,r]$,让你覆盖整个区间$[1,T]$,求最少的线段数 题目传送门 线段树优化$DP$裸题.. 先去掉所有能被其他线段包含的线段,这种线段一定不在最优解里 ...

随机推荐

  1. 比较好的一些 ConcurrentHashMap讲解博客

    jdk8 https://blog.csdn.net/jianghuxiaojin/article/details/52006118#commentBox jdk7.8 https://crossov ...

  2. P4008 [NOI2003]文本编辑器

    思路 FHQ Treap的板子 用FHQ Treap维护中序遍历序列即可 然后数组开够! 代码 #include <cstdio> #include <cstring> #in ...

  3. Optical Flow related Tutorials

    Optical Flow related Tutorials 2017-04-01 10:50:55 Reference: 1. http://blog.csdn.net/carson2005/art ...

  4. The issus in Age Progression/Regression by Conditional Adversarial Autoencoder (CAAE)

    The issus in Age Progression/Regression by Conditional Adversarial Autoencoder (CAAE) Today I tried ...

  5. facebook api之Marketing API

    General information on the Marketing APIs, access, versioning and more. The main use cases for the M ...

  6. Docker7之Docker overview

    Docker is an open platform for developing, shipping, and running applications. Docker enables you to ...

  7. dynamic web module讲解

    一.java的web系统有多种类型,比如静态的和动态的,然后动态的java web project要设置dynamic web module,也就是动态网页模型,他必须要和对应的服务器搭配好了才能跑, ...

  8. .NET下使用 Seq结构化日志系统

    前言 我们公司在日志管理方面一直没有统一,主要痛点有: 每个开发人员都是各用各的,存储日志的形式也是五花八门,如:本地文件,数据库,Redis,MongoDB 由于公司访问服务器要通过堡垒机,所以本机 ...

  9. Mybatis工程搭建

    工程搭建 • 1依赖包 • 2配置文件 • 2.1spring-mybatis.xml • 2.2mybatis-config.xml自带配置文件 • 2.3 mapper(dao)对象 • 2.4 ...

  10. 理解 Redis(3) - 字符串值

    正如前面所讲的, redis 的数据结构就是一系列的键值对键 -> printable ASCII (可打印的 ASCII 码, 最大值是 512MB)值 -> Primitives (基 ...