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

Posted draymonder

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

 

以上是关于hihoCoder week19 RMQ问题再临-线段树 单点更新 区间查询的主要内容,如果未能解决你的问题,请参考以下文章

HihoCoder 1068 RMQ-ST算法+BIT

hihocoder-Weekly228-Parentheses Matching

hihocoder-Week173--A Game

hihocoder-Weekly224-Split Array

hihocoder-Week174-Dice Possibility

hihocoder-Week175-Robots Crossing River