Cod sursa(job #1483029)

Utilizator AdrianaMAdriana Moisil AdrianaM Data 8 septembrie 2015 16:00:24
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <fstream>
#include <vector>
using namespace std;

ifstream is("arbint.in");
ofstream os("arbint.out");

int n, m, val, poz;
int a, b, arb[2 << 17];

void Update(int nod, int st, int dr);
int Max(int nod, int st, int dr);

int main()
{
    is >> n >> m;
    for ( poz = 1; poz <= n; ++poz )
    {
        is >> val;
        Update(1, 1, n);
    }
    int tip, A, B;
    while ( m-- )
    {
        is >> tip >> A >> B;
        if ( tip )
        {
            poz = A;
            val = B;
            Update(1, 1, n);
        }
        else
        {
            a = A, b = B;
            os << Max(1, 1, n) << "\n";
        }
    }
    is.close();
    os.close();
    return 0;
}

int Max(int nod, int st, int dr)
{
    if ( a <= st && dr <= b )
        return arb[nod];
    int md = ( st + dr ) / 2, answ = 0;
    if ( a <= md )
        answ = Max(2 * nod, st, md);
    if ( md < b )
        answ = max(answ, Max(2 * nod + 1, md + 1, dr));
    return answ;
}

void Update(int nod, int st, int dr)
{
    if ( st == dr )
    {
        arb[nod] = val;
        return;
    }
    int md = ( st + dr ) / 2;
    if ( poz <= md )
        Update(2 * nod, st, md);
    else
        Update(2 * nod + 1, md + 1, dr);
    arb[nod] = max(arb[2 * nod], arb[2 * nod + 1]);
}