LCIS
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1713 Accepted Submission(s): 748
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].
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
Author
shǎ崽
Source
HDOJ Monthly Contest – 2010.02.06
Recommend
wxl
这道题能很能体现线段树的通用性,通过区间节点保存的信息,大大优化了时间
从节点里保存的很多信息可以看出来,线段树题的关键就在于储存哪些信息,和如何利用这些信息
需要缜密的思考tree[n].num,tree[n].tagl,tree[n].tagr,这些值如何更新
这道题可能很神奇的1Y,可惜我写错了一个地方,没体会到这种乐趣。。。
#include <stdio.h>
#include <algorithm>
using namespace std;
struct Node{
int l,r,num;
int first,end;
int tagl,tagr;
}tree[1000000];
void Build(int n,int x,int y){
tree[n].l = x;
tree[n].r = y;
int mid = (x + y) / 2;
if(x == y){
int a;
scanf("%d",&a);
tree[n].first = a;//区间首元素的值
tree[n].end = a;//区间尾元素。。
tree[n].num = 1;//区间内最长序列
tree[n].tagl = 1;//从左起最长上升序列
tree[n].tagr = 1;//从右起。。。。。。
return;
}
Build(2*n,x,mid);
Build(2*n+1,mid+1,y);
tree[n].first = tree[2*n].first;
tree[n].end = tree[2*n+1].end;
//如果左儿子整个区间连续,且末元素<右儿子区间首元素
if(tree[2*n].num == (tree[2*n].r - tree[2*n].l + 1) && tree[2*n].end < tree[2*n+1].first)
tree[n].tagl = tree[2*n].num + tree[2*n+1].tagl;
else
tree[n].tagl = tree[2*n].tagl;
//tagr同理
if(tree[2*n+1].num == (tree[2*n+1].r - tree[2*n+1].l + 1) && tree[2*n].end < tree[2*n+1].first)
tree[n].tagr = tree[2*n+1].num + tree[2*n].tagr;
else
tree[n].tagr = tree[2*n+1].tagr;
//求tree[n].num
if(tree[2*n].end < tree[2*n+1].first)
tree[n].num = max(tree[2*n].tagr + tree[2*n+1].tagl,max(tree[2*n].num,tree[2*n+1].num));
else
tree[n].num = max(tree[2*n].num,tree[2*n+1].num);
}
void Modify(int n,int x,int y){
int l = tree[n].l;
int r = tree[n].r;
int mid = (l + r) / 2;
if(l == r){
tree[n].first = y;
tree[n].end = y;
return;
}
if(x <= mid) Modify(2*n,x,y);
else Modify(2*n+1,x,y);
//更新节点中的所有值,和Build中一样
tree[n].first = tree[2*n].first;
tree[n].end = tree[2*n+1].end;
if(tree[2*n].num == (tree[2*n].r - tree[2*n].l + 1) && tree[2*n].end < tree[2*n+1].first)
tree[n].tagl = tree[2*n].num + tree[2*n+1].tagl;
else
tree[n].tagl = tree[2*n].tagl;
if(tree[2*n+1].num == (tree[2*n+1].r - tree[2*n+1].l + 1) && tree[2*n].end < tree[2*n+1].first)
tree[n].tagr = tree[2*n+1].num + tree[2*n].tagr;
else
tree[n].tagr = tree[2*n+1].tagr;
if(tree[2*n].end < tree[2*n+1].first)
tree[n].num = max(tree[2*n].tagr + tree[2*n+1].tagl,max(tree[2*n].num,tree[2*n+1].num));
else
tree[n].num = max(tree[2*n].num,tree[2*n+1].num);
}
int Searchl(int n,int x,int y){
int l = tree[n].l;
int r = tree[n].r;
int mid = (l + r) / 2;
if(l == x && r == y){
return tree[n].tagl;
}
if(x <= mid && y <= mid)
return Searchl(2*n,x,y);
else if(x > mid && y > mid)
return Searchl(2*n+1,x,y);
else if(Searchl(2*n,x,mid) == (mid - x + 1) && tree[2*n].end < tree[2*n+1].first)
return (mid - x + 1) + Searchl(2*n+1,mid+1,y);
else
return Searchl(2*n,x,mid);
}
int Searchr(int n,int x,int y){
int l = tree[n].l;
int r = tree[n].r;
int mid = (l + r) / 2;
if(l == x && r == y){
return tree[n].tagr;
}
if(x <= mid && y <= mid)
return Searchr(2*n,x,y);
else if(x > mid && y > mid)
return Searchr(2*n+1,x,y);
else if(Searchr(2*n+1,mid+1,y) == (y - (mid + 1) + 1) && tree[2*n].end < tree[2*n+1].first)
return Searchr(2*n,x,mid) + (y - mid);
else
return Searchr(2*n+1,mid+1,y);
}
int Find(int n,int x,int y){
int l = tree[n].l;
int r = tree[n].r;
int mid = (l + r) / 2;
if(l == x && r == y){
return tree[n].num;
}
int ans;
if(x <= mid && y <= mid) ans = Find(2*n,x,min(y,mid));
else if(y > mid && x > mid) ans = Find(2*n+1,max(x,mid+1),y);
else if(tree[2*n].end < tree[2*n+1].first)
ans = max(Searchr(2*n,x,mid) + Searchl(2*n+1,mid+1,y),max(Find(2*n,x,min(y,mid)),Find(2*n+1,max(x,mid+1),y)));
else
ans = max(Find(2*n,x,min(y,mid)),Find(2*n+1,max(x,mid+1),y));
return ans;
}
int main(){
int t,m,n;
int i;
char str[10];
int a,b;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
Build(1,0,n-1);
for(i = 0;i < m;i++){
scanf("%s%d%d",str,&a,&b);//及时只有一个字符,用%s存而不用%c存,避免读取时空格、回车这些干扰
if(str[0] == 'Q') printf("%d\n",Find(1,a,b));
else Modify(1,a,b);
}
}
return 0;
}