hdu 1754 I hate it(线段树)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1754

当时校队在学线段树的时候

我跑到罗马尼亚去做志愿者了= =

所以压根没学到...校赛的时候遇到两道线段树的题目根本没办法下手= =...

深感自己弱渣,只好自己从树状数组到线段树学起,前天从树状数组看的,今天基本学完线段树了。

感觉树状数组就是个模板(若有大神路过...求轻喷,估计是hdu上我刷的10题树状数组太简单),然后线段是可能还有点写头...

我是看着notonlyasuccess大神的日志学的,代码风格估计也就差不多了,写下blog只是记录一段历程...顺便巩固以下。

写这题的原因是我用树状数组写了好久发现很难求区间最值,然后网上看了大牛的代码...发觉这样好像是比较麻烦,还是用现学的线段树吧...

这题题目就不用说了,中文题。


代码如下,线段树入门题

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define maxn 200002
int MAX[maxn<<2];
char answer[2];
using namespace std;
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(lson);
    build(rson);
    pushup(rt);
}
int query(int ql,int qr,int l,int r,int rt)
{
    int ans=-999999;
    if(ql<=l&&r<=qr)
        return MAX[rt];
    int m=(l+r)>>1;
    if(ql<=m)
        ans=max(ans,query(ql,qr,lson));
    if(m<qr)
        ans=max(ans,query(ql,qr,rson));
    return ans;
}
void update(int p,int v,int l,int r,int rt)
{
    int m=(l+r)>>1;
    if(l==r)
    {
        MAX[rt]=v;
        return ;
    }
    else
    {
        if(p<=m)
            update(p,v,lson);
        else
            update(p,v,rson);
        pushup(rt);
    }

}
int main()
{
    int a,b;
    //freopen("in.txt","r",stdin);
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        build(1,n,1);
        while(m--)
        {
            scanf("%s",answer);
            scanf("%d%d",&a,&b);
            if(answer[0]=='Q')
                printf("%d\n",query(a,b,1,n,1));
            else
                update(a,b,1,n,1);
        }
    }

    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值