HDU1754 I Hate It 线段树单点更新

HDU1754 I Hate It

/*线段树单点更新
967MS	3580K
*/
#include <stdio.h>
#include <algorithm>
using namespace std;
const int MAX=2*1e5+5;
int tree[MAX*4];
void read(int &x)//读入优化
{
    int f=1;x=0;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    x*=f;
}
void pushup (int root)//向上更新
{
    tree[root]=max(tree[root*2],tree[root*2+1]);
}
void build (int root,int left,int right)//建树
{
    if (left==right)
    {
        read(tree[root]);
        return;
    }
    int mid=(left+right)/2;
    build (root*2,left,mid);
    build (root*2+1,mid+1,right);
    pushup(root);
}
void update (int root,int left,int right,int pos,int val)//单点更新最值
{
    if (left==right)
    {
        tree[root]=val;
        return;
    }
    int mid=(right+left)/2;
    if (pos<=mid) update(root*2,left,mid,pos,val);
    else update(root*2+1,mid+1,right,pos,val);
    pushup(root);
}
int query (int root,int left,int right,int L,int R)//区间查询最值
{
    if (left>=L&&right<=R)
        return tree[root];
    int mid=(left+right)/2,max_=0;
    if (L<=mid) max_=max(max_,query(root*2,left,mid,L,R));
    if (R>mid) max_=max(max_,query(root*2+1,mid+1,right,L,R));
    return max_;
}
int main()
{
    int n,m;
    while (~scanf("%d%d",&n,&m))
    {
        int x,y;
        char a;
        build (1,1,n);
        for (int k=0;k<m;k++)
        {
            a=getchar();
            read(x);read(y);
            if (a=='U')
                update(1,1,n,x,y);
            else if (a=='Q')
                printf ("%d\n",query(1,1,n,x,y));
        }
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/nrtostp/article/details/80148794