题目链接:https://vjudge.net/problem/HDU-3308
解题思路:
题目很明显的线段树,但不是那么好写。要求区间最长的连续递增长度,那么需要多考虑的就是当左右区间合并时如果右区间的左边界值大于左区间的右边界值时,会合并出一个新的连续递增段,所以我们还要维护一个区间的左右边界最长上升长度才行。
#include<bits/stdc++.h>
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
const int mx = 1e5 + 10;
int a[mx];
struct node
{
int len,ls,rs;
int sum;
}s[mx*3];
node push_up(int mid,node p1,node p2)
{
node now;
now.len = max(p1.len,p2.len);
now.ls = now.rs = 0;
now.sum = p1.sum + p2.sum;
if(a[mid+1]>a[mid]){
now.len = max(now.len,p1.rs+p2.ls);
if(p1.len==p1.sum) now.ls = p1.len + p2.ls;
if(p2.len==p2.sum) now.rs = p2.len + p1.rs;
}
now.ls = max(now.ls,p1.ls);
now.rs = max(now.rs,p2.rs);
return now;
}
void build(int l,int r,int rt)
{
if(l==r){
scanf("%d",a+l);
s[rt].len = s[rt].ls = s[rt].rs = 1;
s[rt].sum = 1;
return ;
}
int mid = (l+r)>>1;
build(lson);build(rson);
s[rt] = push_up(mid,s[rt<<1],s[rt<<1|1]);
}
void update(int l,int r,int rt,int p,int v)
{
if(l==r){
a[l] = v;
return ;
}
int mid = (l+r)>>1;
if(p<=mid) update(lson,p,v);
else update(rson,p,v);
s[rt] = push_up(mid,s[rt<<1],s[rt<<1|1]);
}
node query(int l,int r,int rt,int L,int R)
{
if(L<=l&&r<=R) return s[rt];
int mid = (l+r)>>1;
if(L<=mid&&R>mid)
return push_up(mid,query(lson,L,R),query(rson,L,R));
if(L<=mid) return query(lson,L,R);
return query(rson,L,R);
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
int n , m ;
scanf("%d%d",&n,&m);
build(1,n,1);
char c[10];
int x,y;
while(m--){
scanf("%s%d%d",c,&x,&y);
if(c[0]=='Q'){
printf("%d\n",query(1,n,1,x+1,y+1).len);
}else{
update(1,n,1,x+1,y);
}
}
}
return 0;
}