您的位置:首页 > 其它

hdu1754线段树单点更新

2016-03-18 19:53 393 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1754

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;

const int maxn=200005;
int MAXN[maxn<<2];

void pushup(int rt)
{
MAXN[rt]=max(MAXN[rt<<1],MAXN[rt<<1|1]);
}

void build(int l,int r,int rt)
{
if(l==r)
{
scanf("%d",&MAXN[rt]);
return ;
}
int m=(l+r)>>1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
pushup(rt);
}

void update(int p,int x,int l,int r,int rt)
{
if(l==r)
{
MAXN[rt]=x;
return ;
}
int m=(l+r)>>1;
if(p<=m)
update(p,x,l,m,rt<<1);
else
update(p,x,m+1,r,rt<<1|1);
pushup(rt);
}

int search(int ll,int rr,int l,int r,int rt)
{
if(ll<=l&&r<=rr)
return MAXN[rt];
int m=(r+l)>>1;
int ret=0;
if(ll<=m)
ret=max(ret,search(ll,rr,l,m,rt<<1));
if(rr>m)
ret=max(ret,search(ll,rr,m+1,r,rt<<1|1));
return ret;
}
int main()
{int n,m;
while(~scanf("%d%d",&n,&m))
{
build(1,n,1);
char str[2];
int a,b;
while(m--)
{
scanf("%s%d%d",str,&a,&b);
if(str[0] == 'Q')
printf("%d\n",search(a,b,1,n,1));
else
update(a,b,1,n,1);
}
}

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