Cod sursa(job #3128293)

Utilizator RMTomaRican Mihai Toma RMToma Data 9 mai 2023 10:17:26
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.31 kb
#include <bits/stdc++.h>

const int NMax = 400020;
using namespace std;
int arbore[NMax];
int x, poz, maxim, stop, inceput;
ifstream in("arbint.in");
ofstream out("arbint.out");
void upd(int nod, int stanga, int dreapta){
    if(stanga == dreapta){
        arbore[nod] = x;
        return;
    }
    int mij = (stanga + dreapta)/2;
    if( poz <= mij ){
        upd(2*nod,stanga,mij);
    } else {
        upd(2*nod+1,mij+1,dreapta);
    }
         arbore[nod] = max( arbore[2*nod], arbore[2*nod+1] );
}
void quer(int nod, int stanga, int dreapta){
    if(inceput <= stanga && dreapta <= stop){
        if(maxim < arbore[nod]){
            maxim = arbore[nod];
        }
        return;
    }
   
    int mij = (stanga + dreapta)/2;
    if(inceput<=mij){
        quer(2*nod, stanga, mij);
    }
    if (mij < stop){
        quer(2*nod+1, mij+1, dreapta);
    }
}
int main(){
    int n,y,z,k;
    in >> n >> k;
    for(int i=1;i<=n;i++){
        in >> x;
        poz = i;
        upd(1,1,n);
    }
    
    for(int i=1;i<=k;i++){
        in >> y;
        int a, b;
        in >> a >> b;
        if(y==0){
            maxim = 0;
            inceput = a;
            stop = b;
            quer(1,1,n);
            out << maxim <<"\n";
        } else {
            poz = a;
            x = b;
            upd(1,1,n);
        }
    }
    
}