您的位置:首页 > 理论基础 > 数据结构算法

数据结构 线段树 HDU 1754I Hate It(单点更新)

2016-08-16 23:59 246 查看
题意:给n门课,m次询问。

Q,a,b为询问,[a,b] ;’

U,a,b为更新第a个点,更新为b

题解:单点更新,线段树

#include <stdio.h>
#include <string.h>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int N=200005;
int Max[N<<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(l,m,rt<<1);
build(m+1,r,rt<<1|1);
PushUP(rt);
}
void update(int p,int ss,int l,int r,int rt){
//p为要改的位置,ss为要改的值
if(l==r){//找到叶子 ,更新
Max[rt]=ss;
return ;
}
int m=(l+r)>>1;
if(p<=m) update(p,ss,l,m,rt<<1);
else update(p,ss,m+1,r,rt<<1|1);
//每次都向上更新新的最大值
PushUP(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);
while(m--){
char tmp[2];//用%s 小技巧,避免回车
int a,b;
scanf("%s%d%d",&tmp,&a,&b);
if(tmp[0]=='Q'){
printf("%d\n",query(a,b,1,n,1));
}
if(tmp[0]=='U'){
update(a,b,1,n,1);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: