hdu1754解题报告

题目大意:中文题意,自行参考hdu原题

解题思路:线段树,点修改。

ac代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=2*1e5+1000;

int maxv[maxn<<2],n,m;
int  a[maxn];
char q[2];
int ql,qr,p,v;

void build(int o,int L,int R)
{
        int M=L+(R-L)/2;
        if(L==R)  maxv[o]=a[L];
        else
        {
                build(o*2,L,M);
                build(o*2+1,M+1,R);
                maxv[o]=max(maxv[o*2],maxv[o*2+1]);
        }
     //   cout<<L<<" "<<R<<" "<<o<<" "<<maxv[o]<<endl;
}

void update(int o,int L,int R)
{
         int M=L+(R-L)/2;
         if(L==R)  maxv[o]=v;
         else
         {
                 if(p<=M) update(o*2    ,L     ,M);
                 else        update(o*2+1,M+1,R);
                  maxv[o]=max(maxv[o*2],maxv[o*2+1]);
         }
}

int query(int o,int L,int R)
{
           int M=L+(R-L)/2,ans=-1;
           if(ql<=L&&R<=qr) return maxv[o];
           if(ql<=M)  ans=max(ans,query(o*2,L,M));
           if(M<qr)    ans=max(ans,query(o*2+1,M+1,R));
           return ans;
}

int main()
{
        while(scanf("%d%d",&n,&m)!=EOF)
        {
                for(int i=1;i<=n;i++)
                        scanf("%d",&a[i]);
               build(1,1,n);
                while(m--)
                {
                        scanf("%s",&q);
                        if(q[0]=='Q')
                        {
                                scanf("%d%d",&ql,&qr);
                                printf("%d\n",query(1,1,n));
                        }
                        else if(q[0]=='U')
                        {
                                scanf("%d%d",&p,&v);
                                a[p]=v;
                                update(1,1,n);
                        }
                }
        }
       return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值