hdu1754 线段树端点更新

本文介绍了一种基于区间树的数据结构实现,支持区间查询和单点更新操作。通过递归方式构建和维护区间树,实现了高效查询和更新区间内最大值的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单端点更新

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define maxn 210000
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int n,q;
int tree[maxn<<2];
void pushup(int rt)
{
    tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);
}
void build(int l,int r,int rt)
{
    int m=(l+r)>>1;
    if(l==r)
    {
        scanf("%d",&tree[rt]);
        return ;
    }
    build(lson);
    build(rson);
    pushup(rt);
}
void update(int l,int r,int rt,int k,int add)
{
    if(l==r)
    {
        tree[rt]=add;
        return;
    }
    int m=(l+r)>>1;
    if(k<=m)
    {
        update(lson,k,add);
    }
    else
    {
        update(rson,k,add);
    }
    pushup(rt);
}
int query(int l,int r,int rt,int x,int y)
{
    int tmp=0,tmp2=0;
    if(x<=l&&y>=r)
    {
        return tree[rt];
    }
    int m=(l+r)>>1;
    if(x<=m)
    {
        tmp=query(lson,x,y);
    }
    if(y>m)
    {
        tmp2=query(rson,x,y);
    }
    return max(tmp,tmp2);
}
int main()
{
  int n , m;
	while (~scanf("%d%d",&n,&m))
    {
		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( 1 , n , 1,a , b ));
			else update( 1 , n , 1,a , b);
		}

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值