您的位置:首页 > 其它

hdu1754I Hate It(线段树---单点更新,区间求值)

2013-08-06 20:29 357 查看
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;
}


 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  线段树