Cod sursa(job #1188278)

Utilizator Stefanescu_MihaiStefanescu Mihai-Nicolae Stefanescu_Mihai Data 19 mai 2014 11:05:29
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.51 kb
#include <fstream>

using namespace std;

#define MaxN 100050

int arb[MaxN*4];

int pos, val, pos1, pos2;

int max2( int a, int b )
{
    if ( a > b )
        return a;
    return b;
}

void Update( int st, int dr, int nod )
{
    if ( st == dr )
        arb[nod] = val;
    else
    {
        int mid = ( st + dr ) >> 1;

        if ( mid < pos )
            Update( mid+1, dr, ( nod << 1 ) + 1 );
        else
            Update( st, mid, nod << 1 );

        arb[nod] = max2( arb[ nod << 1 ], arb[ ( nod << 1 ) + 1 ] );
    }
}

void Query( int st, int dr, int nod )
{
    if ( ( st >= pos1 ) && ( dr <= pos2 ) )
    {
        if ( arb[nod] > val )
            val = arb[nod];
    }
    else
    {
        int mid = ( st + dr ) >> 1;

        if ( mid >= pos1 )
            Query( st, mid, nod << 1 );
        if ( mid < pos2 )
            Query( mid+1, dr, ( nod << 1 ) + 1 );
    }
}

int main()
{
    ifstream f1( "arbint.in" );
    ofstream f2( "arbint.out" );

    int n, m, i;

    f1 >> n >> m;

    for ( pos = 1; pos <= n; ++pos )
    {
        f1 >> val;
        Update( 1, n, 1 );
    }

    short c;

    for ( i = 0; i < m; ++i )
    {
        f1 >> c;

        if ( c )
        {
            f1 >> pos >> val;
            Update( 1, n, 1 );
        }
        else
        {
            f1 >> pos1 >> pos2;
            Query( 1, n, 1 );
            f2 << val << '\n';
        }
    }


    f1.close();
    f2.close();

    return 0;
}