LCIS
Given n integers.
You have two operations:
U A B: replace the Ath number by B. (index counting from 0)
Q A B: output the length of the longest consecutive increasing subsequence (LCIS) in [a, b].
Input
T in the first line, indicating the case number.
Each case starts with two integers n , m(0<n,m<=10 5).
The next line has n integers(0<=val<=10 5).
The next m lines each has an operation:
U A B(0<=A,n , 0<=B=10 5)
OR
Q A B(0<=A<=B< n).
Output
For each Q, output the answer.
Sample Input
1
10 10
7 7 3 3 5 9 9 8 1 8
Q 6 6
U 3 4
Q 0 1
Q 0 5
Q 4 7
Q 3 5
Q 0 2
Q 4 6
U 6 10
Q 0 9
Sample Output
1
1
4
2
3
1
2
5
题意:
给序列,
操作1:单点修改
操作2:求区间内最大连续递增序列的长度
分析:
线段树维护:
1.左端点开始的连续递增序列长度
2.右端点结尾的连续递增序列长度
3.区间最大连续递增序列长度(不一定在左右端点)
关键在pushup和查询
代码自带少许注释
code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#include<stack>
#define ll long long
const int inf=0x3f3f3f3f;
const int inn=-(1<<30);
using namespace std;
const int maxm=1e5+5;
int L[maxm<<2],R[maxm<<2],xx[maxm<<2],a[maxm];
void pushup(int node,int l,int r){//关键
int mid=(l+r)/2;
L[node]=L[node*2];
R[node]=R[node*2+1];
xx[node]=max(xx[node*2],xx[node*2+1]);
if(a[mid]<a[mid+1]){//如果左子树的右端和右子树的左端是递增的就继续更新
if(L[node*2]==mid-l+1){
L[node]+=L[node*2+1];
}
if(R[node*2+1]==r-(mid+1)+1){
R[node]+=R[node*2];
}
xx[node]=max(xx[node],R[node*2]+L[node*2+1]);
}
}
void build(int l,int r,int node){
if(l==r){
L[node]=R[node]=xx[node]=1;//因为是长度所以初始为1
return ;
}
int mid=(l+r)/2;
build(l,mid,node*2);
build(mid+1,r,node*2+1);
pushup(node,l,r);
}
void update(int x,int val,int l,int r,int node){
if(l==r){
a[x]=val;//这里改的是a[x],容易写成a[node]
return ;
}
int mid=(l+r)/2;
if(x<=mid){
update(x,val,l,mid,node*2);
}else{
update(x,val,mid+1,r,node*2+1);
}
pushup(node,l,r);
}
int ask(int st,int ed,int l,int r,int node){
if(st<=l&&ed>=r){//1.全包含
return xx[node];
}
int mid=(l+r)/2;
int ans=1;
if(st<=mid){//左边部分
ans=max(ans,ask(st,ed,l,mid,node*2));
}
if(ed>=mid+1){//右边部分
ans=max(ans,ask(st,ed,mid+1,r,node*2+1));
}
if(st<=mid&&ed>=mid&&a[mid]<a[mid+1]){//横跨左右
ans=max(ans,min(mid-st+1,R[node*2])+min(ed-(mid+1)+1,L[node*2+1]));
}
return ans;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){//必须先输入再建树,否则pushup会出错
scanf("%d",&a[i]);
}
build(1,n,1);
while(m--){
char s[5];
int x,y;
scanf("%s%d%d",s,&x,&y);
if(s[0]=='U'){
x++;
update(x,y,1,n,1);
}else{
x++,y++;
printf("%d\n",ask(x,y,1,n,1));
}
}
}
return 0;
}