• hihoCoder week19 RMQ问题再临-线段树 单点更新 区间查询


    单点更新 区间查询

    #include <bits/stdc++.h>
    using namespace std;
    #define m ((l+r)/2)
    #define ls (rt<<1)
    #define rs (rt<<1|1)
    const int N = 2e6+10;
    
    int s[N], tr[N];
    
    void build(int rt,int l,int r)
    {
        if(l == r) {
            tr[rt] = s[l];
            return ;
        }
        build(ls, l, m);
        build(rs, m+1, r);
        tr[rt] = min(tr[ls], tr[rs]);
    }
    
    void update(int rt,int l,int r,int pos,int val)
    {
        if(l == r && l == pos) {
            tr[rt] = val;
            return ;
        }
        if(pos <= m) 
            update(ls, l, m, pos, val);
        else 
            update(rs, m+1, r, pos, val);
        tr[rt] = min(tr[ls], tr[rs]);
    }
    
    int query(int rt,int l,int r,int L,int R)
    {
        if(L<=l && r<=R) {
            return tr[rt];
        }
        int res = 0x3f3f3f3f;
        if(L <= m) 
            res = min(res, query(ls,l,m,L,R));
        if(R > m)
            res = min(res, query(rs,m+1,r,L,R));
        return res;
    }
    void out(int rt,int l,int r)
    {
        printf("%d %d %d
    ", l, r, tr[rt]);
        if(l == r ) {
            return ;
        }
        out(ls, l, m);
        out(rs, m+1, r);
    }
    int main()
    {
        freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        int n; scanf("%d", &n);
        for(int i=1; i<=n; i++) {
            scanf("%d", &s[i]);
        }
        build(1,1,n);
        // out(1,1,n);
        int t; scanf("%d", &t);
        while(t--) {
            int op,l,r;
            scanf("%d %d %d", &op, &l, &r);
            if(op) {
                update(1,1,n,l,r);
                //out(1,1,n);
            }
            else {
                printf("%d
    ", query(1,1,n,l,r));
            }
            // out(1,1,n);
        }
        return 0;
    }
  • 相关阅读:
    C语言基础--函数
    C语言基础--for循环
    C语言基础--while循环
    C语言基础--switch
    iOS UIView常用方法和属性
    iOS UIView简单缩放动画
    Android ListView动态改变Item高度
    iOS UILabel自定义行间距时获取高度
    iOS UILable高度自适应
    iOS 简单block的使用
  • 原文地址:https://www.cnblogs.com/Draymonder/p/10010356.html
Copyright © 2020-2023  润新知