Description:
For a sequences1,s2,?,sN , and a pair of integers(i,J) , if1≤i≤j≤N andSI<SI+1<si+2<< Span id= "mathjax-span-54" class= "Mo"? <sj−1< Span id= "mathjax-span-63" class= "Mo" ><sj , then the sequencesi,si+1,?,sJ is aCIS
(Continuous increasing subsequence). The longestCIS
Of a sequence is called theLCIS
(Longest continuous increasing subsequence).
In this problem, we'll give you a sequence first, and then some add
operations and some query
operations. An add operation adds a value of member in a specified interval. For a query operation, you should output the length of the lcis of a specified interval.
The main idea is to find the maximal ascending sub-sequence of an interval (note that the subsequence here must be continuous.) )
Maintain prefix maximum ascending subsequence, suffix maximum ascending subsequence, maximum ascending subsequence, and value of the left end of interval, and the value of the right endpoint for the segment tree.
Note here that if pushup, the left subtree prefix is its length, then the length of the father's prefix should be left plus right, right subtree similarly.
The code is as follows:
#include <iostream>#include<cstdio>#defineMax (A, B) (A>B?A:B)#defineMin (A, b) (A<B?A:B)#defineLson l,m,po*2#defineRson m+1,r,po*2+1using namespacestd;structstate{intLef,rig,mid; intx, y;};intn,q;state bit[100005*4];intcol[100005*4];intnum[100005];voidPushdown (intPO) { if(Col[po]) {Col[po*2]+=Col[po]; Col[po*2+1]+=Col[po]; Bit[po*2].x+=Col[po]; Bit[po*2].y+=Col[po]; Bit[po*2+1].x+=Col[po]; Bit[po*2+1].y+=Col[po]; COL[PO]=0; }}voidPushup (intLintRintPO) {Bit[po].mid=max (bit[po*2].mid,bit[po*2+1].mid); Bit[po].lef=bit[po*2].lef; Bit[po].rig=bit[po*2+1].rig; bit[po].x=bit[po*2].x; Bit[po].y=bit[po*2+1].y; intM= (L+R)/2; if(bit[po*2].y<bit[po*2+1].x) {Bit[po].mid=max (bit[po].mid,bit[po*2].rig+bit[po*2+1].lef); if(bit[po*2].lef==m-l+1) Bit[po].lef=m-l+1+bit[po*2+1].lef; if(bit[po*2+1].rig==r-M) Bit[po].rig=r-m+bit[po*2].rig; }}voidBuild_tree (intLintRintPO) { if(l==R) {Bit[po].lef=bit[po].rig=bit[po].mid=1; scanf ("%d",&Col[po]); bit[po].x=bit[po].y=Col[po]; return; } intM= (L+R)/2; COL[PO]=0; Build_tree (Lson); Build_tree (Rson); Pushup (L,R,PO);}voidUpdateintUlinturintAddintLintRintPO) { if(ul<=l&&ur>=R) {Col[po]+=add; bit[po].x+=add; Bit[po].y+=add; return; } pushdown (PO); intM= (L+R)/2; if(ul<=M) Update (Ul,ur,add,lson); if(ur>M) Update (Ul,ur,add,rson); Pushup (L,R,PO);}intQueryintQlintQrintLintRintPO) { if(ql<=l&&qr>=R)returnBit[po].mid; Pushdown (PO); intM= (L+R)/2; intMAXN; if(ql>M)returnquery (Ql,qr,rson); Else if(qr<=M)returnquery (Ql,qr,lson); Else{MAXN=Max (Query (Ql,qr,lson), query (Ql,qr,rson)); if(bit[po*2].y<bit[po*2+1].x) MAXN=max (Maxn,min (m-ql+1, bit[po*2].rig) +min (qr-m,bit[po*2+1].lef)); returnMAXN; }}intMain () {intT; Charch[5]; intA,b,c; CIN>>T; for(intcas=1; cas<=t;++CAs) {printf ("Case #%d:\n", CAs); scanf ("%d%d",&n,&Q); Build_tree (1N1); for(intI=1; i<=q;++i) {scanf ("%s", CH); if(ch[0]=='Q') {scanf ("%d%d",&a,&b); printf ("%d\n", query (A, B,1N1)); } Else{scanf (" %d%d%d",&a,&b,&c); Update (A,B,C,1N1); } } } return 0;}
View Code
Medium UESTC another LCIs, segment tree + interval update.