Alyona and towers CodeForces - 739C (线段树)
大意: 给定序列, 要求实现区间加, 询问整个序列最长的先增后减的区间.
线段树维护左右两端递增,递减,先增后减的长度即可, 要注意严格递增, 合并时要注意相等的情况, 要注意相加会爆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 (线段树)的更多相关文章
- B - Alyona and towers CodeForces - 739C
链接: https://vjudge.net/contest/202699#problem/B 题意: 给出一个序列,要支持区间加和操作 求其中最长的区间,该区间内的元素满足(ai<ai+1&l ...
- Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论
Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...
- Alyona and a tree CodeForces - 739B (线段树合并)
大意: 给定有根树, 每个点$x$有权值$a_x$, 对于每个点$x$, 求出$x$子树内所有点$y$, 需要满足$dist(x,y)<=a_y$. 刚开始想错了, 直接打线段树合并了..... ...
- 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 ...
- Codeforces 938G 线段树分治 线性基 可撤销并查集
Codeforces 938G Shortest Path Queries 一张连通图,三种操作 1.给x和y之间加上边权为d的边,保证不会产生重边 2.删除x和y之间的边,保证此边之前存在 3.询问 ...
- codeforces 1136E 线段树
codeforces 1136E: 题意:给你一个长度为n的序列a和长度为n-1的序列k,序列a在任何时候都满足如下性质,a[i+1]>=ai+ki,如果更新后a[i+1]<ai+ki了, ...
- Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset
Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...
- D - The Bakery CodeForces - 834D 线段树优化dp···
D - The Bakery CodeForces - 834D 这个题目好难啊,我理解了好久,都没有怎么理解好, 这种线段树优化dp,感觉还是很难的. 直接说思路吧,说不清楚就看代码吧. 这个题目转 ...
- B - Legacy CodeForces - 787D 线段树优化建图+dij最短路 基本套路
B - Legacy CodeForces - 787D 这个题目开始看过去还是很简单的,就是一个最短路,但是这个最短路的建图没有那么简单,因为直接的普通建图边太多了,肯定会超时的,所以要用线段树来优 ...
随机推荐
- python3 之logging模块
logging.getLogger(name=None)Return a logger with the specified name or, if name is None, return a lo ...
- 使用Azure Site Recovery把VM批量搬迁到Azure
Azure Site Recovery可以提供如下服务: Site Recovery 服务:Site Recovery 可以在站点出现故障时,让应用在其他站点继续可用,从而确保业务连续性. Site ...
- java代码异常,水位异常的捕获
总: 异常的产生!!!!异常如何实现,继承~~~ package com.b; //我不懂为什么这里的异常一定要来自于父类.子类.去继承它.因为Exception是Throwable类的子类异常类.而 ...
- DCloud-5+Runtime:杂项
ylbtech-DCloud-5+Runtime:杂项 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 7.返回顶部 8.返回顶部 ...
- CountDownLatch的介绍和使用
1.类介绍 java.util.concurrent 类 CountDownLatch java.lang.Object java.util.concurrent.CountDownLatch 一个同 ...
- 基于OpenCV的火焰检测(三)——HSI颜色判据
上文向大家介绍了如何用最简单的RGB判据来初步提取火焰区域,现在我要给大家分享的是一种更加直观的判据--HSI判据. 为什么说HSI判据是更加直观的判据呢?老规矩,先介绍一下HSI色彩模型: HSI颜 ...
- 反射+属性标签 通用Excel导入导
在做通用导入导出的时候,最关键的应该就是实体导出导入的顺序了,但是编译器在编译的时候又无法自定义属性编译的顺序,所以需要一个自定义的特性标签来指定实体类导出的顺序,然后通过自定义的比较器将属性排序 因 ...
- 部署和调优 2.4 tomcat安装
下载tamcet 官网 http://tomcat.apache.org/ 左侧选择版本 复制下载链接 切换到下载目录 cd /usr/local/src linux wget wget http:/ ...
- DAY7-面向对象之绑定方法与非绑定方法
一.类中定义的函数分成两大类 一:绑定方法(绑定给谁,谁来调用就自动将它本身当作第一个参数传入): 1. 绑定到类的方法:用classmethod装饰器装饰的方法. 为类量身定制 类.boud_met ...
- for xml path 按分类合并行数据
) as itemnum FROM ( SELECT Sonum, (SELECT ItemNum+',' FROM testtb WHERE Sonum=A.Sonum FOR XML ...