LCIS
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5890 Accepted Submission(s): 2555
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
Author
shǎ崽
Source
线段树区间合并
和Hotel那道题有些相似,基本做法是一致的。
题目大意:
一串数字,可以单点更新,可以查询某一区间内的最长单调递增字串(是子串,而不是子序列;字串是连续的,子序列不需要连续)的长度。
注意题目的区间索引是从0开始的。
#include <stdio.h>
#include <algorithm>
using namespace std;
const int MAXN=100005;
int num[MAXN];
int left[MAXN<<2],right[MAXN<<2],middle[MAXN<<2];
void pushUp(int x,int l,int r)
{
left[x]=left[x<<1];
right[x]=right[x<<1|1];
middle[x]=max(middle[x<<1],middle[x<<1|1]);
int center=(l+r)/2;
int length=r-l+1;
if(num[center]<num[center+1])
{
if(left[x]==length-length/2)
left[x]+=left[x<<1|1];
if(right[x]==length/2)
right[x]+=right[x<<1];
middle[x]=max(right[x<<1]+left[x<<1|1],middle[x]);
}
}
void build(int x,int l,int r)
{
if(l==r)
{
left[x]=right[x]=middle[x]=1;
return;
}
int center=(l+r)/2;
build(x<<1,l,center);
build(x<<1|1,center+1,r);
pushUp(x,l,r);
}
void update(int x,int var,int l,int r,int p)
{
if(l==r)
{
num[l]=var;
return;
}
int center=(l+r)/2;
if(p<=center)
update(x<<1,var,l,center,p);
else
update(x<<1|1,var,center+1,r,p);
pushUp(x,l,r);
}
int query(int x,int l,int r,int L,int R)
{
if(L<=l && r<=R)
return middle[x];
int center=(l+r)/2;
if(R<=center)
return query(x<<1,l,center,L,R);
else if(center<L)
return query(x<<1|1,center+1,r,L,R);
else
{
int temp = 0;
if(num[center] < num[center+1])
temp = min(right[x<<1],center-L+1) + min(left[x<<1|1],R - center);
int t1 = query(x<<1,l,center,L,R);
int t2 = query(x<<1|1,center+1,r,L,R);
return max(temp,max(t1,t2));
}
}
int main()
{
int t,n,m,a,b;
char option[5];
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
build(1,1,n);
while(m--)
{
scanf("%s",option);
scanf("%d%d",&a,&b);
if(option[0]=='U')
update(1,b,1,n,a+1);
else
printf("%d\n",query(1,1,n,a+1,b+1));
}
}
return 0;
}