hdu 1754 I Hate It

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1754

解题思路:

线段树点修改,区间最大值。。。

AC代码:

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

const int N = 200005;
int n,m;
int a[N];
struct node{
    int l,r,maxn;
}tree[N<<2];

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

void update(int m,int a,int val){
    if(tree[m].l == a && tree[m].r == a){
        tree[m].maxn = val;
        return;
    }
    int mid = (tree[m].l+tree[m].r)>>1;
    if(a <= mid)
        update(m<<1,a,val);
    else
        update(m<<1|1,a,val);
    tree[m].maxn = max(tree[m<<1].maxn,tree[m<<1|1].maxn);
}

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

int main(){
    while(~scanf("%d%d",&n,&m)){
        for(int i = 1; i <= n; i++)
            scanf("%d",&a[i]);
        build(1,1,n);
        char op;
        int x,y;
        while(m--){
            getchar();
            scanf("%c%d%d",&op,&x,&y);
            if(op == 'Q')
                printf("%d\n",query(1,x,y));
            else
                update(1,x,y);
        }
    }
    return 0;
}


你可能感兴趣的:(线段树,点修改)