Cod sursa(job #541178)

Utilizator ssergiussSergiu-Ioan Ungur ssergiuss Data 24 februarie 2011 21:22:10
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <algorithm>
#include <fstream>

using namespace std;

const char Input[] = "arbint.in";
const char Output[] = "arbint.out";

const int Dim = 100001;

int N, M;
int mx, ai[Dim << 2];

void Upd( int nod, int st, int dr, int x, int y ) {

    int fs, fd, mj;

    if( st == dr ) {

        ai[nod] = y;
        return;
    }

    fs = nod << 1;
    fd = (nod << 1) | 1;
    mj = (st + dr) / 2;

    if( x <= mj )
        Upd( fs, st, mj, x, y );
    else
        Upd( fd, mj + 1, dr, x, y );

    ai[nod] = max( ai[fs], ai[fd] );
}

void Que( int nod, int st, int dr, int x, int y ) {

    int fs, fd, mj;

    if( x <= st && dr <= y ) {

        mx = max( mx, ai[nod] );
        return;
    }

    fs = nod << 1;
    fd = (nod << 1) | 1;
    mj = (st + dr) / 2;
    mx = 0;

    if( x <= mj )
        Que( fs, st, mj, x, y );
    if( y > mj )
        Que( fd, mj + 1, dr, x, y );
}

int main() {

    ifstream fin( Input );
    ofstream fout( Output );

    int i, x, y, z;

    fin >> N >> M;
    for( i = 1; i <= N; ++i ) {

        fin >> x;
        Upd( 1, 1, N, i, x );
    }
    while( M-- ) {

        fin >> x >> y >> z;

        if( x == 0 ) {

            mx = 0;
            Que( 1, 1, N, y, z );
            fout << mx << "\n";
        }
        else
            Upd( 1, 1, N, y, z );
    }

    fin.close();
    fout.close();

    return 0;
}