Cod sursa(job #3299515)

Utilizator Bolbotina_David-AndreiBolbotina David-Andrei Bolbotina_David-Andrei Data 7 iunie 2025 17:07:42
Problema Arbori de intervale Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.93 kb
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <fstream>
using namespace std;

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

int vect[100000] = {0}, tree[400000] = {0};

void arbint(int nod, int st, int dr){
    if (st==dr){
        tree[nod]=vect[st];
        return;
    }
    int mij = (st + dr)/2;
    arbint(2 * nod, st, mij);
    arbint(2 * nod + 1, mij + 1, dr);
    if (tree[2 * nod]>tree[2 * nod + 1])
        tree[nod] = tree[2 * nod];
    else
        tree[nod] = tree[2 * nod + 1];
}

int maximul(int nod, int st, int dr, int x, int y){
    if (y < st || dr < x)
        return 0;
    if (x <= st && dr <= y)
        return tree[nod];
    int mij = (st + dr) / 2;
    int a = maximul(2 * nod, st, mij, x, y);
    int b = maximul(2 * nod + 1, mij + 1, dr, x, y);
    if (a >b)
        return a;
    else
        return b;
}

void schimba(int nod, int st, int dr, int x, int y){
    if (st == dr){
        tree[nod] = y;
        return;
    }
    int mij = (st + dr)/2;
    if (x <= mij)
        schimba(2 * nod, st, mij, x, y);
    else
        schimba(2 * nod + 1, mij + 1, dr, x, y);
    if (tree[2 * nod] > tree[2 * nod + 1])
        tree[nod] = tree[2 * nod];
    else
        tree[nod] = tree[2 * nod + 1];
}

int main(){
    
    int m,n,nr,x,y;
    fin>>n>>m;
    for (int i=1; i<=n; i++)
        fin >> vect[i];
    arbint(1, 1, n); 
    for (int i=0; i<m; i++)
    {
        fin>>nr>>x>>y;
        if (nr == 0)
            fout<<maximul(1, 1, n, x, y)<<"\n"; 
        else
            schimba(1, 1, n, x, y);
    }
    return 0;
}