其他分享
首页 > 其他分享> > hdu1754 I Hate It(线段树)

hdu1754 I Hate It(线段树)

作者:互联网

I Hate It

线段树:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

typedef long long LL;

/**
 * Segment Tree
 */
class SegTree
{
    vector<LL> tree;
    void pushUp(int rt) {
        tree[rt] = max(tree[rt<<1], tree[rt<<1|1]);
    }
    void pushDown(int L, int R, int rt) {
    }
public:
    SegTree(int n) {
        tree.resize(n<<2);
    }
    void build(int L, int R, int rt) {
        if (L == R) {
            scanf("%lld", &tree[rt]);
            return;
        }
        int m = L+R >> 1;
        build(L, m, rt<<1);
        build(m+1, R, rt<<1|1);
        pushUp(rt);
    }
    void update(int x, int y, LL val, int L, int R, int rt){
        if (x <= L && R <= y) {
            tree[rt] = val;
            return;
        }
        pushDown(L, R, rt);
        int m = L+R >> 1;
        if (x <= m) update(x, y, val, L, m, rt<<1);
        if (y > m) update(x, y, val, m+1, R, rt<<1|1);
        pushUp(rt);
    }
    LL query(int x, int y, int L, int R, int rt) {
        if (x <= L && R <= y) {
            return tree[rt];
        }
        pushDown(L, R, rt);
        int m = L+R >> 1;
        LL ans = LLONG_MIN;
        if (x <= m) ans = max(ans,query(x, y, L, m, rt<<1));
        if (y > m) ans = max(ans,query(x, y, m+1, R, rt<<1|1));
        return ans;
    }
};

int main()
{
    int n, m;
    while (~scanf("%d%d", &n, &m)) {
        SegTree st(n);
        st.build(1,n,1);
        while (m--) {
            char c;
            int x,y;
            scanf("%*c%c%d%d", &c, &x, &y);
            if (c == 'Q') {
                printf("%lld\n", st.query(x,y,1,n,1));
            }
            else if (c == 'U') {
                st.update(x,x,y,1,n,1);
            }
        }
    }

    return 0;
}

标签:rt,线段,tree,long,hdu1754,ans,include,Hate,LL
来源: https://www.cnblogs.com/zbhfz/p/14475534.html