hihoCoder 1077 RMQ问题再临-线段树

今天把这题一做,才知道以前很多地方都用错了,杭电的数据也太水了,今天算是大彻大悟了。

#include <iostream>
#include <cstdio>
#include <algorithm>
#define N 1000005
using namespace std;

int n,m;
int a[N];
struct node{
    int l,r,mid,minn;
}tree[N<<2];

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

void update(int m,int a,int val){
    if(tree[m].l == tree[m].r){
        tree[m].minn = val;
        return ;
    }
    if(a <= tree[m].mid)
        update(m<<1,a,val);
    else
        update((m<<1)+1,a,val);
    tree[m].minn = min(tree[m<<1].minn,tree[(m<<1)+1].minn);
}

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

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

另贴一错误思想代码,但是hdu1754居然能过。。。

#include <iostream>
#include <cstdio>
#include <algorithm>
#define N 200005
using namespace std;

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

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

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

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

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值