HDU - 1754 I Hate It (线段树区间最值)

题意:给出N个数,并且有修改第i个数的值,和查询在某个区间的最大值

思路:使用线段树来记录区间的最值,此题还是仅仅对叶子结点的修改,所以即 在HDU-1166 上把 update 和 query 函数进行略微修改即可。

 

完整代码:

 
  
#include <iostream>
#include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int maxn=200005; int s[maxn],seg[maxn<<2]; void push_up(int rt) { seg[rt]=max(seg[rt<<1],seg[rt<<1|1]); //求最大值 } void build(int l,int r,int rt) { if(l==r) { seg[rt]=s[l]; return ; } int mid=(l+r)/2; build(l,mid,rt<<1); build(mid+1,r,rt<<1|1); push_up(rt); } int query(int L,int R,int l,int r,int rt) { if(L<=l && R>=r) return seg[rt]; int mid=(l+r)>>1; int ret=0; if(L<=mid) ret=max(ret,query(L,R,l,mid,rt<<1)); if(R>mid) ret=max(ret,query(L,R,mid+1,r,rt<<1|1)); return ret; } void update(int L,int s,int l,int r,int rt) { if(l==r) { seg[rt]=s; return ; } int mid=(l+r)>>1; if(L<=mid) update(L,s,l,mid,rt<<1); else update(L,s,mid+1,r,rt<<1|1); push_up(rt); } int main() { int n,m; while(~scanf("%d%d",&n,&m)) { for(int i=1; i<=n; i++) 
      scanf("%d",&s[i]); build(1,n,1); int a,b; char ch; while(m--) { scanf(" %c%d%d",&ch,&a,&b); if(ch=='Q') printf("%d\n",query(a,b,1,n,1)); else update(a,b,1,n,1); } } return 0; }
 

 

转载于:https://www.cnblogs.com/Tianwell/p/11238818.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值