B - Alyona and towers CodeForces - 739C
链接:
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的更多相关文章
- Alyona and towers CodeForces - 739C (线段树)
大意: 给定序列, 要求实现区间加, 询问整个序列最长的先增后减的区间. 线段树维护左右两端递增,递减,先增后减的长度即可, 要注意严格递增, 合并时要注意相等的情况, 要注意相加会爆int. #in ...
- Codeforces 739C Alyona and towers 线段树
Alyona and towers 这个题写起来真的要人命... 我们发现一个区间被加上一个d的时候, 内部的结构是不变的, 改变的只是左端点右端点的值, 这样就能区间合并了. 如果用差分的话会简单一 ...
- Codeforces 739C - Alyona and towers(线段树)
Codeforces 题目传送门 & 洛谷题目传送门 可能有人会问我为什么为这道 *2500 的 D1C 写题解,我觉得大概是想要在写题解数量上 dd ycx 吧,因为 ycx 到目前为止写了 ...
- Towers CodeForces - 229D
The city of D consists of n towers, built consecutively on a straight line. The height of the tower ...
- C Alyona and Spreadsheet Codeforces Round #401(Div. 2)(思维)
Alyona and Spreadsheet 这就是一道思维的题,谈不上算法什么的,但我当时就是不会,直到别人告诉了我,我才懂了的.唉 为什么总是这么弱呢? [题目链接]Alyona and Spre ...
- CODEFORCES ROUND #740 ANALYSES BY TEAM:RED & BLACK
A.Alyona and copybooks Problems: 给你一个数n和代价分别为a, b, c.数量不限的1, 2, 3,求将n凑成4的倍数的最小代价 Analysis: cj:取个模随便凑 ...
- codeforces740B
Alyona and flowers CodeForces - 740B Little Alyona is celebrating Happy Birthday! Her mother has an ...
- Codeforces 740C. Alyona and mex 思路模拟
C. Alyona and mex time limit per test: 2 seconds memory limit per test: 256 megabytes input: standar ...
- Codeforces 740A. Alyona and copybooks 模拟
A. Alyona and copybooks time limit per test: 1 second memory limit per test: 256 megabytes input: st ...
随机推荐
- boost 文件系统
第 9 章 文件系统 目录 9.1 概述 9.2 路径 9.3 文件与目录 9.4 文件流 9.5 练习 该书采用 Creative Commons License 授权 9.1. 概述 库 Boo ...
- 使用sudo而无需输入密码的设置
在linux上,root用户是老大,什么事都能做.但是,很多时候由于安全等各种原因,我们不希望把root用户开放给大家,但是又希望其他的用户可以有root的权限,所以就有了sudo用户.而执行sudo ...
- Gitbook
2017年9月13日 17:12:20 星期三 gitbook 可以将markdown格式的文件编译成html格式 放在当前目录里的_book目录里(需要手动创建, 也可以指定编译后的html文件放到 ...
- mariadb:SQL日常使用总结
1.关联删除 DELETE T_Base_Resource_Action FROM T_Base_Resource_Action INNER JOIN T_Base_Resource ON T_Ba ...
- Fragment与Acitvity通信
Fragment与Activity通信的方式如下: 一.通过初始化函数提供 1.在动态添加Fragment的过程中,我们在Activity中通过Fragment.setArguments()的方法为F ...
- 前端 --- 关于DOM的介绍
111 什么是DOM DOM:文档对象模型.DOM 为文档提供了结构化表示,并定义了如何通过脚本来访问文档结构.目的其实就是为了能让js操作html元素而制定的一个规范. DOM就是由节点组成的. 解 ...
- Netflix正式开源其API网关Zuul 2
5 月 21 日,Netflix 在其官方博客上宣布正式开源微服务网关组件 Zuul 2.Netflix 公司是微服务界的楷模,他们有大规模生产级微服务的成功应用案例,也开源了相当多的微服务组件(详见 ...
- Python学习-字符编码浅析
1.什么是字符编码 既然是简述那肯定是简单明了.字符编码,看名字就是一种字符的编码格式,由于计算机内部采用二进制,想要将人类的语言字符输入到计算机就需要一种编码格式,这就是字符编码.字符------- ...
- 金九银十中,看看这31道Android面试题
阅读目录 1.如何对 Android 应用进行性能分析 2.什么情况下会导致内存泄露 3.如何避免 OOM 异常 4.Android 中如何捕获未捕获的异常 5.ANR 是什么?怎样避免和解决 ANR ...
- linux 下安装vscode
下载安装包 https://code.visualstudio.com/docs/?dv=linux64_deb (注意是deb包) sudo dpkg -i code_1.18.1-15108573 ...