hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。

线段树的模板题,试用了一下交大的模板。效率有点略低。


代码:

#include <stdio.h>
#include <string.h>
#define TREE_SIZE (1 << (20))

//const int TREE_SIZE = 200000 + 10;

int max(int a, int b)
{
    return a > b ? a : b;
}

int Cover[TREE_SIZE], Top[TREE_SIZE];

class IntervalTree
{
private:
    int size;
    int _Query(int a, int b, int l, int r, int Ind)
    {
        if (a <= l && b >= r)
            return Top[Ind];
        int mid = (l + r) >> 1, ret = Cover[Ind];
        if (a <= mid)
            ret = max(ret, _Query(a, b, l, mid, Ind << 1));
        if (b > mid)
            ret = max(ret, _Query(a, b, mid + 1, r, (Ind << 1) + 1));
        return ret;
    }
    void _Modify(int a, int l, int r, int Ind, int d)
    {
        if (l == r && l == a)
        {
            Cover[Ind] = Top[Ind] = d;
            return ;
        }
        int mid = (l + r) >> 1;
        if (a <= mid)
            _Modify(a, l, mid, Ind << 1, d);
        else
            _Modify(a, mid + 1, r, (Ind << 1) + 1, d);
        Top[Ind] = max(Top[Ind << 1], Top[(Ind << 1 )+ 1]);
    }
public:
    IntervalTree()
    {
        memset(Cover, 0, sizeof(Cover));
        memset(Top, 0, sizeof(Top));
        size = (TREE_SIZE >> 2) - 1;
    }
    IntervalTree(int s)
    {
        memset(Cover, 0, sizeof(Cover));
        memset(Top, 0, sizeof(Top));
        size = s;
    }
    int Query(int a, int b)
    {
        return _Query(a, b, 1, size, 1);
    }
    void Modify(int a, int d)
    {
        return _Modify(a, 1, size, 1, d);
    }
};

int main()
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
    #endif // LOCAL
    int n, m;
    while (scanf("%d%d", &n, &m) == 2)
    {
        IntervalTree T(n);
        for (int i = 1; i <= n; i++)
        {
            int tmp;
            scanf("%d", &tmp);
            T.Modify(i, tmp);
        }
        for (int i = 0; i < m; i++)
        {
            getchar();
            char ch;
            int a, b;
            scanf("%c%d%d", &ch, &a, &b);
            //printf("%c%d%d\n", ch,a,b);
            if (ch == 'Q')
            {
                printf("%d\n", T.Query(a, b));
                continue;
            }
            if (ch == 'U')
            {
                T.Modify(a, b);
                continue;
            }
        }
    }
    return 0;
}


update:

2015.4.21

学习这种漂亮的写法:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1

using namespace std;
const int maxn = 200000 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);

int maxPoint[maxn << 2];
void pushup(int rt)
{
    maxPoint[rt] = max(maxPoint[rt << 1], maxPoint[rt << 1 | 1]);
}

void build(int lo, int hi, int rt)
{
    if (lo == hi)
    {
        scanf("%d", &maxPoint[rt]);
        return;
    }
    int mi = (lo + hi) >> 1;
    build(lson);
    build(rson);
    pushup(rt);
}

void update(int pos, int num, int lo, int hi, int rt)
{
    if (lo == hi)
    {
        maxPoint[rt] = num;
        return;
    }
    int mi = (lo + hi) >> 1;
    if (pos <= mi)
        update(pos, num, lson);
    else
        update(pos, num, rson);
    pushup(rt);
}

int query(int L, int H, int lo, int hi, int rt)
{
    if (L <= lo && hi <= H)
    {
        return maxPoint[rt];
    }
    int mi = (lo + hi) >> 1;
    int res = 0;
    if (L <= mi)
        res = max(res, query(L, H, lson));
    if (mi < H)
        res = max(res, query(L, H, rson));
    return res;
}

int main()
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
    #endif // LOCAL
    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(a, b, 1, n, 1));
            else
                update(a, b, 1, n, 1);
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值