hdu1754I Hate It(线段树)

来源:互联网 发布:mmd h 动作数据 编辑:程序博客网 时间:2024/06/14 08:59

题目链接:

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

好久没做线段树的题了,先来一道简单的练手,就是端点更新,区间最大值。。看了大神的代码,风格法很飘逸~

#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#define MAXN 222222#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;int MAX[MAXN<<2];void pushup(int rt){     MAX[rt]=max(MAX[rt<<1],MAX[rt<<1|1]);}void build(int l,int r,int rt){    if(l==r)    {        scanf("%d",&MAX[rt]);        return;    }    int m=(l+r)>>1;    build(lson);    build(rson);    pushup(rt);}void updata(int p,int sc,int l,int r,int rt){    if(l==r)    {        MAX[rt]=sc;        return;    }    int m=(l+r)>>1;    if(p<=m) updata(p,sc,lson);    else updata(p,sc,rson);    pushup(rt);}int  query(int L,int R,int l,int r,int rt){    int ret=0;    if(L<=l && r<=R)        return MAX[rt];    int m=(l+r)>>1;    if(L<=m)        ret=max(ret,query(L,R,lson));    if(R>m)        ret=max(ret,query(L,R,rson));    return ret;}int main() {    int n , m;    while (~scanf("%d%d",&n,&m)) {        build(1 , n , 1);        while (m --) {            char op[2];            int a , b;            scanf("%s%d%d",op,&a,&b);            if (op[0] == 'Q') printf("%d\n",query(a , b , 1 , n , 1));            else updata(a , b , 1 , n , 1);        }    }    return 0;}


0 0
原创粉丝点击