(线段树)hdoj 3308-LCIS

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,ba,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
  1 #include <iostream>
  2 //#include<bits/stdc++.h>
  3 #include <stack>
  4 #include <queue>
  5 #include <map>
  6 #include <set>
  7 #include <cstdio>
  8 #include <cstring>
  9 #include <algorithm>
 10 #include <math.h>
 11 using namespace std;
 12 typedef long long ll;
 13 typedef unsigned long long ull;
 14 const int MAX=1e5+5;
 15 struct node
 16 {
 17     int left,right,mid;
 18     int x,y;
 19 }st[10*MAX];
 20 void pushup(int l,int r,int k)
 21 {
 22     st[k].left=st[2*k].left;
 23     st[k].right=st[2*k+1].right;
 24     st[k].mid=max(st[2*k].mid,st[2*k+1].mid);
 25     st[k].x=st[2*k].x;
 26     st[k].y=st[2*k+1].y;
 27     if(st[2*k].y<st[2*k+1].x)
 28     {
 29         st[k].mid=max(st[k].mid,st[2*k].right+st[2*k+1].left);
 30         if(st[2*k].left==(l+r)/2-l)
 31             st[k].left=st[2*k].left+st[2*k+1].left;
 32         if(st[2*k+1].right==r-(l+r)/2)
 33             st[k].right=st[2*k+1].right+st[2*k].right;
 34     }
 35 }
 36 void init(int l,int r,int k)
 37 {
 38     st[k].left=st[k].right=st[k].mid=r-l;
 39     if(l+1==r)
 40     {
 41         scanf("%d",&st[k].x);
 42         st[k].y=st[k].x;
 43         return;
 44     }
 45     init(l,(l+r)/2,2*k);
 46     init((l+r)/2,r,2*k+1);
 47     pushup(l,r,k);
 48 }
 49 void update(int l,int r,int ul,int ur,int val,int k)//当下[l,r), 目标更新[ul,ur) 为val
 50 {
 51     if(ul==l&&ur==r)
 52     {
 53         st[k].x=st[k].y=val;
 54         return;
 55     }
 56     int mid=(l+r)/2;
 57     if(mid>=ur)
 58         update(l,mid,ul,ur,val,2*k);
 59     if(mid<=ul)
 60         update(mid,r,ul,ur,val,2*k+1);
 61     st[k].mid=max(st[2*k].mid,st[2*k+1].mid);
 62     st[k].left=st[2*k].left;
 63     st[k].right=st[2*k+1].right;
 64     st[k].x=st[2*k].x;
 65     st[k].y=st[2*k+1].y;
 66     if(st[2*k].y<st[2*k+1].x)
 67     {
 68         st[k].mid=max(st[k].mid,st[2*k].right+st[2*k+1].left);
 69     if(st[2*k].left==(l+r)/2-l)
 70         st[k].left+=st[2*k+1].left;
 71     if(st[2*k+1].right==r-(l+r)/2)
 72         st[k].right+=st[2*k].right;
 73     }
 74     return;
 75 }
 76 int query(int l,int r,int ql,int qr,int k)
 77 {
 78     if(l>=ql&&r<=qr)
 79         return st[k].mid;
 80     int mid=(l+r)/2;
 81     if(mid<=ql)
 82         return query(mid,r,ql,qr,2*k+1);
 83     else if(mid>=qr)
 84         return query(l,mid,ql,qr,2*k);
 85     else
 86     {
 87         int an;
 88         an=max(query(l,mid,ql,qr,2*k),query(mid,r,ql,qr,2*k+1));
 89         if(st[2*k].y<st[2*k+1].x)
 90             an=max(an,min(st[2*k].right,mid-ql)+min(st[2*k+1].left,qr-mid));
 91         return an;
 92     }
 93 }
 94 int t;
 95 int N,M;
 96 char op[10];
 97 int qq,pp;
 98 int main()
 99 {
100     scanf("%d",&t);
101     while(t--)
102     {
103         scanf("%d %d",&N,&M);
104         init(1,N+1,1);
105         for(int i=1;i<=M;i++)
106         {
107             scanf("%s %d %d",op,&qq,&pp);
108             qq++;
109             if(op[0]=='U')
110             {
111                 update(1,N+1,qq,qq+1,pp,1);
112             }
113             else
114             {
115                 pp++;
116                 printf("%d
",query(1,N+1,qq,pp+1,1));
117             }
118         }
119     }
120 }
原文地址:https://www.cnblogs.com/quintessence/p/6435163.html