HDU 1754 I Hate It:区间树 单点更新

之前做过 区间更新的 所以这个做起来要容易些 算是线段树的水题吧

都是基本操作 直接贴代码

#include<iostream>
#include<cstdio>
#define Size 200000
using namespace std;
struct node
{
        int L, R, Score;
        int Max;
        int Mid()
        {
                return (L+R)/2;
        }
}Tree[Size*3];

void Build( int root, int L, int R )
{
        Tree[root].L = L;
        Tree[root].R = R;
        Tree[root].Max = 0;

        if( L==R )
            return ;
        Build( root*2+1, L, (L+R)/2 );
        Build( root*2+2, (L+R)/2+1, R );
}

void Insert( int root, int i, int a )
{
        if( Tree[root].L == Tree[root].R )
        {
                Tree[root].Score = a;
                Tree[root].Max = a;
                return ;
        }
        Tree[root].Max = max( Tree[root].Max, a );
        if( i<=Tree[root].Mid() )
            Insert( root*2+1, i, a );
        else
            Insert( root*2+2, i, a );
}

int Query( int root, int a, int b )
{
        if( Tree[root].L == a && Tree[root].R == b )
                return Tree[root].Max;

        if( b<=Tree[root].Mid() )
            return Query( root*2+1, a, b );
        else
            if( a>Tree[root].Mid() )
            return Query( root*2+2, a, b );
        else
            return max( Query( root*2+1, a, Tree[root].Mid() ), Query( root*2+2, Tree[root].Mid()+1, b ) );
}

int main()
{
        int N, M, A, B;
        char S[10];
        while( cin>>N>>M )
        {
                Build( 0, 1, N );
                for( int i=1; i<=N; i++ )
                {
                        scanf( "%d", &A );
                        Insert( 0, i, A );
                }
                for( int i=0; i<M; i++ )
                {
                        scanf( "%s%d%d", &S, &A, &B );
                        if( S[0]=='Q' )
                            printf("%d\n",Query( 0, A, B ));
                        else
                             Insert( 0, A, B );
                }
        }
        return 0;
}

 

你可能感兴趣的:(HDU)