Cod sursa(job #1454211)

Utilizator CollermanAndrei Amariei Collerman Data 25 iunie 2015 19:03:52
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.54 kb
#include<fstream>
using namespace std;
ifstream fin("arbint.in");
ofstream fout("arbint.out");
const int MMAX = 100005;

int n, m, a, b, operatie;
int arbore[MMAX * 3];

void construire_arbore(int nod, int st, int dr)
{
    if(st == dr) {
        fin >> arbore[nod];
        return;
    }
    else {
        int mij = st + (dr - st) / 2;
        construire_arbore(nod * 2, st, mij);
        construire_arbore(nod * 2 + 1, mij + 1, dr);
        arbore[nod] = max(arbore[nod * 2], arbore[nod * 2 + 1]);
    }
}

int query_arbore(int nod, int st, int dr)
{
    if(st >= a && b >= dr) return arbore[nod];
    else {
        int Max = 0;
        int mij = st + (dr - st) / 2;

        if(a <= mij) Max = max(query_arbore(nod * 2, st, mij), Max);
        if(b > mij) Max = max(query_arbore(nod * 2 + 1, mij + 1, dr), Max);
        return Max;
    }
}

void actualizare_arbore(int nod, int st, int dr)
{
    if(st >= a && a >= dr) {
         arbore[nod] = b;
         return;
    }
    else {
        int mij = st + (dr - st) / 2;
        if(a <= mij) actualizare_arbore(nod * 2, st, mij);
        else actualizare_arbore(nod * 2 + 1, mij + 1, dr);
        arbore[nod] = max(arbore[nod * 2], arbore[nod * 2 + 1]);
    }
}

int main()
{
    fin >> n >> m;
    construire_arbore(1, 1, n);

    for(int i=1; i<=m; i++) {
        fin >> operatie >> a >> b;

        if(!operatie) fout << query_arbore(1, 1, n) << '\n';
        else actualizare_arbore(1, 1, n);
    }

    fin.close();
    fout.close();
    return 0;
}