您的位置:首页 > 其它

HDU 1754 线段树应用(二)

2013-01-20 17:39 225 查看
这道题考察了线段树 查询最大值 ,更新结点等基本操作,题目本身难度不大,一定记住,数组一定要开大,不然后RUN TIME ERROR


#include <cstdio>
#include <algorithm>

using namespace std;

#define lchild l, mid , rt << 1
#define rchild mid + 1 , r ,rt << 1|1

const int maxn = 520520;

int arr[maxn<<2];

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

void build(int l,int r,int rt)
{
if(l == r)
{
scanf("%d",&arr[rt]);
return ;
}
int mid = (l + r)>>1;
build(lchild);
build(rchild);
PushUP(rt);
}

void update(int p ,int sg,int l,int r ,int rt)
{
if( l == r )
{
arr[rt] = sg;
return ;
}
int mid = (l + r)>>1;
if(p<=mid)
update( p , sg , lchild);
else
update( p , sg , rchild);
PushUP(rt);
}

int Query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return arr[rt];
}
int mid = (l + r)>>1;
int tem = 0;
if(L<=mid)   tem = max(tem,Query(L,R,lchild));
if(R>mid)    tem = max(tem,Query(L,R,rchild));
return tem;
}

int main()
{
//freopen("in.txt","r",stdin);
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
build(1,n,1);
while(m--)
{
char op[2];
int a,b;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: