线段树_HDU_1754

来源:互联网 发布:阜宁县网络发言人平台 编辑:程序博客网 时间:2024/06/12 01:18

前面是区间求和,这里是区间求最大值,写着写着,上一个疑惑就解决了

#include<iostream>#include<cstring>#include<cstdio>#define oo 0x3f3f3f3fconst int maxn = 2000010;using namespace std;int tree[maxn<<2];void Max(int t){    int ls = tree[t<<1];    int rs = tree[t<<1|1];    tree[t] = ls>rs?ls:rs;}void build(int l, int r,int rt){    if(l == r)    {        scanf("%d",&tree[rt]);        return;    }    int mid = (l+r)>>1;    build(l,mid,rt<<1);    build(mid+1,r,rt<<1|1);    Max(rt);}void updata(int a, int b,int l,int r,int rt){    if(l == r)        tree[rt] = b;    else    {        int mid = (l+r)>>1;        if(a<=mid)            updata(a,b,l,mid,rt<<1);        else            updata(a,b,mid+1,r,rt<<1|1);        Max(rt);    }}int query(int a, int b, int l, int r, int rt){    if(a<=l&&r<=b)return tree[rt];    int mid = (l+r)>>1;    int ans = -oo;    if(a<=mid)        ans = max(ans,query(a,b,l,mid,rt<<1));    if(b>mid)        ans = max(ans,query(a,b,mid+1,r,rt<<1|1));    return ans;}int main(){    int n,m,a,b;    char s[10];    while(scanf("%d%d",&n,&m)!=EOF)    {        build(1,n,1);        while(m--)        {            scanf("%s",s);            scanf("%d%d",&a,&b);            if(s[0]=='Q')                printf("%d\n",query(a,b,1,n,1));            else                updata(a,b,1,n,1);        }    }    return 0;}
0 0