题目链接

题目大意

给你一个长为n的数组a,要你构造一个非严格单调上升的数组b和一个非严格单调下降的数组c,使得\(b_i+c_i=a_i\) 要你使这两个数组b,c中最大的元素最小,还有q次修改(q<=1e5),每次修改使[l,r]上的所有元素加x。在线查询

题目思路

看到这个区间操作,在线查询,就能想到线段树qwq,结果居然是差分

假设现在已经确定了\(b[1]\)\(c[1]\)。

假设\(a[2]\)大于\(a[1]\)而\(b[2]+c[2]=a[2]\)

显然是想要b数组的每一个元素尽可能小,而c数组是非严格单调上升那么\(c[2]=c[1]\),则\(b[2]=b[2]+(a[2]-a[1])\)

同理可以得出结论

当\(a[i]>a[i-1]\)

\(b[i]=b[i-1]+(a[i]-a[i-1])\;\;c[i]=c[i-1]\)

当\(a[i]<a[i-1]\)

\(b[i]=b[i-1]\;\;c[i]=c[i-1]+(a[i]-a[i-1])\)

而\(ans=\max(b[1],c[n])\)

\(b[1]+c[1]=a[1]\)

\(b[n]=b[1]+s=\sum_{i=2}^{i=n}\max(a[i]-a[i-1],0)\)

上述两式可以推出

\(b[n]+c[1]=a[1]+s\)

则要使得ans最小,则要使得b[n]和c[1]尽可能相近,\(ans=(a[1]+2)/2\)

易错:注意负数/2,c语言中/2是向0靠近

代码

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#define fi first
#define se second
#define debug printf(" I am here\n");
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e5+5,inf=0x3f3f3f3f;
const double eps=1e-10;
int n,q;
ll a[maxn],b[maxn],res;
ll cal(){
ll ans=res+b[1];
if(ans>0){
return (ans+1)/2;
}else{
return ans/2;
}
}
signed main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
b[i]=a[i]-a[i-1];
if(i!=1&&b[i]>0){
res+=b[i];
}
}
printf("%lld\n",cal());
scanf("%d",&q);
for(int i=1,l,r,x;i<=q;i++){
scanf("%d%d%d",&l,&r,&x);
if(l!=1){
res=res-max(0ll,b[l])+max(0ll,b[l]+x);
}
b[l]+=x;
if(r!=n){
res=res-max(0ll,b[r+1])+max(0ll,b[r+1]-x);
}
b[r+1]-=x;
printf("%lld\n",cal());
}
return 0;
}

Codeforces Round #670 (Div. 2) D. Three Sequences 题解(差分+思维+构造)的更多相关文章

  1. 数学 Codeforces Round #219 (Div. 2) B. Making Sequences is Fun

    题目传送门 /* 数学:这题一直WA在13组上,看了数据才知道是计算cost时超long long了 另外不足一个区间的直接计算个数就可以了 */ #include <cstdio> #i ...

  2. Codeforces Round #609 (Div. 2)前五题题解

    Codeforces Round #609 (Div. 2)前五题题解 补题补题…… C题写挂了好几个次,最后一题看了好久题解才懂……我太迟钝了…… 然后因为longlong调了半个小时…… A.Eq ...

  3. Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思维)

    Problem  Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 1000 mSec Problem Descripti ...

  4. Codeforces Round #219 (Div. 2) B. Making Sequences is Fun

    B. Making Sequences is Fun time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  5. Codeforces Round #162 (Div. 1) B. Good Sequences (dp+分解素数)

    题目:http://codeforces.com/problemset/problem/264/B 题意:给你一个递增序列,然后找出满足两点要求的最长子序列 第一点是a[i]>a[i-1] 第二 ...

  6. Codeforces Round #450 (Div. 2) D.Unusual Sequences (数学)

    题目链接: http://codeforces.com/contest/900/problem/D 题意: 给你 \(x\) 和 \(y\),让你求同时满足这两个条件的序列的个数: \(a_1, a_ ...

  7. Codeforces Round #670 (Div. 2) 深夜掉分(A - C题补题)

    1406A. Subset Mex https://codeforces.com/contest/1406/problem/A Example input 4 6 0 2 1 5 0 1 3 0 1 ...

  8. Divide by Zero 2021 and Codeforces Round #714 (Div. 2) B. AND Sequences思维,位运算 难度1400

    题目链接: Problem - B - Codeforces 题目 Example input 4 3 1 1 1 5 1 2 3 4 5 5 0 2 0 3 0 4 1 3 5 1 output 6 ...

  9. Codeforces Round #670 (Div. 2) C. Link Cut Centroids (dfs,树)

    C. Link Cut Centroids Fishing Prince loves trees, and he especially loves trees with only one centro ...

随机推荐

  1. EXPECT交互式批量上传公钥

    EXPECT交互式批量上传公钥 # vim key.sh #/bin/bash Public_Key() { [ ! -f /usr/bin/expect ] && yum insta ...

  2. SpringBoot第四集:静态资源与首页定(2020最新最易懂)

    SpringBoot第四集:静态资源与首页定(2020最新最易懂) 问题 SpringBoot构建的项目结构如下:没有webapp目录,没有WEB-INF等目录,那么如果开发web项目,项目资源放在那 ...

  3. 适合 C++ 新手学习的开源项目——在 GitHub 学编程

    作者:HelloGitHub-小鱼干 俗话说:万事开头难,学习编程也是一样.在 HelloGitHub 的群里,经常遇到有小伙伴询问编程语言如何入门方面的问题,如: 我要学习某一门编程语言,有什么开源 ...

  4. C语言经典100例-ex002

    系列文章<C语言经典100例>持续创作中,欢迎大家的关注和支持. 喜欢的同学记得点赞.转发.收藏哦- 后续C语言经典100例将会以pdf和代码的形式发放到公众号 欢迎关注:计算广告生态 即 ...

  5. Redux学习day1

    01.React介绍 Redux是一个用来管理管理数据状态和UI状态的JavaScript应用工具.随着JavaScript单页应用(SPA)开发日趋复杂,JavaScript需要管理比任何时候都要多 ...

  6. mac下docker安装php链接使用国产数据库驱动

    docker下配置php macos系统无法使用神通数据库,所以使用docker来安装php 第一步先安装php docker search php docker pull php:7.1-fpm d ...

  7. C中二叉排序树的非递归和递归插入操作以及中序遍历代码实现【可运行】

    C中二叉排序树的非递归和递归插入操作以及中序遍历代码实现[可运行] #include <stdio.h> #include <stdlib.h> typedef int Key ...

  8. JVM常用调优工具介绍

    前言 王子在之前的JVM文章中已经大体上把一些原理性问题说清楚了,今天主要是介绍一些实际进行JVM调优工作的工具和命令,不会深入讲解,因为网上资料很多,篇幅可能不长,但都是实用的内容,小伙伴们有不清楚 ...

  9. ESP8266交叉编译环境变量设置

    在build目录下执行sudo cp -r xtensa-lx106-elf /opt 修改xtensa-lx106-elf目录权限:这一步非常重要,否则在后续编译中很容易出现没有权限问题:sudo ...

  10. JS仿贪吃蛇:一串跟着鼠标的Div

    贪吃蛇是一款80后.90后比较熟悉的经典游戏,下面通过简单的JS代码来实现低仿版贪吃蛇效果:随着鼠标的移动,在页面中呈现所有Div块跟随鼠标依次移动,效果如下图所示. <!DOCTYPE htm ...