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 ...
随机推荐
- python多线程并发
# coding=utf8 # 使用前需安装net-snmp-utils或net-snmp包 from _utils.patrol2 import run_cmd import sys import ...
- SharePoint 2010 安装错误:请重新启动计算机,然后运行安装程序以继续
一.环境:Windows Server 2008 R2 with sp1,SharePoint 2010 二.问题描述: 正常的安装SharePoint 2010 ,安装完必备组件,并提示所有必备组件 ...
- jquery获取当前按钮、截取字符串、字符串拼接、动态循环添加元素
截取字符串:字符串拼接:动态循环添加元素:获取当前按钮: {data : null, render: function(data, type, row ) { var loginName = $(&q ...
- sqlserver 2012 分页
--2012的OFFSET分页方式 select number from spt_values where type='p' order by number offset 10 rows fetch ...
- mysql 5.6 windows 启动脚本
2018-4-25 17:02:08 星期三 下载mysql 5.6 zip(免安装版)到本机 一台电脑上可能装有多个版本的mysql, 启动时为了不影响: 1. 解压后文件夹根目录改名为 mysql ...
- freeswitch反注册记录
应用情景: 使用阿里服务器,落地使用本地的模拟线路(O口网关). 1.FreeSWITCH 服务器开一个账号,比如 5000 internal , O口 SIP设置页面按照网关注册 5000 的账号信 ...
- 13)django-ORM(连表一对多,外键创建,创建数据,3种查询)
一对多需要使用外键 一:外键创建ForeignKey b=models.ForeignKey(to="Business",to_field=("id"))#dj ...
- python-元类的几种单例模式
单例介绍: 单例即单个的实例,指的是同一个类实例化多次的结果都是指向同一个对象,用于节省内存空间 如果我们从配置文件中读取配置信息来进行实例化,在配置相同的情况下,就没必要重复产生对象浪费内存了. # ...
- day14 迭代器 生成器 面向过程思想
"" 迭代器 什么是迭代器(iterator) 器指的某种工具, 迭代指的是更新换代的过程,例如应用程序的版本更新从1.0 变成 1.1 再1.2 迭代的目的是要根据上一个结果,产 ...
- AngularJs中,如何在ng-repeat完成之后,执行Js脚本
//ng-repeat生成4个li,生成后再执行自定义方法fn在每个li后加一根横线 <script> var myapp=angular.module('myapp',[]); myap ...