Cod sursa(job #2115303)

Utilizator AndreiMironMiron Andrei AndreiMiron Data 26 ianuarie 2018 16:53:03
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 kb
#include <fstream>

using namespace std;
ifstream cin("arbint.in");
ofstream cout("arbint.out");
const int MAX = 1e5 + 1;
int v[MAX], n, m, arb[3 * MAX], a, b;
void update(int nod, int st, int dr, int poz) {
    if(st == dr) {
        arb[nod] = v[st];
    } else {
        int mij = (st + dr) / 2;
        if(poz == 0 or (st <= poz and poz <= mij)) {
            update(nod * 2, st, mij, poz);
        }
        if(poz == 0 or (mij + 1 <= poz and poz <= dr)) {
            update(nod * 2 + 1, mij + 1, dr, poz);
        }
        arb[nod] = arb[nod * 2];
        if(arb[nod * 2 + 1] > arb[nod]) {
            arb[nod] = arb[nod * 2 + 1];
        }
    }
}
int query(int nod, int st, int dr) {
    if(a <= st && dr <= b) {
        return arb[nod];
    } else {
        int mij = (st + dr) / 2;
        int rezs = 0;
        if(a <= mij) {
            rezs = query(nod * 2, st, mij);
        }
        int rezd = 0;
        if(mij + 1 <= b) {
            rezd = query(nod * 2 + 1, mij + 1, dr);
        }
        if(rezs > rezd) {
            return rezs;
        } else {
            return rezd;
        }
    }
}
int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; ++i)
        cin >> v[i];

    update(1, 1, n, 0);
    for(int i = 1; i <= m; ++i) {
        int cod;
        cin >> cod >> a >> b;
        if(cod == 0) {
            cout << query(1, 1, n) << '\n';
        } else {
            v[a] = b;
            update(1, 1, n, a);
        }
    }
    return 0;
}