有条件的区间合并。
另设一个数组储存数据。
判断条件成立后才进行区间合并。
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int Max=100000+100;
int date[Max<<2],rmaxn[Max<<2],lmaxn[Max<<2],maxn[Max<<2];
void pushup(int l,int r,int rt)
{
int m=(l+r)>>1;
lmaxn[rt]=lmaxn[rt<<1];
rmaxn[rt]=rmaxn[rt<<1|1];
maxn[rt]=max(maxn[rt<<1],maxn[rt<<1|1]);
if(date[m]<date[m+1]) //能否合并的前提条件。
{
if(lmaxn[rt]==m-l+1) lmaxn[rt]+=lmaxn[rt<<1|1]; //区间合并的三条语句。
if(rmaxn[rt]==r-m) rmaxn[rt]+=rmaxn[rt<<1];
maxn[rt]=max(maxn[rt],lmaxn[rt<<1|1]+rmaxn[rt<<1]);
}
}
void build(int l,int r,int rt)
{
if(l==r)
{
scanf("%d",&date[l]);
maxn[rt]=rmaxn[rt]=lmaxn[rt]=1;
return ;
}
int m=(l+r)>>1;
build(lson);
build(rson);
pushup(l,r,rt);
}
void update(int a,int b,int l,int r,int rt)
{
if(l==r)
{
date[l]=b;
return ;
}
int m=(l+r)>>1;
if(a<=m) update(a,b,lson);
if(a>m) update(a,b,rson);
pushup(l,r,rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(L==l&&r==R)
return maxn[rt];
int m=(l+r)>>1;
if(R<=m) return query(L,R,lson);
if(L>m) return query(L,R,rson);
int ans=max(query(L,m,lson),query(m+1,R,rson));
int ret=0;
if(date[m]<date[m+1]) //判断能否合并。
ret=min(lmaxn[rt<<1|1],R-m)+min(rmaxn[rt<<1],m-L+1); //合并Ing.
return max(ans,ret);
}
int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
int n,m;
scanf("%d %d",&n,&m);
n--;
build(0,n,1);
while(m--)
{
char ch[2];
int a,b;
scanf("%s %d %d",ch,&a,&b);
if(ch[0]=='U')
update(a,b,0,n,1);
else
printf("%d\n",query(a,b,0,n,1));
}
}
return 0;
}