题目传送门

题解:

枚举 r 的位置。

线段树每个叶子节点存的是对应的位置到当前位置的价值。

每次往右边移动一个r的话,那么改变的信息有2个信息:

1. sum(a-ci)

2.gap(l, r)

对于第一个东西,我们直接对[1,r]区间内的所有值都加上a-ci就好了。

对于第2个修改的值,首先要明白的是,这个值只会对[1,r-1]内的值存在影响。

并且可以发现,每一段区间的这个gap值每次更新完之后是从左到右递减的,所以我们可以用一个单调栈来维护这个gap值。

代码:

/*
code by: zstu wxk
time: 2019/01/31
*/
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 3e5 + ;
int Wa(){return rand()%;}
void Hack(int n){srand(time());int hack = ;for(int j = ; j <= n; ++j)hack += Wa();if(hack == n)puts("OH No!");}
int n, a;
int c[N], d[N];
LL Mx[N<<], lz[N<<];
void PushDown(int rt){
if(lz[rt]){
Mx[rt<<] += lz[rt];
Mx[rt<<|] += lz[rt];
lz[rt<<] += lz[rt];
lz[rt<<|] += lz[rt];
lz[rt] = ;
}
}
void PushUp(int rt){
Mx[rt] = max(Mx[rt<<], Mx[rt<<|]);
}
void Update(int L, int R, LL C, int l, int r, int rt){
if(L <= l && r <= R){
Mx[rt] += C;
lz[rt] += C;
return ;
}
int m = l+r >> ;
PushDown(rt);
if(L <= m) Update(L, R, C, lson);
if(m < R) Update(L, R, C, rson);
PushUp(rt);
}
struct Node{
int l, r;
LL v;
}B[N];
stack<int> id;
void Ac(){
for(int i = ; i <= n; ++i){
scanf("%d%d", &d[i], &c[i]);
c[i] = a - c[i];
}
LL ans = max(, c[]);
Update(,,c[],,n,);
for(int i = ; i <= n; ++i){
Update(,i,c[i],,n,);
B[i].l = B[i].r = i-;
B[i].v = 1ll * (d[i] - d[i-]) * (d[i] - d[i-]);
while(!id.empty() && B[id.top()].v < B[i].v){
int x = id.top(); id.pop();
Update(B[x].l, B[x].r, B[x].v, , n, );
B[i].l = B[x].l;
}
Update(B[i].l, B[i].r, -B[i].v, , n, );
id.push(i);
ans = max(ans, Mx[]);
}
printf("%I64d\n", ans);
}
int main(){
while(~scanf("%d%d", &n, &a)){
Ac();
}
return ;
}

CodeForces 1107 - G Vasya and Maximum Profit 线段树的更多相关文章

  1. Codeforces 1107G Vasya and Maximum Profit 线段树最大子段和 + 单调栈

    Codeforces 1107G 线段树最大子段和 + 单调栈 G. Vasya and Maximum Profit Description: Vasya got really tired of t ...

  2. [Educational Round 59][Codeforces 1107G. Vasya and Maximum Profit]

    咸鱼了好久...出来冒个泡_(:з」∠)_ 题目连接:1107G - Vasya and Maximum Profit 题目大意:给出\(n,a\)以及长度为\(n\)的数组\(c_i\)和长度为\( ...

  3. 牛客多校第三场 G Removing Stones(分治+线段树)

    牛客多校第三场 G Removing Stones(分治+线段树) 题意: 给你n个数,问你有多少个长度不小于2的连续子序列,使得其中最大元素不大于所有元素和的一半 题解: 分治+线段树 线段树维护最 ...

  4. Codeforces 666E Forensic Examination SAM or SA+线段树合并

    E. Forensic Examination http://codeforces.com/problemset/problem/666/E 题目大意:给模式串S以及m个特殊串,q个询问,询问S的子串 ...

  5. 2018 Arab Collegiate Programming Contest (ACPC 2018) G. Greatest Chicken Dish (线段树+GCD)

    题目链接:https://codeforces.com/gym/101991/problem/G 题意:给出 n 个数,q 次询问区间[ li,ri ]之间有多少个 GCD = di 的连续子区间. ...

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

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

  7. Codeforces 1175F - The Number of Subpermutations(线段树+单调栈+双针/分治+启发式优化)

    Codeforces 题面传送门 & 洛谷题面传送门 由于这场的 G 是道毒瘤题,蒟蒻切不动就只好来把这场的 F 水掉了 看到这样的设问没人想到这道题吗?那我就来发篇线段树+单调栈的做法. 首 ...

  8. Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)

    题目链接:http://codeforces.com/contest/522/problem/D 题目大意:  给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...

  9. Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树

    C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...

随机推荐

  1. Mac Android 配置环境变量

    进入终端,输入以下命令: cd ~ touch .bash_profile //没有该文件的话新建一个 vi .bash_profile //vim 形式打开 输入内容jdk变量配置内容: expor ...

  2. Wpf窗口设置可拖动

    在窗口界面的一个控件(TopGrid)设置如下MouseLeftButtonDown事件即可 private void TopGrid_MouseLeftButtonDown(object sende ...

  3. CentOS 7服务器安装brook和bbr加速

    一.安装Brook 执行一键部署脚本 $ wget -N --no-check-certificate wget -N --no-check-certificate https://raw.githu ...

  4. SpringBoot第一天

    一,SpringBoot 介绍 1,如果使用 Spring 开发一个"HelloWorld"的 web 应用: • 创建一个 web 项目并且导入相关 jar 包.SpringMV ...

  5. jquery验证大全

    jQuery验证及限制 绑定键盘监听事件 $(document).on("keypress", ".txt-valid-len", function (e) { ...

  6. (二十四)c#Winform自定义控件-单标题窗体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  7. lumen 路由访问路径

    项目目录/public/index.php/接你设置的路由 比如设置了 $app->get('/test', function () use ($app) {    return $app-&g ...

  8. Go类型别名与类型定义区别

    类型别名和自定义类型区别 自定义类型 //自定义类型是定义了一个全新的类型 //将MyInt定义为int类型 type MyInt int 类型别名 //类型别名规定:TypeAlias只是Type的 ...

  9. Web API Media Type Formatter

    public static void Register(HttpConfiguration config) { // Web API configuration and services // Web ...

  10. mysql datetime timestamp区别

    timestamp 支持数据库级UTC 时区 datetime 不支持  timestamp占4个字节 datetime占8个字节 timestamp所能存储的时间范围为:'1970-01-01 00 ...