大意: 给定序列, 要求实现区间加, 询问整个序列最长的先增后减的区间.

线段树维护左右两端递增,递减,先增后减的长度即可, 要注意严格递增, 合并时要注意相等的情况, 要注意相加会爆int.

#include <iostream>
#include <random>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head const int N = 3e5+10;
int n, m;
struct _ {
ll l,r,tag;
int len,La,Lb,Lab,Ra,Rb,Rab,ab;
_ () {}
_ (int x) {
l=r=x,len=La=Lb=Lab=Ra=Rb=Rab=ab=1,tag=0;
}
void upd(ll x) {
l+=x,r+=x,tag+=x;
}
_ operator + (const _ &rhs) const {
_ ret;
ret.l=l,ret.r=rhs.r;
ret.len=len+rhs.len;
ret.La = La+(La==len&&r<rhs.l?rhs.La:0);
ret.Lb = Lb+(Lb==len&&r>rhs.l?rhs.Lb:0);
ret.Lab = Lab;
if (La==len) {
if (r<rhs.l) ret.Lab=La+rhs.Lab;
else if (r>rhs.l) ret.Lab=La+rhs.Lb;
}
else if (Lab==len&&r>rhs.l) ret.Lab=Lab+rhs.Lb;
ret.Ra = rhs.Ra+(rhs.Ra==rhs.len&&r<rhs.l?Ra:0);
ret.Rb = rhs.Rb+(rhs.Rb==rhs.len&&r>rhs.l?Rb:0);
ret.Rab = rhs.Rab;
if (rhs.Rb==rhs.len) {
if (r<rhs.l) ret.Rab=rhs.Rb+Ra;
else if (r>rhs.l) ret.Rab=rhs.Rb+Rab;
}
else if (rhs.Rab==rhs.len&&r<rhs.l) ret.Rab=rhs.Rab+Ra;
ret.ab = max(ab,rhs.ab);
if (r!=rhs.l) ret.ab=max(ret.ab,Ra+rhs.Lb);
if (r<rhs.l) ret.ab=max(ret.ab,Ra+rhs.Lab);
if (r>rhs.l) ret.ab=max(ret.ab,Rab+rhs.Lb);
ret.tag = 0;
return ret;
}
} tr[N<<2];
void build(int o, int l, int r) {
if (l==r) tr[o]=_(rd());
else build(ls),build(rs),tr[o]=tr[lc]+tr[rc];
}
void pd(int o) {
if (tr[o].tag) {
tr[lc].upd(tr[o].tag);
tr[rc].upd(tr[o].tag);
tr[o].tag=0;
}
}
void update(int o, int l, int r, int ql, int qr, int v) {
if (ql<=l&&r<=qr) return tr[o].upd(v);
pd(o);
if (mid>=ql) update(ls,ql,qr,v);
if (mid<qr) update(rs,ql,qr,v);
tr[o]=tr[lc]+tr[rc];
}
int main() {
build(1,1,n=rd());
m=rd();
REP(i,1,m) {
int l=rd(),r=rd(),d=rd();
update(1,1,n,l,r,d);
printf("%d\n", tr[1].ab);
}
}

Alyona and towers CodeForces - 739C (线段树)的更多相关文章

  1. B - Alyona and towers CodeForces - 739C

    链接: https://vjudge.net/contest/202699#problem/B 题意: 给出一个序列,要支持区间加和操作 求其中最长的区间,该区间内的元素满足(ai<ai+1&l ...

  2. Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论

    Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...

  3. Alyona and a tree CodeForces - 739B (线段树合并)

    大意: 给定有根树, 每个点$x$有权值$a_x$, 对于每个点$x$, 求出$x$子树内所有点$y$, 需要满足$dist(x,y)<=a_y$. 刚开始想错了, 直接打线段树合并了..... ...

  4. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 831E) - 线段树 - 树状数组

    Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this int ...

  5. Codeforces 938G 线段树分治 线性基 可撤销并查集

    Codeforces 938G Shortest Path Queries 一张连通图,三种操作 1.给x和y之间加上边权为d的边,保证不会产生重边 2.删除x和y之间的边,保证此边之前存在 3.询问 ...

  6. codeforces 1136E 线段树

    codeforces 1136E: 题意:给你一个长度为n的序列a和长度为n-1的序列k,序列a在任何时候都满足如下性质,a[i+1]>=ai+ki,如果更新后a[i+1]<ai+ki了, ...

  7. Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset

    Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...

  8. D - The Bakery CodeForces - 834D 线段树优化dp···

    D - The Bakery CodeForces - 834D 这个题目好难啊,我理解了好久,都没有怎么理解好, 这种线段树优化dp,感觉还是很难的. 直接说思路吧,说不清楚就看代码吧. 这个题目转 ...

  9. B - Legacy CodeForces - 787D 线段树优化建图+dij最短路 基本套路

    B - Legacy CodeForces - 787D 这个题目开始看过去还是很简单的,就是一个最短路,但是这个最短路的建图没有那么简单,因为直接的普通建图边太多了,肯定会超时的,所以要用线段树来优 ...

随机推荐

  1. Day2-Python基础2---字典操作

    一.字典操作 字典一种key - value 的数据类型,使用就像我们上学用的字典,通过笔划.字母来查对应页的详细内容. 语法: 1.基本语法 >>> info = { 'stu11 ...

  2. Nunit 2.6 无法调试.Net Framework 4.0

    <configuration> <!-- The GUI only runs under .NET 2.0 or higher. The useLegacyV2RuntimeActi ...

  3. [转] 张凌 ARM体系架构

    很多时候我们都会对M0,M0+,M3,M4,M7,arm7,arm9,CORTEX-A系列,或者说AVR,51,PIC等,一头雾水,只知道是架构,不知道具体是什么,有哪些不同?今天查了些资料,来解解惑 ...

  4. mina写入数据的过程

    mina架构图  写数据.读数据触发点: 写数据: 1.写操作很简单,是调用session的write方法,进行写数据的,写数据的最终结果保存在一个缓存队列里面,等待发送,并把当前session放入f ...

  5. 转:MySQL Row Format(MySQL行格式详解)

    MySQL Row Format(MySQL行格式详解) --转载自登博的博客

  6. Maven 创建动态web 3.0项目

    使用eclipse 创建Maven 项目时候 默认是2.3的,需要一些小技巧把他转换成3.0项目 操作步骤如下分四步, 1.创建一个simple maven project 2. 转换成web3.0项 ...

  7. linux yum 脚本实现

    yum 位于linux /usr/bin/yum yum命令是python脚本进行编写的(python 2.6) #!/usr/bin/python2.6 import sys try: import ...

  8. leetcode482

    这道题主要使用了C++的几个API,大小写转化,字符串替换.其余的逻辑都比较简单.而且经查资料,string类字符串拼接的速度使用+=的速度是很快的.以下代码,也是用的+=来拼接字符串. string ...

  9. 回调函数(callback)经典解答

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:常溪玲链接:http://www.zhihu.com/question/19801131/answer/13005983来源: ...

  10. DAY12-前端之CSS

    CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素. 当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染). CSS语法 CSS实例 ...