大意: 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. HDU 5852 Intersection is not allowed!(LGV定理行列式求组合数)题解

    题意:有K个棋子在一个大小为N×N的棋盘.一开始,它们都在棋盘的顶端,它们起始的位置是 (1,a1),(1,a2),...,(1,ak) ,它们的目的地是 (n,b1),(n,b2),...,(n,b ...

  2. SpringBoot JDBC 源码分析之——NamedParameterJdbcTemplate 查询数据返回bean对象

    1,NamedParameterJdbcTemplate 查询列表 /***测试***/ public void queyBeanTest(){ String s = "select * f ...

  3. 【ASP.NET】System.Web.Routing - PageRouteHandler Class

    用于提供一些属性和方法来定义如何将URL匹配到一个物理文件上面. public PageRouteHandler (string virtualPath, bool checkPhysicalUrlA ...

  4. matplotlib python

    #导入包 import matplotlib.pyplot as plt import numpy as np # 从[-1,1]中等距去50个数作为x的取值 x = np.linspace(-1, ...

  5. module.exports 与 exports

    module.exports 与 exports 注意:1 对于要导出的属性,可以简单直接挂到 exports 对象上2 对于类,为了直接使导出的内容作为类的构造器可以让调用者使用 new 操作符创建 ...

  6. 20165306 预备作业3 Linux安装及学习

    查看了许多教程,VirtualBox和Ubuntu已安装完成.以下为学习Linux基础入门课程的实验报告.实验截图.尚未解决的问题及体会. 实验三 用户及文件权限管理 一.Linux用户管理 (一)查 ...

  7. File操作-将数据库里的数据写入到指定路径的txt文件里

    package com.Cristin.File;//将数据库里的数据写入到指定路径的txt文件里 import java.io.File;import java.io.FileOutputStrea ...

  8. 【Java】【线程】

    /* 栗子 通过Runnable接口实现简历线程实例 */ class Dog implements Runnable{ //重写run函数 public void run(){ int times ...

  9. Codeforces 776D The Door Problem

    题目链接:http://codeforces.com/contest/776/problem/D 把每一个钥匙拆成两个点${x,x+m}$,分别表示选不选这把钥匙. 我们知道一扇门一定对应了两把钥匙. ...

  10. 七牛云存储上传自有证书开启https访问

    虽然七牛云存储也提供免费SSL证书申请,但我就喜欢用其他平台申请的,于是在腾讯云申请了免费SSL证书,正准备在七牛上传,弹出的界面却让我傻了眼,如下图所示: 腾讯免费SSL证书提供了不同服务器环境的版 ...