HDU 1754 线段树单点更新 区间最值

来源:互联网 发布:华为v8连上数据不能上 编辑:程序博客网 时间:2024/05/20 23:37
<pre name="code" class="cpp">#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int MAXN = 200001;struct{    int l,r,m;}nod[MAXN * 4];///总线段长度为MAXN 数组一般开到总长度的4倍int a[MAXN];void build(int t,int l,int r){    nod[t].l = l;    nod[t].r = r;    if(l == r)    {        nod[t].m = a[l];        return ;    }    int mid = (l + r) / 2;    build(t << 1,l,mid);    build(t << 1|1,mid+1,r);    nod[t].m = max(nod[t << 1].m,nod[t << 1|1].m);}void update(int t,int n,int v){    if(nod[t].l == nod[t].r && nod[t].l == n)    {        nod[t].m = v;        return ;    }    int mid = (nod[t].l + nod[t].r) / 2;    if(n <= mid)        update(t << 1,n,v);    else        update(t << 1|1,n,v);    nod[t].m = max(nod[t << 1].m,nod[t << 1|1].m);}int query(int t,int L,int R){    if(L == nod[t].l && R == nod[t].r)        return nod[t].m;    int s;    if(R <= nod[t << 1].r) s = query(t << 1,L,R);///若所查找区间在左儿子    else if(L >= nod[t << 1|1].l) s = query(t << 1|1,L,R);///若所查找区间在右儿子    else s = max(query(t << 1,L,nod[t << 1].r),query(t << 1|1,nod[t << 1|1].l,R));    ///所查找区间横跨左右儿子区间        return s;}int main(){    int n,m,i,x1,x2;    char s[2];    while(~scanf("%d%d",&n,&m))    {        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);        }        build(1,1,n);        while(m--)        {            scanf("%s%d%d",s,&x1,&x2);            if(s[0]=='Q')                printf("%d\n",query(1,x1,x2));            else                update(1,x1,x2);        }    }    return 0;}


                                             
0 0
原创粉丝点击