HDU 1754 I Hate It(线段树)

#include <iostream>
#include <cstdio>
#include <memory.h>
using namespace std;
const int maxn=200000;
int sc[maxn<<2],n,m;

#define lson l,m,rt<<1//左子树 
#define rson m+1,r,(rt<<1)|1//右子树

void pushUp(int rt){
	sc[rt]=max(sc[rt<<1],sc[rt<<1|1]);
}
void build(int l,int r,int rt){
	if(l==r){//元线段(叶子节点),输入成绩
		scanf("%d",&sc[rt]);
		return;
	}
	int m=(l+r)>>1;
	build(lson),build(rson);//分别建左右子树
	pushUp(rt);//更新父节点值为左右子结点中的最大值
}
void update(int p,int v,int l,int r,int rt){
	if(l==r){//元线段(叶子结点),更新其值
		sc[rt]=v;
		return;
	}
	int m=(l+r)>>1;
	if(p<=m)update(p,v,lson);//要更新的结点在左边
	else if(p>m)update(p,v,rson);//要更新的结点在右边
	pushUp(rt);//更新父节点值为左右子结点中的最大值
}

int query(int L,int R,int l,int r,int rt){
	if(L<=l&&r<=R)return sc[rt];//如果待查区间整段包含了当前结点的区间,返回当前结点区间的值
	int m=(l+r)>>1,ret=0;
	if(L<=m)ret=max(ret,query(L,R,lson));//待查区间与当前区间的左边有交集
	if(R>m)ret=max(ret,query(L,R,rson));//待查区间与当前区间的右边有交集
	return ret;//返回左右子树中的最值
}
int main(){
	while (scanf("%d%d",&n,&m)==2)
	{
		build(1,n,1);
		char str[10];
		int a,b;
		while (m--)
		{
			scanf("%s %d %d",str,&a,&b);
			if(strcmp(str,"Q")==0)printf("%d\n",query(a,b,1,n,1));
			else update(a,b,1,n,1);
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值