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


    线段树功能:update:单点更新 query:区间最值

    #include <bits/stdc++.h>
    #define lson l, m, rt<<1
    #define rson m+1, r, rt<<1|1
    using namespace std;
    
    const int MAXN = 200008;
    int maxs[MAXN<<2];
    
    void build(int l, int r, int rt)
    {
        if(l == r)
        {
            scanf("%d", &maxs[rt]);
            return;
        }
        int m = (l + r) >> 1;
        build(lson);
        build(rson);
        maxs[rt] = max(maxs[rt<<1], maxs[rt<<1|1]);
    }
    
    int query(int L, int R, int l, int r, int rt)
    {
        if(L <= l && r <= R) return maxs[rt];
        int ret = 0;
        int m = (l + r) >> 1;
        if(L <= m) ret = max(ret, query(L, R, lson));
        if(R > m) ret = max(ret, query(L, R, rson));
        return ret;
    }
    
    void updata(int p, int news, int l, int r, int rt)
    {
        if(l == r)
        {
            maxs[rt] = news;
            return;
        }
        int m = (l + r) >> 1;
        if(p <= m) updata(p, news, lson);
        else updata(p, news, rson);
        maxs[rt] = max(maxs[rt<<1], maxs[rt<<1|1]);
    }
    
    int main()
    {
    //    freopen("in.txt", "r", stdin);
        int n,k;
        while(~scanf("%d%d", &n, &k))
        {
            build(1, n, 1);
            while(k--)
            {
                char s[5];
                scanf("%s", s);
                int a,b;
                scanf("%d%d", &a, &b);
                if(s[0] == 'Q') printf("%d
    ", query(a, b, 1, n, 1));
                else updata(a, b, 1, n, 1);
            }
        }
        return 0;
    }
  • 相关阅读:
    静态方法中访问类的实例成员
    静态初始化块
    Java字段初始化的规律
    java中函数重载
    哈姆雷特观后感 一把辛酸泪
    枚举
    验证码
    四则运算
    JAVA输出中+号的作用以及如何使用
    dev控件ASPxComboBox设置ReadOnly="true"后
  • 原文地址:https://www.cnblogs.com/pach/p/7347080.html
Copyright © 2020-2023  润新知