#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = 100010;
int tlen[maxn<<2];
int llen[maxn<<2],rlen[maxn<<2],lc[maxn<<2],rc[maxn<<2];
void push_up(int rt,int l,int r)
{
int temp=max(tlen[rt<<1],tlen[rt<<1|1]);
if(rc[rt<<1]<lc[rt<<1|1])
temp=max(temp,rlen[rt<<1]+llen[rt<<1|1]);
tlen[rt]=temp;
int m=(l+r)/2;
int lnum=m-l+1;
int rnum=r-m;
llen[rt]=llen[rt<<1];
rlen[rt]=rlen[rt<<1|1];
if(llen[rt<<1]==lnum && rc[rt<<1]<lc[rt<<1|1])
llen[rt]+=llen[rt<<1|1];
if(rlen[rt<<1|1]==rnum && lc[rt<<1|1]>rc[rt<<1])
rlen[rt]+=rlen[rt<<1];
lc[rt]=lc[rt<<1];
rc[rt]=rc[rt<<1|1];
}
void build(int l,int r,int rt)
{
if(l==r)
{
int temp;
scanf("%d",&temp);
lc[rt]=rc[rt]=temp;
tlen[rt]=llen[rt]=rlen[rt]=1;
return;
}
int m=(l+r)/2;
build(lson);
build(rson);
push_up(rt,l,r);
}
void update(int pos,int v,int l,int r,int rt)
{
if(l==r)
{
rc[rt]=lc[rt]=v;
return;
}
int m=(l+r)/2;
if(pos<=m)
update(pos,v,lson);
else
update(pos,v,rson);
push_up(rt,l,r);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return tlen[rt];
}
int m=(l+r)/2;
int ret=1;
if(L<=m)
ret=max(ret,query(L,R,lson));
if(R>m)
ret=max(ret,query(L,R,rson));
if(rc[rt<<1]<lc[rt<<1|1])//两个子区间相交部分
ret=max(ret,min(m-L+1,rlen[rt<<1])+min(R-m,llen[rt<<1|1]));
return ret;
}
int t,n,m;
char op[2];
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
build(1,n,1);
while(m--)
{
int a,b;
scanf("%s%d%d",op,&a,&b);
if(op[0]=='U')
{
a++;
update(a,b,1,n,1);
}
else
{
a++,b++;
printf("%d\n",query(a,b,1,n,1));
}
}
}
return 0;
}
hdu 3308 线段树+区间合并
最新推荐文章于 2023-09-11 23:07:10 发布