当前位置:编程学习 > C/C++ >>

HDU 1754 I Hate It

分析:继续单点更新、学线段树的时候尽量不要去看模板、自己慢慢分析、那样才是真的学会了、尽管变形依然能做出来、单点更新先就做到这里、
 
#include<stdio.h>  
#include<string.h>  
#define N 200005  
struct node{  
    int l,r;  
    int max;  
}tree[N<<2];  
int a[N];  
int n,ans;  
int maxx(int a,int b){  
    if(a>b)return a;  
    return b;  
}  
void create(int x,int l,int r){  
    tree[x].l=l;  
    tree[x].r=r;  
    int mid=(l+r)/2;  
    if(l==r){  
        tree[x].max=a[l];  
        return;  
    }  
    create(x*2,l,mid);  
    create(x*2+1,mid+1,r);  
    tree[x].max=maxx(tree[x*2].max,tree[x*2+1].max);  
}  
void update(int x,int p,int num){  
    if(tree[x].l==p&&tree[x].r==p){  
        tree[x].max=num;  
        return;  
    }  
    int mid=(tree[x].l+tree[x].r)/2;  
    if(p<=mid)  
        update(x*2,p,num);  
    else  
        update(x*2+1,p,num);  
    tree[x].max=maxx(tree[x*2].max,tree[x*2+1].max);  
}  
void query(int x,int l,int r){  
    if(tree[x].l==l&&tree[x].r==r){  
        ans=maxx(ans,tree[x].max);  
        return;  
    }  
    int mid=(tree[x].l+tree[x].r)/2;  
    if(l>mid)  
        query(x*2+1,l,r);  
    else if(r<=mid)  
        query(x*2,l,r);  
    else{  
        query(x*2,l,mid);  
        query(x*2+1,mid+1,r);  
    }  
}  
int main(){  
    int m,i,x,y;  
    char ch[5];  
    while(scanf("%d%d",&n,&m)!=EOF){  
        memset(a,0,sizeof(a));  
        for(i=1;i<=n ;i++)  
            scanf("%d",&a[i]);  
        create(1,1,n);  
        while(m--){  
            scanf("%s%d%d",ch,&x,&y);  
            if(ch[0]=='U'){  
                update(1,x,y);  
            }  
            if(ch[0]=='Q'){  
                ans=0;  
                query(1,x,y);  
                printf("%d\n",ans);  
            }  
        }  
    }  
    return 0;  
}  

 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,