线段树(区间维护):HDU 3308 LCIS

LCIS

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5951    Accepted Submission(s): 2578


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<=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

 

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 const int maxn=100010;
 6 int T[maxn<<2],L[maxn<<2],R[maxn<<2],ar[maxn],n,m;
 7 
 8 void Updata(int node,int l,int r)
 9 {    
10     int mid=(l+r)>>1,a=node<<1,b=a|1;
11 
12     L[node]=L[a];
13     if(L[a]==mid-l+1&&ar[mid]<ar[mid+1])
14         L[node]+=L[b];
15     
16     R[node]=R[b];
17     if(R[b]==r-mid&&ar[mid]<ar[mid+1])
18         R[node]+=R[a];
19     
20     T[node]=max(T[a],T[b]);
21     if(ar[mid]<ar[mid+1])
22         T[node]=max(T[node],R[a]+L[b]);
23 }
24 
25 void Change(int node,int l,int r,int pos,int x)
26 {
27     if(l==r){
28         T[node]=L[node]=R[node]=1;
29         ar[pos]=x;
30         return;
31     }
32     int mid=(l+r)>>1;
33     if(mid>=pos)
34         Change(node<<1,l,mid,pos,x);
35     else
36         Change(node<<1|1,mid+1,r,pos,x);
37     Updata(node,l,r);    
38 }
39 int ans;
40 
41 int Query(int node,int l,int r,int a,int b)
42 {
43     if(l>=a&&r<=b)
44         return T[node];
45     int mid=(l+r)>>1;
46     int ret=0;
47     if(a<=mid)
48         ret=Query(node<<1,l,mid,a,b);
49     if(b>mid)
50         ret=max(ret,Query(node<<1|1,mid+1,r,a,b));
51     if(a<=mid&&b>mid&&ar[mid]<ar[mid+1])
52         ret=max(ret,min(R[node<<1],mid-a+1)+min(L[node<<1|1],b-mid));
53     return ret;
54 }            
55 int main()    
56 {                
57     int Tk,a,b;        
58     char s[5];        
59     scanf("%d",&Tk);    
60     while(Tk--)        
61     {                        
62         scanf("%d%d",&n,&m);
63         memset(L,0,sizeof(L));
64         memset(R,0,sizeof(R));
65         memset(T,0,sizeof(T));
66         
67         for(int i=1;i<=n;i++){
68             scanf("%d",&ar[i]);
69             Change(1,1,n,i,ar[i]);
70         }
71         while(m--){                    
72             scanf("%s",s);                
73             scanf("%d%d",&a,&b);            
74             if(s[0]=='U')                
75                 Change(1,1,n,a+1,b);            
76             else{                                                        
77                 printf("%d\n",Query(1,1,n,a+1,b+1));            
78             }                        
79         }                                
80     }                
81     return 0;
82 }

 

posted @ 2016-03-13 22:59  TenderRun  阅读(184)  评论(0编辑  收藏  举报