Cod sursa(job #2622183)

Utilizator Zamfirescuste2Zamfirescu Stefan Zamfirescuste2 Data 31 mai 2020 16:57:05
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.37 kb
#include <bits/stdc++.h>

const int MAX = 1e5 + 2;

int n, m;
int arb[4*MAX];
int value, poz, max1, x, y;

void find_max(int root, int l, int r);

void change_max(int root, int l, int r);

int main(){

    std :: ifstream f("arbint.in");
    std :: ofstream g("arbint.out");

    f >> n >> m;
    for (int i = 1; i <= n; ++i){
        f >> value;
        poz = i;
        change_max(1, 1, n);
    }

    int a, b, op;
    for (int i=0;i < m; ++i){

        f>> op >> a >> b;

        if (op == 0){
            x = a;
            y = b;
            max1 = 0;
            find_max(1,1,n);
            g << max1 << "\n";
        }
        else{
            poz = a;
            value=b;
            change_max(1,1,n);
        }
    }
}

void find_max(int root, int l, int r){

    if (x <= l && r <= y){
        if (arb[root] >= max1)
            max1 = arb[root];
        return;
    }
    
    int mid = l + (r - l) / 2;
    if (x <= mid)
        find_max(2 * root, l, mid);
    if (mid + 1 <= y)
        find_max(2 * root + 1, mid + 1, r);
}

void change_max(int root, int l, int r){
    
    if (l == r){
        arb[root] = value;
        return;
    }
    
    int mid = (l + r) / 2;
    if (poz <= mid)
        change_max(2 * root, l, mid);
    else
        change_max(2 * root + 1, mid + 1, r);
    
    arb[root] = std :: max(arb[2 * root], arb[2 * root + 1]); // setare tata ca max al fiilor
}