链接:

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. oracle监控

    python代码 #!/usr/bin/env python # -*- coding: UTF-8 -*- import subprocess import sys import re def ru ...

  2. windows下django1.7 +python3.4.2搭建记录1

    python+django在linux下搭建比较简单,windows下搭建比较复杂,所以列在下方一.下载安装下载django的包,到刚解压后的Django-1.7目录下执行命令 python setu ...

  3. Nodejs脚手架搭建基于express的应用

    原文链接:https://www.cnblogs.com/FE-yanyi1993/p/6413042.html 这篇写的非常详细,此处只做记录. 1.安装生成器 $ npm install expr ...

  4. python字符串前面u,r,b的含义详解

    u/U:表示unicode字符串 不是仅仅是针对中文, 可以针对任何的字符串,代表是对字符串进行unicode编码. 一般英文字符在使用各种编码下, 基本都可以正常解析, 所以一般不带u:但是中文, ...

  5. java-pdf转word

    注:原文来至 < java-pdf转word   > 一: java Pdf 文字 转 Word 废话不说,直接上图 很简单的用法:1.new个PDFBox对象2.调用pdfToDoc() ...

  6. Guideline 5.2.1 - Legal - Intellectual Property 解决方案

    最近在上架公司公司项目的时候遇到这个问题什么5.2.1 然后去了解发现最近不少人都遇到了这个问题.先说一下 我上架的APP是一个医疗的APP然后说需要什么医疗资质,估计是账号的公司资质不够吧.后面和苹 ...

  7. ( linker command failed with exit code 1) 错误解决方案 项目使用的是pod

    targets -> build settings -> architectures -> build active architecture only -> debug 改成 ...

  8. 利用Form组件和ajax实现的注册

    一.注册相关的知识点 1.Form组件 我们一般写Form的时候都是把它写在views视图里面,那么他和我们的视图函数也不影响,我们可以吧它单另拿出来,在应用下面建一个forms.py的文件来存放 2 ...

  9. LeetCode(94):二叉树的中序遍历

    Medium! 题目描述: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗 ...

  10. 【python】mongo删除数据

    参考:https://stackoverflow.com/questions/23334743/setting-justone-limiter-for-pymongo-remove-throws-ty ...