Cod sursa(job #3230358)

Utilizator alexxiacrisanCrisan Maria - Alexia alexxiacrisan Data 20 mai 2024 20:09:54
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int pom[400005];

void update(int nod, int left, int right, int poz, int val)
{
    if(left == right)
    {
        pom[nod] = val;
        return;
    }

    int mij = (left + right) / 2;

    if(poz <= mij)
        update(2 * nod, left, mij, poz, val);
    else
        update(2 * nod + 1, mij + 1, right, poz, val);

    pom[nod] = max(pom[2 * nod], pom[2 * nod + 1]);
}

int determinare_maxim(int nod, int left, int right, int x, int y)
{
    int max1 = 0, max2 = 0;
    if(x <= left && right <= y)
        return pom[nod];

    int mij = (left + right) / 2;

    if(x <= mij)
        max1 = determinare_maxim(2 * nod, left, mij, x, y);

    if(y > mij)
        max2 = determinare_maxim(2 * nod + 1, mij + 1, right, x, y);

    return max(max1, max2);
}

int main()
{
    int n, m, operatie, a, b, i, aux;
    fin>>n>>m;

    for(i=1; i <= n; i++)
    {
        fin>>aux;
        update(1, 1, n, i, aux);
    }

    for(i=1; i <= m; i++)
    {
        fin>>operatie>>a>>b;
        if(operatie == 0)
            fout<<determinare_maxim(1, 1, n, a, b)<<endl;
        else if(operatie == 1)
            update(1, 1, n, a, b);
    }
    return 0;
}