LCIS
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5863 Accepted Submission(s): 2545
Problem Description
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].
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).
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
用pre,suf,len存放一段区间上最大连续的前缀长度,最大连续后缀长度,最大连续长度。然后就是合并区间了,在查询和合并区间的时候需要注意不能忽略掉最大长度的子串覆盖区间中点的情况。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
struct tree
{
int l,r,len,pre,suf;
};
tree d[400005];
int T,n,m,a[100005],A,B;
char s[5];
void buildtree(int i,int l,int r)
{
int mid=(l+r)/2;
d[i].l=l; d[i].r=r; d[i].len=0; d[i].pre=0; d[i].suf=0;
if (l==r)
{
d[i].pre=1; d[i].suf=1; d[i].len=1;
return;
}
buildtree(i*2,l,(r+l)/2);
buildtree(i*2+1,(l+r)/2+1,r);
d[i].len=max(d[i*2].len,d[i*2+1].len);
if (a[mid]<a[mid+1]) d[i].len=max(d[i*2].suf+d[i*2+1].pre,d[i].len);
if (a[mid]<a[mid+1] && d[i*2].pre==mid-l+1) d[i].pre=d[i*2].pre+d[i*2+1].pre;
else d[i].pre=d[i*2].pre;
if (a[mid]<a[mid+1] && d[i*2+1].suf==r-mid) d[i].suf=d[i*2].suf+d[i*2+1].suf;
else d[i].suf=d[i*2+1].suf;
}
int _count(int i,int l,int r)
{
int mid=(d[i].l+d[i].r)/2;
if (d[i].l==l && d[i].r==r) return d[i].len;
else if (r<=mid) return _count(i*2,l,r);
else if (l>mid) return _count(i*2+1,l,r);
else
{
int x,y,len=0;
x=_count(i*2,l,mid);
y=_count(i*2+1,mid+1,r);
if (a[mid]<a[mid+1]) len=min(d[i*2].suf,mid-l+1)+min(d[i*2+1].pre,r-mid);
len=max(len,max(x,y));
return len;
}
}
void _insert(int i,int l,int k)
{
int mid=(d[i].l+d[i].r)/2;
if (d[i].l==d[i].r && d[i].l==l)
{
a[l]=k;
return ;
}
else if (l<=mid) _insert(i*2,l,k);
else _insert(i*2+1,l,k);
d[i].len=max(d[i*2].len,d[i*2+1].len);
if (a[mid]<a[mid+1]) d[i].len=max(d[i*2].suf+d[i*2+1].pre,d[i].len);
if (a[mid]<a[mid+1] && d[i*2].pre==mid-d[i].l+1) d[i].pre=d[i*2].pre+d[i*2+1].pre;
else d[i].pre=d[i*2].pre;
if (a[mid]<a[mid+1] && d[i*2+1].suf==d[i].r-mid) d[i].suf=d[i*2].suf+d[i*2+1].suf;
else d[i].suf=d[i*2+1].suf;
}
int main()
{
scanf("%d",&T);
while (T--)
{
memset(d,0,sizeof(d));
memset(a,0,sizeof(a));
scanf("%d%d",&n,&m);
for (int i=0;i<n;i++) scanf("%d",&a[i]);
buildtree(1,0,n-1);
for (int i=1;i<=m;i++)
{
scanf("%s%d%d",s,&A,&B);
if (s[0]=='Q') printf("%d\n",_count(1,A,B));
if (s[0]=='U') _insert(1,A,B);
}
}
return 0;
}