线段树 HDU1734 I Hate It

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=14797

题意:给定一些点的值,并在操作的过程中会附带一些重新赋值的操作。问某个区间内最大值是多少。

思路:线段树。轻松愉快不解释。

源码:

#include <cstdio>

#include <cmath>

#include <cstring>

#include <iostream>

#include <algorithm>

using namespace std;

int n,m;

int const MAXN = 200000;

int sum[MAXN<<2];

int gmax(int a,int b)

{

    return a>b?a:b;

}

void update(int a,int now,int L,int R,int st)

{

    if(L==R){

        sum[st] = now;

        return;

    }

    int mid = (L+R)>>1;

    if(mid>=a)  update(a,now,L,mid,st<<1);

    else if(mid<a)  update(a,now,mid+1,R,(st<<1)+1);

    sum[st] = gmax(sum[st<<1],sum[(st<<1)+1]);

//    puts(" hahah\n");

}

int query(int l,int r,int L,int R,int st)

{

    if(l<=L && r>=R){

        return sum[st];

    }

    int mid = (L+R)>>1;

    int t1=0,t2=0;

    if(mid>=l)  t1 = query(l,r,L,mid,st<<1);

    if(mid<r)   t2 = query(l,r,mid+1,R,(st<<1)+1);

    return gmax(t1,t2);

}

int main()

{

    while(scanf("%d%d",&n,&m)!=EOF)

    {

        int a,b;

        char t[5];

        memset(sum,0,sizeof(sum));

        for(int i=1; i<=n; i++){

            scanf("%d",&a);

            update(i,a,1,n,1);

        }

        for(int i=0; i<m; i++){

            scanf("%s%d%d",t,&a,&b);

            if(t[0]=='Q')  printf("%d\n",query(a,b,1,n,1));

            if(t[0]=='U')  update(a,b,1,n,1);

        }

    }

    return 0;

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值