1.题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1754
2.参考代码:
#include <cstdio>
#include <algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=200000;
int MAX[maxn<<2];
void PushUP(int rt){
MAX[rt]=max(MAX[rt<<1],MAX[rt<<1|1]);
}
void build(int l,int r,int rt){
if(l==r)
{
scanf("%d",&MAX[rt]);
return ;
}
int m=(l+r)>>1;
build(lson);
build(rson);
PushUP(rt);
}
void update(int p,int add,int l,int r,int rt){ ///单点替换
if(l==r)
{
MAX[rt]=add;
return ;
}
int m=(l+r)>>1;
if(p<=m)
update(p,add,lson);
else
update(p,add,rson);
PushUP(rt);
}
int query(int L,int R,int l,int r,int rt){ ///区间最值
if(L<=l &&r<=R)
return MAX[rt];
int ret=0;
int m=(l+r)>>1;
if(L<=m)
ret=max(ret,query(L,R,lson));
if(R>m)
ret=max(ret,query(L,R,rson));
return ret;
}
int main()
{
int n,m,a,b;
char s[2];
while(~scanf("%d %d",&n,&m))
{
build(1,n,1);
while(m--)
{
scanf("%s %d %d",s,&a,&b);
if(s[0]=='Q')
printf("%d\n",query(a,b,1,n,1));
else if(s[0]=='U')
update(a,b,1,n,1);
}
}
return 0;
}
本文提供了一道来自杭州电子科技大学在线评测系统(HDU OJ)的1754号题目的解决方案。通过使用线段树数据结构实现区间查询和单点更新操作,该代码示例详细展示了如何高效地解决这一问题。
2557

被折叠的 条评论
为什么被折叠?



