hdu - 1754 I Hate It 线段树 点更新

题意: 中文题

query  区间最大值,保存最大值就好了

点更新

学长博客:https://blog.csdn.net/sun897949163/article/details/52092178

链接:hdu 1754

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF 0x3f3f3f3f
#define ll long long

using namespace std;

const int inf = 0x3f3f3f3f;
const int maxn = 200005;
const int maxm = 1000005;

int n, m, a[maxn];

struct node {
    int l, r;
    int v;
}tree[maxn << 2];

void pushup(int rt) {
    tree[rt].v = max(tree[rt << 1].v, tree[rt << 1 | 1].v);
}

void build(int rt, int l, int r) {
    tree[rt].l = l;
    tree[rt].r = r;
    if(r == l) {
        tree[rt].v = a[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(rt << 1, l, mid);
    build(rt << 1 | 1, mid + 1, r);
    pushup(rt);
}

void update(int rt, int k, int v) {
    if(tree[rt].l == k && tree[rt].r == k) {
        tree[rt].v = v;
        return;
    }
    int mid = (tree[rt].l + tree[rt].r) >> 1;
    if(k <= mid)
        update(rt << 1, k, v);
    if(k > mid)
        update(rt << 1 | 1, k, v);
    pushup(rt);
}

int query(int rt, int l, int r) {
    if(tree[rt].l == l && tree[rt].r == r)
        return tree[rt].v;
    int mid = (tree[rt].l + tree[rt].r) >> 1;
    if(r <= mid)
        return query(rt << 1, l, r);
    if(l > mid)
        return query(rt << 1 | 1, l, r);
    if(l <= mid && r > mid)
        return max(query(rt << 1, l, mid), query(rt << 1 | 1, mid + 1, r));

    return 0;
}

int main ()
{
    while(scanf("%d %d", &n, &m) != EOF) {
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
        }
        build(1, 1, n);
        int b, c;
        char str[5];
        for(int i = 1; i <= m; i++) {
            scanf("%s %d %d", str, &b, &c);
            if(str[0] == 'Q') {
                printf("%d\n", query(1, b, c));
            }
            else {
                update(1, b, c);
            }
        }
    }
    return 0;
}


你可能感兴趣的:(线段树)