题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308
LCIS
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6386 Accepted Submission(s): 2772
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<=105).
The next line has n integers(0<=val<=105).
The next m lines each has an operation:
U A B(0<=A,n , 0<=B=105)
OR
Q A B(0<=A<=B< n).
Each case starts with two integers n , m(0<n,m<=105).
The next line has n integers(0<=val<=105).
The next m lines each has an operation:
U A B(0<=A,n , 0<=B=105)
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
HDOJ Monthly Contest – 2010.02.06
Recommend
wxl
思路:LIS的变形,多了一个约束条件,就是要保证连续。由于是区间问题,所以很容易想到用线段树来维护信息。维护的信息有三个,一个是当前区间的LCIS,一个是该区间左端点开始向右的LCIS,还剩一个是该区间右端点开始向左的LCIS。这样,就只需要考虑区间合并的问题了。如果父节点的左儿子节点的右端点的值小于右儿子节点的左端点的值,则表示在衔接处满足LCIS的条件,即可将它们合并成一段LCIS,而且只有这一段可能会影响当前节点的LCIS,这时只需要比较更新一下当前节点的LCIS即可。另外在查询时,进行区间剖分前应检查剖分处的LCIS的大小。详见代码。
附上AC代码:
思路:LIS的变形,多了一个约束条件,就是要保证连续。由于是区间问题,所以很容易想到用线段树来维护信息。维护的信息有三个,一个是当前区间的LCIS,一个是该区间左端点开始向右的LCIS,还剩一个是该区间右端点开始向左的LCIS。这样,就只需要考虑区间合并的问题了。如果父节点的左儿子节点的右端点的值小于右儿子节点的左端点的值,则表示在衔接处满足LCIS的条件,即可将它们合并成一段LCIS,而且只有这一段可能会影响当前节点的LCIS,这时只需要比较更新一下当前节点的LCIS即可。另外在查询时,进行区间剖分前应检查剖分处的LCIS的大小。详见代码。
附上AC代码:
#include <bits/stdc++.h>
#define lrt rt<<1
#define rrt rt<<1|1
#define lson l, m, lrt
#define rson m+1, r, rrt
using namespace std;
const int maxn = 100005;
int llis[maxn<<2], rlis[maxn<<2];
int lcis[maxn<<2];
int num[maxn];
int n, q;
char op[5];
void push_up(int l, int r, int rt){
llis[rt] = llis[lrt];
rlis[rt] = rlis[rrt];
lcis[rt] = max(lcis[lrt], lcis[rrt]);
int m = (l+r)>>1;
if (num[m] < num[m+1]){
lcis[rt] = max(lcis[rt], rlis[lrt]+llis[rrt]);
if (llis[rt] == m-l+1)
llis[rt] += llis[rrt];
if (rlis[rt] == r-m)
rlis[rt] += rlis[lrt];
}
}
void build(int l, int r, int rt){
if (l == r){
llis[rt] = rlis[rt] = lcis[rt] = 1;
scanf("%d", num+l);
return ;
}
int m = (l+r)>>1;
build(lson);
build(rson);
push_up(l, r, rt);
}
void modify(int p, int val, int l, int r, int rt){
if (l == r){
num[l] = val;
return ;
}
int m = (l+r)>>1;
if (p <= m)
modify(p, val, lson);
else
modify(p, val, rson);
push_up(l, r, rt);
}
int query(int ql, int qr, int l, int r, int rt){
if (ql<=l && r<=qr)
return lcis[rt];
int m = (l+r)>>1;
int maxr = 0;
if (num[m] < num[m+1])
maxr = min(qr, m+llis[rrt])-max(ql, m+1-rlis[lrt])+1;
if (ql <= m)
maxr = max(maxr, query(ql, qr, lson));
if (qr > m)
maxr = max(maxr, query(ql, qr, rson));
return maxr;
}
int main(){
int T;
scanf("%d", &T);
while (T--){
scanf("%d%d", &n, &q);
build(0, n-1, 1);
int a, b;
while (q--){
scanf("%s%d%d", op, &a, &b);
if (op[0] == 'U')
modify(a, b, 0, n-1, 1);
else
printf("%d\n", query(a, b, 0, n-1, 1));
}
}
return 0;
}