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

线段树维护左右两端递增,递减,先增后减的长度即可, 要注意严格递增, 合并时要注意相等的情况, 要注意相加会爆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. think python chapter3

    3.1 built-in function type(42)=> <class 'int'> int('32')=>32 int(3.9) => 3 int(-2.3)= ...

  2. EMC (电磁兼容性)

    电磁兼容性EMC(Electro Magnetic Compatibility),是指设备或系统在其电磁环境中符合要求运行并不对其环境中的任何设备产生无法忍受的电磁干扰的能力.因此,EMC包括两个方面 ...

  3. Day2-VIM(一):移动

    基础 字符移动 k 上移 k h 左移 h l l 右移 j j 下移 你也可以使用键盘上的方向键来移动,但这么做h j k l的存在就失去了意义 之所以使用h j k l来控制方向,其主要目的是让你 ...

  4. java代码,用continue写出偶数

    总结:不满足条件时,输出当前==== package com.b; import java.util.Scanner; //用continue写出偶数的代码是: public class twe { ...

  5. java中i/o练习

    总结: FileInputStream fis; int length; while((length=fis.read(b,0,b.length))!=-1){ output.write(b,0,le ...

  6. filter中获取spring bean

    import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import ja ...

  7. 环境搭建:Vue环境搭建和项目初始化(windows)

    1.    安装node.js 官网下载安装:https://nodejs.org/en/ 版本查看:node -v 注意:node版本最好新一点好,推荐6.0以上. 2.    npm安装webpa ...

  8. php字符型转整型

    $arr = array(0=>1,"aa"=>2, 3, 4); foreach($arr as $key=>$val){ print($key == &quo ...

  9. leetcode479

    public class Solution { public int LargestPalindrome(int n) { ) ; , n) - ; ; v > max / ; v--) { S ...

  10. docker 笔记 (6)搭建本地registry

    转:http://blog.csdn.net/felix_yujing/article/details/51564739 新版 registry v2对镜像存储格式进行了重新设计,并且和旧版还不兼容. ...