标签:
Description
Input
Output
Sample Input
5 6 1 2 3 4 5 Q 1 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5
Sample Output
5 6 5 9
模板题,改改就行了!
注意!!!没事别读%c,容易出事,所以都转化成字符串读入
代码如下:
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 #define M 200200 5 #define inf 0x3f3f3f3f 6 struct segTree 7 { 8 int l,r,elem; 9 int mid() 10 { 11 return (l+r)/2; 12 } 13 }tree[4*M]; 14 int n,m,ans; 15 void buildTree (int now,int left,int right) 16 { 17 tree[now].l=left; 18 tree[now].r=right; 19 if (left==right) 20 { 21 scanf("%d",&tree[now].elem); 22 return; 23 } 24 int middle=tree[now].mid(); 25 buildTree(now<<1,left,middle); 26 buildTree(now<<1|1,middle+1,right); 27 tree[now].elem=max(tree[now<<1].elem,tree[now<<1|1].elem); 28 } 29 void update (int now,int left,int right,int x,int change) 30 { 31 if (left==right) 32 { 33 tree[now].elem=change; 34 return; 35 } 36 int middle=tree[now].mid(); 37 if (x<=middle) 38 update(now<<1,left,middle,x,change); 39 else 40 update(now<<1|1,middle+1,right,x,change); 41 tree[now].elem=max(tree[now<<1].elem,tree[now<<1|1].elem); 42 } 43 void query (int now,int left,int right,int L,int R) 44 { 45 if (L<=left&&right<=R) 46 { 47 ans=max(ans,tree[now].elem); 48 return ; 49 } 50 int middle=tree[now].mid(); 51 if (R<=middle) 52 query(now<<1,left,middle,L,R); 53 else if (L>middle) 54 query(now<<1|1,middle+1,right,L,R); 55 else 56 { 57 query(now<<1,left,middle,L,R); 58 query(now<<1|1,middle+1,right,L,R); 59 } 60 } 61 int main() 62 { 63 //freopen("de.txt","r",stdin); 64 while (~scanf("%d%d",&n,&m)) 65 { 66 buildTree(1,1,n); 67 char op[6]; 68 while (m--) 69 { 70 int x,y; 71 scanf("%s",op); 72 scanf("%d%d",&x,&y); 73 if (op[0]==‘U‘) 74 update(1,1,n,x,y); 75 else 76 { 77 ans=-inf; 78 query(1,1,n,x,y); 79 printf("%d\n",ans); 80 } 81 } 82 } 83 return 0; 84 }
hdu1574 I Hate It (线段树,查询区间最大值)
标签:
原文地址:http://www.cnblogs.com/agenthtb/p/5874889.html