hdu 1754 线段树模板题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 #include <iostream> 5 #include <cstring> 6 #include <queue> 7 #include <vector> 8 9 #define maxn 230050 10 #define lson l,mid,u<<1 11 #define rson mid+1,r,u<<1|1 12 using namespace std; 13 14 const int INF = 0x3f3f3f; 15 16 int seg[maxn<<2]; 17 18 int Push_UP(int u){ 19 seg[u] = max(seg[u<<1],seg[u<<1|1]); //****这个地方 u<<1|1 不能换为 u << 1 +1 20 } 21 void build(int l,int r,int u){ 22 if(l == r){ 23 scanf("%d",&seg[u]); 24 return; 25 } 26 int mid = (l + r)/2; 27 build(lson); 28 build(rson); 29 Push_UP(u); 30 } 31 void Update(int loc,int num,int l,int r,int u){ 32 if(l == r){ // 这个地方要注意!! 33 seg[u] = num; 34 return; 35 } 36 int mid = (l + r)/2; 37 if(loc <= mid) Update(loc,num,lson); 38 else Update(loc,num,rson); 39 Push_UP(u); 40 } 41 int Query(int L,int R,int l,int r,int u){ 42 if(L <= l && r <= R){ 43 return seg[u]; 44 } 45 int ret = 0; 46 int mid = (l + r)/2; 47 if(L <= mid) ret =max(ret,Query(L,R,lson)); 48 if(R > mid) ret =max(ret,Query(L,R,rson)); 49 return ret; 50 } 51 int main() 52 { 53 if(freopen("input.txt","r",stdin)== NULL) {printf("Error\n"); exit(0);} 54 int N,M; 55 while(cin>>N>>M){ 56 build(1,N,1); 57 while(M--){ 58 char query[2]; 59 int l,r; 60 scanf("%s%d%d",query,&l,&r); 61 if(query[0] == 'U') Update(l,r,1,N,1); 62 else{ 63 int ans = Query(l,r,1,N,1); 64 printf("%d\n",ans); 65 } 66 } 67 } 68 }