1、http://acm.hdu.edu.cn/showproblem.php?pid=3308
2、题目大意:
给出一个由n个数字组成的序列,有两种操作,U A B是将A位置的数字替换成B,Q A B是查询A-B区间有多少个递增的数字,也就是求A-B区间的最长上升子序列的长度
这道题目有对区间的查询及对单点的更新,很容易想到线段树,只是在具体实现的时候还是有些困难,具体看代码
要记录区间里的最长的序列长度是多少,用smax来记录。由于最长的连续上升序列可以在区间的左端点,右端点,所以在线段树里增加两个域lmax和rmax,分别表示,在区间的端点处,分别向右向左的最长的满足条件的序列长度。
在更新当前区间的lmax时,如果左子区间的lmax等于左子区间的长度,那么当前区间的lmax就要加上右儿子的lmax。更新当前区间的rmax时同理。
另外在查询的时候,要注意,有可能当前区间的lmax大于mid-st+1,所以要在其中取一个较小值,同理rmax有可能大于ed-mid。
3、题目:
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3479 Accepted Submission(s): 1554
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
1 1 4 2 3 1 2 5
4、AC代码:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define N 400005 #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 int lmax[N],rmax[N],smax[N]; int a[N]; void pushUp(int d,int m,int rt) { lmax[rt]=lmax[rt<<1]; rmax[rt]=rmax[rt<<1|1]; smax[rt]=max(smax[rt<<1],smax[rt<<1|1]); if(a[m]<a[m+1]) { if(lmax[rt<<1]==(d-(d>>1))) lmax[rt]+=lmax[rt<<1|1]; if(rmax[rt<<1|1]==(d>>1)) rmax[rt]+=rmax[rt<<1]; smax[rt]=max(smax[rt],rmax[rt<<1]+lmax[rt<<1|1]); } } void build(int l,int r,int rt) { if(l==r) { scanf("%d",&a[l]); lmax[rt]=1; rmax[rt]=1; smax[rt]=1; return ; } int m=(l+r)>>1; build(lson); build(rson); pushUp(r-l+1,m,rt); } void update(int b,int c,int l,int r,int rt) { if(l==r) { a[b]=c; return ; } int m=(l+r)>>1; if(b<=m) update(b,c,lson); //错在if(r>m) else update(b,c,rson); pushUp(r-l+1,m,rt); } int query(int L,int R,int l,int r,int rt) { if(L<=l && R>=r) { return smax[rt]; } int m=(l+r)>>1; int ans=-1; if(L<=m) ans=max(ans,query(L,R,lson)); if(R>m) ans=max(ans,query(L,R,rson)); if(a[m]<a[m+1]) { int tmpR=lmax[rt<<1|1]+m; int tmpL=m-rmax[rt<<1]+1; ans=max(ans,min(R,tmpR)-max(L,tmpL)+1); } return ans; } int main() { int t,b,c,n,m; char ch[3]; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); build(0,n-1,1); for(int i=1;i<=m;i++) { scanf("%s%d%d",ch,&b,&c); //printf("%c %d %d\n",ch[0],b,c); if(ch[0]=='Q') { int ans=query(b,c,0,n-1,1); printf("%d\n",ans); } else { update(b,c,0,n-1,1); } } } return 0; }