链接:

https://vjudge.net/contest/202699#problem/B

题意:

给出一个序列,要支持区间加和操作

求其中最长的区间,该区间内的元素满足(ai<ai+1<。。。ak>ak+1>。。>aj-1>aj)

要支持区间加值

题解:

是一个很经典的差分

对于相邻两项要判断大小只需看差分数组即可

而对于区间修改,只需对差分数组进行单点修改即可

接下来问题可以转化为,给出一个只有-1,0,1的数列,求最长的11111...-1-1-1-1...

可以考虑用线段树进行维护5个值

1.left1数组 表示从左端点开始都为-1

2.leftt数组 表示从左端点开始先为1再为-1

3.right1数组 表示从右端点之前到右端点都为1

4.rightt数组  表示从右端点之前到右端点 先为1后为-1

5.maxn数组 维护该节点中的最长区间

为了处理的方便,其中2包含1,4包含3

另外,只有当值的正负发生变化时才调用change函数,否则会超时

#其中对right和left的更新有一些复杂,具体见代码

代码:

#include <bits/stdc++.h>
using namespace std;
#define max1 3000000
long long a[max1],maxn[max1],right1[max1],left1[max1],rightt[max1],leftt[max1],sum[max1];
struct re{long long h,t;}p[max1];
void js(long long x)
{
long long len1=p[x*].t-p[x*].h+,len2=p[x*+].t-p[x*+].h+;
maxn[x]=max(max(maxn[x*],maxn[x*+]),max(rightt[x*]+left1[x*+],right1[x*]+leftt[x*+]));
if (right1[x*+]==len2) right1[x]=right1[x*+]+right1[x*]; else right1[x]=right1[x*+];
if (rightt[x*+]==len2)
{
rightt[x]=rightt[x*+]+right1[x*];
if (sum[p[x*+].h]<) rightt[x]=max(rightt[x],rightt[x*+]+rightt[x*]);
}
else rightt[x]=rightt[x*+];
if (left1[x*]==len1) left1[x]=left1[x*+]+left1[x*]; else left1[x]=left1[x*];
if (leftt[x*]==len1)
{
leftt[x]=leftt[x*]+left1[x*+];
if (sum[p[x*].t]>) leftt[x]=max(leftt[x],leftt[x*+]+leftt[x*]);
}
else leftt[x]=leftt[x*];
};
void build(long long x,long long h,long long t)
{
p[x].h=h; p[x].t=t;
if (h==t)
{
long long i=p[x].h;
if (sum[i]==) maxn[x]=left1[x]=right1[x]=rightt[x]=leftt[x]=;
if (sum[i]>) maxn[x]=leftt[x]=rightt[x]=right1[x]=,left1[x]=;
if (sum[i]<) maxn[x]=left1[x]=leftt[x]=rightt[x]=,right1[x]=;
return;
}
long long mid=(h+t)/;
build(x*,h,mid);
build(x*+,mid+,t);
js(x);
};
void change(long long x,long long pos)
{
if (p[x].h==p[x].t)
{
long long i=p[x].h;
if (sum[i]==) maxn[x]=left1[x]=leftt[x]=right1[x]=rightt[x]=;
if (sum[i]>) maxn[x]=leftt[x]=rightt[x]=right1[x]=,left1[x]=;
if (sum[i]<) maxn[x]=left1[x]=leftt[x]=rightt[x]=,right1[x]=;
return;
}
long long mid=(p[x].h+p[x].t)/;
if (pos<=mid) change(x*,pos); else change(x*+,pos);
js(x);
};
int main()
{
std::ios::sync_with_stdio(false);
long long n,m,c,d,e;
cin>>n;
for (long long i=;i<=n;i++) cin>>a[i];
for (long long i=;i<=n;i++) sum[i]=a[i]-a[i-];
build(,,n);
cin>>m;
bool tt;
for (long long i=;i<=m;i++)
{
cin>>c>>d>>e;
if (c!=)
{
tt=false;
if (sum[c]==) tt=true;
if ((sum[c]> && sum[c]+e<=)||(sum[c]< && sum[c]+e>=))
tt=true;
sum[c]+=e;
if (tt) change(,c);
}
tt=false;
if (sum[d+]==) tt=true;
if ((sum[d+]> && sum[d+]-e<=)||(sum[d+]< && sum[d+]-e>=))
tt=true;
sum[d+]-=e;
if (tt) change(,d+);
cout<<maxn[]+<<endl;
}
return();
}

B - Alyona and towers CodeForces - 739C的更多相关文章

  1. Alyona and towers CodeForces - 739C (线段树)

    大意: 给定序列, 要求实现区间加, 询问整个序列最长的先增后减的区间. 线段树维护左右两端递增,递减,先增后减的长度即可, 要注意严格递增, 合并时要注意相等的情况, 要注意相加会爆int. #in ...

  2. Codeforces 739C Alyona and towers 线段树

    Alyona and towers 这个题写起来真的要人命... 我们发现一个区间被加上一个d的时候, 内部的结构是不变的, 改变的只是左端点右端点的值, 这样就能区间合并了. 如果用差分的话会简单一 ...

  3. Codeforces 739C - Alyona and towers(线段树)

    Codeforces 题目传送门 & 洛谷题目传送门 可能有人会问我为什么为这道 *2500 的 D1C 写题解,我觉得大概是想要在写题解数量上 dd ycx 吧,因为 ycx 到目前为止写了 ...

  4. Towers CodeForces - 229D

    The city of D consists of n towers, built consecutively on a straight line. The height of the tower ...

  5. C Alyona and Spreadsheet Codeforces Round #401(Div. 2)(思维)

    Alyona and Spreadsheet 这就是一道思维的题,谈不上算法什么的,但我当时就是不会,直到别人告诉了我,我才懂了的.唉 为什么总是这么弱呢? [题目链接]Alyona and Spre ...

  6. CODEFORCES ROUND #740 ANALYSES BY TEAM:RED & BLACK

    A.Alyona and copybooks Problems: 给你一个数n和代价分别为a, b, c.数量不限的1, 2, 3,求将n凑成4的倍数的最小代价 Analysis: cj:取个模随便凑 ...

  7. codeforces740B

    Alyona and flowers CodeForces - 740B Little Alyona is celebrating Happy Birthday! Her mother has an ...

  8. Codeforces 740C. Alyona and mex 思路模拟

    C. Alyona and mex time limit per test: 2 seconds memory limit per test: 256 megabytes input: standar ...

  9. Codeforces 740A. Alyona and copybooks 模拟

    A. Alyona and copybooks time limit per test: 1 second memory limit per test: 256 megabytes input: st ...

随机推荐

  1. zabbix添加对haproxy的监控

    zabbix添加对haproxy的监控 HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy本身提供一个web页面 ...

  2. ubuntu安装jdk8

    文章连接:https://www.cnblogs.com/lighten/p/6105463.html 1.简单的安装方法 安装JDK的最简单方法应该就是使用apt-get来安装了,但是源一般是Ope ...

  3. 解决 安装或卸载软件时报错Error 1001 的问题

    卸载或安装程序时出错1001:错误1001可能发生在试图更新.修复或卸载windows os中的特定程序时.此问题通常是由于程序的先前安装损坏而引起的. 错误“1001”通常会遇到,因为程序的先前安装 ...

  4. log4net使用的两种方式

    1.首先添加log4net.dll引用(可以使用  管理NuGet程序包添加引用,也可以下载下来手动去添加引用) 2.在app.config文件中配置 3.log4net使用的2终方式 log4net ...

  5. 《Oracle DBA工作笔记:运维、数据迁移与性能调优》 PDF 下载

    一:下载途径 二:本书图样 三:本书目录 第1篇 数据库运维篇第1章 数据库安装配置1.1 安装前的准备 11.2 安装数据库软件 51.2.1 方法1:OUI安装 61.2.2 方法2:静默安装 8 ...

  6. Confluence 6 创建站点的导出文件

    希望为你的站点创建一个 XML 导出文件: 进入  > 基本配置(General Configuration) > 备份和恢复(Backup & Restore). 选择 归档到备 ...

  7. nginx官方模块之http_sub_status_module

    作用 显示nginx的连接状态,nginx客户端状态 配置语法 配置

  8. LoadRunner监控window系统各项指标详解

    一.监控系统时,需要监控的项 System 系统 Processor 处理器 Memory 内存 PhysicalDisk 磁盘 Server 服务器 二.指标详解 (一). PhysicalDisk ...

  9. AXI Traffic Generator 生成axi-lite axi4 axis 的IP

    addr.coe memory_initialization_radix = ; memory_initialization_vector = ,,,,,,,,ffffffff; ctrl.coe m ...

  10. 《剑指offer》二叉树中和为某一值的路径

    本题来自<剑指offer> 反转链表 题目: 思路: C++ Code: Python Code: 总结: