链接:

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. 高效的多维空间点索引算法 — Geohash 和 Google S2

    原文地址:https://www.jianshu.com/p/7332dcb978b2   引子 每天我们晚上加班回家,可能都会用到滴滴或者共享单车.打开 app 会看到如下的界面:     app ...

  2. slice的部分说明

    1.slice是数值的一个引用,并不会新增内存地址. 2.slice的容量和长度是两个概念,这个长度跟数组的长度是一个概念,即在内存中进行了初始化实际存在的元素的个数.何谓容量?如果通过make函数创 ...

  3. 在 mingw32 上编译 libvpx 1.7.0 时的注意事项

    in the vp8/common/theading.h Just need to add 1 line:#include <sys/types.h>before the last occ ...

  4. zabbix-3.0.4添加对windows 2008r2的监控

    zabbix-3.0.4添加对windows 2008r2的监控 一.windows客户端的配置关闭windows防火墙或者开通10050和10051端口(1).关闭防火墙(不推荐直接关闭,测试可以这 ...

  5. 修改更新源sources.list,提高软件下载安装速度(2017.04.05)

    1.切换到root用户(如果已经是root用户就直接看第二步) dnt@HackerKali:~$ su 密码: 2.用文本编辑器打开sources.list,手动添加下面的更新源 root@Hack ...

  6. mybatis:延迟加载时不要在get/set方法上面添加final关键字(原创)

    1.mybatis-config.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...

  7. 随机函数rand()和srand()

    C++中随机函数rand()和srand()的用法 一.rand() 函数名:   rand     功   能:   随机数发生器   用   法:   int rand(void); 所在头文件: ...

  8. python学习第2天

    03 pycharm使用04 格式化输出05 while循环 why: 吃饭睡觉上课, 地球绕着太阳公转,单曲循环,列表循环. what: while how: while 条件: 循环体 where ...

  9. grep匹配某个次出现的次数

    cat file | grep  -c 'xxx'  统计xxx在file中出现的行数 cat file | grep  -o 'xxx'  统计xxx在file中出现的次数

  10. Oracle Database 11g : SQL 基础

    简介 1:课程目标 2:课程 目标 3:Oracle Database 11g 以及相关产品概览 1:Oracle Database 11g :重点领域 2:Oracle Fusion Middlew ...