链接:https://www.nowcoder.com/acm/contest/158/B
来源:牛客网
最长区间
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
给你一个长度为 n 的序列 a ,求最长的连续的严格上升区间的长度。
同时会进行 m 次修改,给定 x , y ,表示将 ax 修改为 y ,每次修改之后都要求输出答案。
第一行 2 个数 n,m,表示序列长度,修改次数; 接下来一行 n 个数表示 ; 接下来 m 行,每行 2 个数 x , y ,描述一次修改。
第一行 1 个数表示最初的答案; 接下来 m 行,第 i 行 1 个数表示第 i 次修改后的答案。
示例1
复制
4 3 1 2 3 4 3 1 2 5 3 7
复制
4 2 2 3
序列变换如下: 1 2 3 4 1 2 1 4 1 5 1 4 1 5 7 4
n,m ≤ 100000,1 ≤ x ≤ n,1 ≤ ai,y ≤ 100
思路:本题的y值域很小,大可暴力。但是为了更加熟悉线段树,就写(chao)了个线段树。只需要标记左右区间是否全为上升序列即可。
代码:
#include
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define maxn 100010
int lsum[maxn<<2];//lsum表示区间左起最长连续上升序列的长度
int rsum[maxn<<2];//rsum表示区间右起最长连续上升序列的长度
int msum[maxn<<2];//msum表示区间最长连续上升序列的长度
int num[maxn];
void pushup(int l,int r,int rt)//区间合并
{
int m=(l+r)>>1;
lsum[rt]=lsum[rt<<1];
rsum[rt]=rsum[rt<<1|1];
msum[rt]=max(msum[rt<<1],msum[rt<<1|1]);
if(num[m]>1;
build(lson);
build(rson);
pushup(l,r,rt);
}
void updata(int p,int c,int l,int r,int rt)//单点更新
{
if(l==r)
{
num[l]=c;
return ;
}
int m=(l+r)>>1;
if(m>=p)
updata(p,c,lson);
else updata(p,c,rson);
pushup(l,r,rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(l>=L&&R>=r)
return msum[rt];
int ret=0;
int m=(l+r)>>1;
if(m>=L) ret=max(ret,query(L,R,lson));
if(R>m) ret=max(ret,query(L,R,rson));
if(num[m]