HDU 1754 I Hate It

同样是线段树,这道题是求区间最值,单点更新。线段树代码愈发熟练。

/*2012-08-07 16:02:53    Accepted    1754    1093MS    2276K    1361 B    G++    Yu*/

#include<stdio.h>

#include<algorithm>

using namespace std;

#define lson l, m, rt << 1

#define rson m + 1, r, rt << 1 | 1



const int MAXN = 200020;

int Max[MAXN << 2];

int n, m;



void PushUp(int rt)

{

    Max[rt] = max(Max[rt << 1], Max[rt << 1 | 1]);

}



void build(int l, int r, int rt)

{

    int m = l + r >> 1;

    if(l == r)

    {

        scanf("%d", &Max[rt]);

        return;

    }

    build(lson);

    build(rson);

    PushUp(rt);

}



void update(int p, int c, int l, int r, int rt)

{

    int m = l + r >> 1;

    if(l == r)

    {

        Max[rt] = c;

        return;

    }

    if(p <= m) update(p, c, lson);

    else update(p, c, rson);

    PushUp(rt);

}



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

{

    int ret = 0, m = l + r >> 1;

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

    {

        return Max[rt];

    }

    if(L <= m) ret = max(ret, query(L, R, lson));

    if(R > m) ret = max(ret, query(L, R, rson));

    return ret;

}



void operation()

{

    char op[5];

    int a, b;

    while(m --)

    {

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

        if('Q' == op[0])

            printf("%d\n", query(a, b, 1, n, 1));

        else

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

    }

}



int main()

{

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

    {

        build(1, n, 1);

        operation();

    }

    return 0;

}

 

你可能感兴趣的:(HDU)