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].
InputT 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).
OutputFor 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
需要重新做~~~~
半抄半写的情况下,终于对线段树有了一点熟悉。Pushup中对区间的合并,Query中对区间查询时的讨论,都是值得思考的地方!
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #define lson l , m , rt << 1 6 #define rson m+1,r , rt << 1 | 1 7 using namespace std; 8 9 const int maxn=100005; 10 11 int n,q,x,y; 12 int ls[maxn<<2],rs[maxn<<2],no[maxn<<2]; 13 int a[maxn]; 14 15 void Pushup(int l,int r,int rt){ 16 ls[rt]=ls[rt<<1],rs[rt]=rs[rt<<1|1]; 17 no[rt]=max(no[rt<<1],no[rt<<1|1]); 18 int m=(l+r)>>1; 19 if(a[m]<a[m+1]){ 20 if(ls[rt]==m-l+1) ls[rt]+=ls[rt<<1|1]; 21 if(rs[rt]==r-m) rs[rt]+=rs[rt<<1]; 22 no[rt]=max(no[rt],ls[rt<<1|1]+rs[rt<<1]); 23 } 24 } 25 26 void Build(int l,int r,int rt){ 27 if(l==r){ no[rt]=ls[rt]=rs[rt]=1; return; } 28 int m=(l+r)>>1; 29 Build(lson); 30 Build(rson); 31 Pushup(l,r,rt); 32 } 33 34 void Update(int l,int r,int rt){ 35 if(l==r) return; 36 int m=(l+r)>>1; 37 if(x<=m) Update(lson); 38 if(x>m) Update(rson); 39 Pushup(l,r,rt); 40 } 41 42 int Query(int l,int r,int rt){ 43 if(x<=l&&r<=y) return no[rt]; 44 int m=(l+r)>>1; 45 if(y<=m) return Query(lson); 46 if(x>m) return Query(rson); 47 int n1=Query(lson); 48 int n2=Query(rson); 49 int ans=max(n1,n2); 50 if(a[m]<a[m+1]) ans=max(ans,(min(ls[rt<<1|1],y-m)+min(rs[rt<<1],m-x+1))); //key point!!!! 51 return ans; 52 } 53 54 int main() 55 { int kase; 56 cin>>kase; 57 while(kase--){ 58 scanf("%d%d",&n,&q); 59 for(int i=1;i<=n;i++) scanf("%d",&a[i]); 60 Build(1,n,1); 61 62 while(q--){ 63 char op[5]; 64 scanf("%s%d%d",op,&x,&y); 65 ++x; 66 if(op[0]=='U'){ a[x]=y; Update(1,n,1); } 67 else{ ++y; printf("%d ",Query(1,n,1));} 68 } 69 } 70 return 0; 71 }