Cod sursa(job #1284277)

Utilizator hanganflorinHangan Florin hanganflorin Data 6 decembrie 2014 13:44:55
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include <fstream>
#define MAX 100000
using namespace std;

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

int arb[4*MAX];
int n, m, op, poz, A, B, mx;

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

int main()
{
    is >> n >> m;
    for ( int i = 1; i <= n; ++i )
    {
        is >> B;
        A = i;
        Update(1, 1, n);
    }
    while(m--)
    {
        is >> op >> A >> B;
        if ( op )
            Update(1, 1, n);
        else
        {
            mx = -1;
            Query(1, 1, n);
            os << mx << '\n';
        }

    }
    is.close();
    os.close();
    return 0;
}
void Update(int nod, int st, int dr )
{
    // A - pozitie
    // B - valoare
    if (st == dr )
    {
        arb[nod] = B;
        return;
    }
    int mid = (st+dr)/2;
    if ( A <= mid )
        Update(2*nod, st, mid);
    else
        Update(2*nod+1, mid+1, dr );
    arb[nod] = max( arb[2*nod], arb[2*nod+1] );
}
void Query(int nod, int st, int dr )
{
    // A - pozitie
    // B - pozitie
    if ( A <= st && dr <= B )
    {
        mx = max(mx, arb[nod] );
        return;
    }
    int mid = (st+dr)/2;
    if ( mid >= A )
        Query(2*nod, st, mid);
    if ( mid < B )
        Query(2*nod+1, mid+1, dr);
}