Cod sursa(job #2903546)

Utilizator AntoniaPopoviciAntonia-Adelina Popovici AntoniaPopovici Data 17 mai 2022 17:54:14
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <iostream>
#include <fstream>
#define MAXIM 500000
using namespace std;

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

int n, m, val, poz, opt, maxx, i, v[MAXIM], start, fini;

void query(int nod, int st, int dr)
{
    if (start <= st && fini >= dr)
    {
        if (maxx < v[nod]) maxx = v[nod];
        return;
    }
    int mij = (st + dr) / 2;
    if (start <= mij)
        query(2 * nod, st, mij);
    if (mij < fini)
        query(2 * nod + 1, mij + 1, dr);
}

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

int main()
{   
    fin >> n >> m;
    for (i = 0; i < n; i++) {
        fin >> val;
        poz = i + 1;
        update(1, 1, n);
    }
    for (i = 0; i < m; i++)
    {
        fin >> opt;
        fin >> poz >> val;
        if (opt == 0)
        {
            maxx = 0;
            query(1, 1, n);
            fout << maxx << '\n';
        }
        else
            update(1, 1, n);
    }
    return 0;
}