hdu 1754 I Hate It (线段树版)

来源:互联网 发布:h5制作软件 编辑:程序博客网 时间:2024/06/08 05:12

题目链接:点击打开链接


读入的时候因为要先输入字符C,所以要用getchar()和输入时的%*c处理掉空格,很烦,不按字符输入而是%s输入字符串可避免这个问题


代码:

#include <iostream>#include <cstdio>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define MAX 200010int a[MAX<<2];void pushup(int rt){    a[rt]=max(a[rt<<1],a[rt<<1|1]);}void build(int l,int r,int rt){    if(l==r){            scanf("%d",&a[rt]);            return;    }    else a[rt]=0;    int m=(l+r)>>1;    build(lson);    build(rson);    pushup(rt);}void update(int p,int n,int l,int r,int rt){    if(l==r){        a[rt]=n;        return ;    }    int m=(l+r)>>1;    if(p<=m)update(p,n,lson);    else update(p,n,rson);    pushup(rt);}int query(int L,int R,int l,int r,int rt){    if(L<=l&&R>=r){        return a[rt];    }    int m=(l+r)>>1;    int res=0;    if(L<=m)res=max(res,query(L,R,lson));    if(R>m)res=max(res,query(L,R,rson));    return res;}int main(){    int n,m;    while(~scanf("%d%d%*c",&n,&m)){        build(1,n,1);        getchar();        for(int i=1;i<=m;i++){                char t;                int s,e;                scanf("%c%d%d%*c",&t,&s,&e);                if(t=='Q'){                    printf("%d\n",query(s,e,1,n,1));                }                else update(s,e,1,n,1);        }    }    return 0;}


0 0