hdu 1754
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754
刚学的线段树,只能是现学现卖了,orz...
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<algorithm> 3 const int MAXN=200004; 4 using namespace std; 5 int MAX[MAXN<<2]; 6 7 void Push_Up(int rt){ 8 MAX[rt]=max(MAX[rt<<1],MAX[rt<<1|1]); 9 } 10 11 void Build(int l,int r,int rt){ 12 if(l==r){ 13 scanf("%d",&MAX[rt]); 14 return ; 15 } 16 int m=(l+r)>>1; 17 Build(l,m,rt<<1); 18 Build(m+1,r,rt<<1|1); 19 Push_Up(rt); 20 } 21 22 void Updata(int p,int sc,int l,int r,int rt){ 23 if(l==r){ 24 MAX[rt]=sc; 25 return ; 26 } 27 int m=(l+r)>>1; 28 if(p<=m)Updata(p,sc,l,m,rt<<1); 29 else Updata(p,sc,m+1,r,rt<<1|1); 30 Push_Up(rt); 31 } 32 33 int Query(int L,int R,int l,int r,int rt){ 34 if(L<=l&&r<=R){ 35 return MAX[rt]; 36 } 37 int m=(l+r)>>1; 38 int ret=0; 39 if(L<=m)ret=max(ret,Query(L,R,l,m,rt<<1)); 40 if(R>m)ret=max(ret,Query(L,R,m+1,r,rt<<1|1)); 41 return ret; 42 } 43 44 45 int main(){ 46 int n,m; 47 while(~scanf("%d%d",&n,&m)){ 48 Build(1,n,1); 49 char str[4]; 50 int a,b; 51 for(int i=1;i<=m;i++){ 52 scanf("%s%d%d",str,&a,&b); 53 if(str[0]=='Q'){ 54 printf("%d\n",Query(a,b,1,n,1)); 55 }else 56 Updata(a,b,1,n,1); 57 } 58 } 59 return 0; 60 }