zoukankan      html  css  js  c++  java
  • HDOJ 1754 I Hate It(线段树基本操作)

    思路:

    线段树的基本操作:单点替换,区间最值

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    #define lhs l, m, rt << 1
    #define rhs m + 1, r, rt << 1 | 1
    
    const int maxn = 200010;
    int seg[maxn << 2];
    
    void pushUp(int rt)
    {
        seg[rt] = max(seg[rt << 1], seg[rt << 1 | 1]);
    }
    
    void build(int l, int r, int rt)
    {
        if (l == r)
        {
            scanf("%d", &seg[rt]);
            return;
        }
        int m = (l + r) >> 1;
        build(lhs);
        build(rhs);
        pushUp(rt);
    }
    
    void update(int p, int delta, int l, int r, int rt)
    {
        if (l == r)
        {
            seg[rt] = delta;
            return;
        }
        int m = (l + r) >> 1;
        if (p <= m)
            update(p, delta, lhs);
        else
            update(p, delta, rhs);
        pushUp(rt);
    }
    
    int query(int beg, int end, int l, int r, int rt)
    {
        if (beg <= l && r <= end)
            return seg[rt];
    
        int m = (l + r) >> 1;
        int ret = 0;
    
        if (beg <= m)
            ret = query(beg, end, lhs);
        if (end > m)
            ret = max(ret, query(beg, end, rhs));
    
        return ret;
    }
    
    int main()
    {
        int n, m;
        while (scanf("%d %d", &n, &m) != EOF)
        {
            build(1, n, 1);
    
            while (m--)
            {
                char op[16];
                int a, b;
                scanf("%s", op);
                scanf("%d %d", &a, &b);
    
                if (op[0] == 'Q')
                    printf("%d\n", query(a, b, 1, n, 1));
                else if (op[0] == 'U')
                    update(a, b, 1, n, 1);
            }
        }
        return 0;
    }
    -------------------------------------------------------

    kedebug

    Department of Computer Science and Engineering,

    Shanghai Jiao Tong University

    E-mail: kedebug0@gmail.com

    GitHub: http://github.com/kedebug

    -------------------------------------------------------

  • 相关阅读:
    Careercup
    Careercup
    Careercup
    Careercup
    Careercup
    Careercup
    Careercup
    Careercup
    Careercup
    Careercup
  • 原文地址:https://www.cnblogs.com/kedebug/p/2856178.html
Copyright © 2011-2022 走看看