hdu 1754 I Hate It

本文介绍了一道使用线段树进行单点更新的编程题,详细解释了算法实现过程并提供了代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

也是属于线段树单点更新类型的题目。总体比较简单,很容易看懂。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754

#include <cstdio> #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 const int maxx=200001; int maxn[maxx<<2]; int max(int a,int b){ return a>b?a:b; } void pushUp(int rt){ maxn[rt]=max(maxn[rt<<1],maxn[rt<<1|1]); } int query(int L,int R,int l,int r,int rt){ if(L<=l && R>=r){ return maxn[rt]; } int m=(l+r)>>1; int ret=0; if(L<=m)ret=max(ret,query(L,R,lson)); if(R>m)ret=max(ret,query(L,R,rson)); return ret; } void build(int l,int r,int rt){ if(l==r){ scanf("%d",&maxn[rt]); return ; } int m=(l+r)>>1; build(lson); build(rson); pushUp(rt); } void update(int p,int sc,int l,int r,int rt){ if(l==r){ maxn[rt]=sc; return ; } int m=(l+r)>>1; if(p<=m)update(p,sc,lson); else update(p,sc,rson); pushUp(rt); } int main(){ int n,m,a,b; while(scanf("%d%d",&n,&m)!=EOF){ build(1,n,1); char op[3]; while(m--){ scanf("%s%d%d",op,&a,&b); if(op[0]=='Q')printf("%d\n",query(a,b,1,n,1)); else update(a,b,1,n,1); } } return 0; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值