您的位置:首页 > 其它

hdu 1754

2013-03-31 22:16 183 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754

刚学的线段树,只能是现学现卖了,orz...

View Code

#include<iostream>
#include<algorithm>
const int MAXN=200004;
using namespace std;
int MAX[MAXN<<2];

void Push_Up(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(l,m,rt<<1);
Build(m+1,r,rt<<1|1);
Push_Up(rt);
}

void Updata(int p,int sc,int l,int r,int rt){
if(l==r){
MAX[rt]=sc;
return ;
}
int m=(l+r)>>1;
if(p<=m)Updata(p,sc,l,m,rt<<1);
else Updata(p,sc,m+1,r,rt<<1|1);
Push_Up(rt);
}

int Query(int L,int R,int l,int r,int rt){
if(L<=l&&r<=R){
return MAX[rt];
}
int m=(l+r)>>1;
int ret=0;
if(L<=m)ret=max(ret,Query(L,R,l,m,rt<<1));
if(R>m)ret=max(ret,Query(L,R,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[4];
int a,b;
for(int i=1;i<=m;i++){
scanf("%s%d%d",str,&a,&b);
if(str[0]=='Q'){
printf("%d\n",Query(a,b,1,n,1));
}else
Updata(a,b,1,n,1);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: