Cod sursa(job #2623442)

Utilizator hirneagabrielHirnea Gabriel hirneagabriel Data 3 iunie 2020 10:42:37
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.5 kb
#include <iostream>
#include <fstream>

using namespace std;
ifstream fin("arbint.in");
ofstream fout("arbint.out");

int n, m, val, pos, max, st, dr;
int Arbore[4 * 100002];

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

void update(int nod, int stanga, int dreapta)
{
    if (stanga == dreapta)
    {
        Arbore[nod] = val;
        return;
    }
    int mij = (stanga + dreapta) / 2;
    if (pos <= mij)
        update(2 * nod, stanga, mij);
    else
        update(2 * nod + 1, mij + 1, dreapta);
    Arbore[nod] = maxim(Arbore[2 * nod], Arbore[2 * nod + 1]);
}

void query(int nod, int stanga, int dreapta)
{
    if (st <= stanga && dreapta <= dr)
    {
        if (max < Arbore[nod])
            max = Arbore[nod];
        return;
    }
    int mij = (stanga + dreapta) / 2;
    if (st <= mij)
        query(2 * nod, stanga, mij);
    if (mij < dr)
        query(2 * nod + 1, mij + 1, dreapta);
}

int main()
{
    int a,b,c;
    fin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        cin >> a;
        pos = i;
        val = a;
        update(1, 1, n);
    }
    for (int i = 1; i <= m; i++)
    {
        fin >> a >> b >> c;
        if (a == 0)
        {
            max = -1;
            st = b;
            dr = c;
            query(1, 1, n);
            fout << max;
        }
        else
        {
            pos = b;
            val = c;
            update(1, 1, n);
        }
    }
   
    return 0;
}