线段树加一点dp
这里维护一个每个区间的{fmax,lmax,rmax},分别代表最长递增序列长度,以左端点开始的递增区间长度,以右端点结尾的递增序列长度。
当前根的fmax=max{left.fmax, right.fmax, left.rmax+right.lmax}
当然,第三项需要左右区间的大小关系满足才行。
当前根的lmax=left.lmax+rmax.lmax,此处的第二项需要满足left.lmax等于左区间长度并且大小关系满足。
接下来写好线段树就ok了。
代码如下所示:
#include <iostream>
#include <algorithm>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
typedef long long ll;
const int MAXN=1e5+1;
int w[MAXN];
struct node{
int l, r;
int lmax,rmax,fmax;
}tree[MAXN<<2];
void push_up(node &rt, const node& l_rt, const node& r_rt){
const int llen = l_rt.r-l_rt.l+1, rlen = r_rt.r-r_rt.l+1;
const int LR = w[l_rt.r], RL = w[r_rt.l];// 区间值
rt.fmax = max({l_rt.fmax, r_rt.fmax, RL>LR ? l_rt.rmax+r_rt.lmax : 0});
rt.lmax = l_rt.lmax + (l_rt.lmax==llen && RL>LR ? r_rt.lmax : 0);// 只有满足大小关系才有机会扩大序列大小
rt.rmax = r_rt.rmax + (r_rt.rmax==rlen && RL>LR ? l_rt.rmax : 0);
}
void push_up(int rt){
push_up(tree[rt], tree[rt<<1], tree[rt<<1|1]);
}
void build(int l,int r,int rt){
tree[rt] = {l,r,1,1,1};
if (l>=r){
return;
}
int mid = (l+r)>>1;
build(lson);
build(rson);
push_up(rt);
}
void update(int a,int val,int l,int r,int rt){
if (tree[rt].l==tree[rt].r){
w[a]=val;
return;
}
int mid = (l+r)>>1;
if (a<=mid){
update(a,val,lson);
}else{
update(a,val,rson);
}
push_up(rt);
}
node query(int ul,int ur,int l,int r,int rt){
if (ul<=l && r<=ur) return tree[rt];
int mid = (l+r)>>1;
if (ur<=mid) return query(ul,ur,lson);
if (ul>mid) return query(ul,ur,rson);
node left = query(ul,ur,lson);
node right = query(ul,ur,rson);
node res={left.l,right.r,0,0,0};
push_up(res, left, right);
return res;
}
int main(){
int kase;
cin>>kase;
while (kase--){
int n,m,a,b;
char op[2];
cin>>n>>m;
for (int i = 1; i <= n; ++i) {
scanf("%d", &w[i]);
}
build(1,n,1);
for (int i = 0; i < m; ++i) {
scanf("%s", op);
scanf("%d %d", &a,&b);
// cout<<op<<","<<a<<","<<b<<endl;
if (op[0]=='U'){
update(++a,b,1,n,1);
} else {
a++,b++;
printf("%d\n", query(a,b,1,n,1).fmax);
}
}
}
return 0;
}