大意: 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. html 之 padding,margin

    margin:对象挤压外界 padding:对象挤压自身 例如: td使用margin 对table而言没有任何效果,但使用padding是对table内部的挤压,若是table空间不够,则会扩大ta ...

  2. c# 进阶之 WebAPI

    REST是设计风格而不是标准. webapi有自己的路由. webservice和wcf的协议都是soap协议,数据的序列化和反序列化都是soap的格式.而webapi是Json的数据传递 webap ...

  3. 【bzoj1706】[usaco2007 Nov]relays 奶牛接力跑

    题意 给出一张无向图,求出恰巧经过n条边的最短路. 题解 考虑先离散化,那么点的个数只会有202个最多.于是复杂度里面就可以有一个\(n^3\).考虑构造矩阵\(d^1\)表示经过一条边的最短路,那么 ...

  4. Hive安装部署及简单测试 网页《一》

    1.首先关闭机器上之前配置的分布式Hadoop 命令: (在hadoop的安装目录中)  sbin/stop-dfs.sh              关闭: yarn   命令:  sbin/stop ...

  5. Lintcode9-Fizz Buzz-Easy

    Fizz Buzz Given number n. Print number from 1 to n. But: when number is divided by 3, print "fi ...

  6. unity 截图 压缩 处理

    /****************************************************** unity屏幕截图,并转换成Base64码* 作者: lyb* 日期:2017年7月25 ...

  7. class与struct的区别

    C++中的struct对C中的struct进行了扩充,它已经不再只是一个包含不同数据类型的数据结构了,它已经获取了太多的功能: ①struct能包含成员函数吗? 能! ②struct能继承吗? 能!! ...

  8. js,jq获取父,兄弟,子节点整理

    js获取节点 父: parentNode 获取已知节点的父节点. 子: childNodes; 得到全部子节点 children 得到全部子节点 firstChild 获得第一个子节点 lastChi ...

  9. win10 安装keras

    1.安装Python环境 建议安装Anconda3 ,4.2.0版本 下载地址: https://repo.continuum.io/archive/index.html 或 https://mirr ...

  10. git创建、删除分支

    //创建分支 git branch branchname //创建并切换到新分支 git checkout -b branchname //远程分支 git push origin branchnam ...