I Hate It 1754
题意不说了,中文都能看懂。。。
题解:线段树的简单应用。建树,查找,更新。
AC代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<iostream> 3 using namespace std; 4 const int maxn=10000000; 5 int maxs[maxn]; 6 int max(int a,int b){ 7 return a>b?a:b; 8 } 9 void build(int l,int r,int pos){ 10 if(l==r){ 11 scanf("%d",&maxs[pos]); 12 return; 13 } 14 int mid=(l+r)>>1; 15 build(l,mid,pos*2); 16 build(mid+1,r,pos*2+1); 17 maxs[pos]=max(maxs[pos*2],maxs[pos*2+1]); 18 } 19 int Query(int l,int r,int left,int right,int pos){ 20 if(l<=left&&right<=r)return maxs[pos]; 21 int mid=(left+right)>>1; 22 if(r<=mid)return Query(l,r,left,mid,pos*2); 23 if(l>mid)return Query(l,r,mid+1,right,pos*2+1); 24 else return max(Query(l,r,left,mid,pos*2),Query(l,r,mid+1,right,pos*2+1)); 25 } 26 void Updata(int a,int b,int l,int r,int pos){ 27 if(l==r){ 28 maxs[pos]=b; 29 return; 30 } 31 int mid=(l+r)>>1; 32 if(a<=mid)Updata(a,b,l,mid,pos*2); 33 else Updata(a,b,mid+1,r,pos*2+1); 34 maxs[pos]=max(maxs[pos*2],maxs[pos*2+1]); 35 } 36 int main() 37 { 38 int n,m,a,b; 39 char oper[2]; 40 while(~scanf("%d %d",&n,&m)){ 41 build(1,n,1); 42 while(m--){ 43 scanf("%s %d %d",oper,&a,&b); 44 if(oper[0]=='Q')printf("%d\n",Query(a,b,1,n,1)); 45 else Updata(a,b,1,n,1); 46 } 47 } 48 return 0; 49 }
posted on 2012-10-09 12:05 Acmer_Roney 阅读(146) 评论(0) 编辑 收藏 举报