Cod sursa(job #3253720)

Utilizator Mihai_OctMihai Octavian Mihai_Oct Data 4 noiembrie 2024 16:41:18
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.58 kb
#include <bits/stdc++.h>

using namespace std;

typedef long long Nod;
int n, i, q;
Nod a[400002];

const Nod NEUTRU = LLONG_MAX;
static inline Nod Calc(Nod a, Nod b) {
    return max(a, b);
}

static inline void Build(int nod, int st, int dr) {
    if(st == dr) {
        cin >> a[nod];
        return;
    }
    
    int mij = st + (dr - st) / 2;
    Build(2 * nod    , st     , mij);
    Build(2 * nod + 1, mij + 1, dr);
    
    a[nod] = Calc(a[2 * nod], a[2 * nod + 1]);
}

static inline void Update(int nod, int st, int dr, int poz, int val) {
    if(st == dr) {
        a[nod] = val;
        return;
    }
    
    int mij = st + (dr - st) / 2;
    if(poz <= mij) Update(2 * nod    , st     , mij, poz, val);
    if(mij <  poz) Update(2 * nod + 1, mij + 1, dr , poz, val);
    
    a[nod] = Calc(a[2 * nod], a[2 * nod + 1]);
}

static inline Nod Query(int nod, int st, int dr, int qst, int qdr) {
    if(qst <= st && dr <= qdr) return a[nod];
    
    int mij = st + (dr - st) / 2;
    
    Nod q1 = NEUTRU;
    Nod q2 = NEUTRU;
    
    if(qst <= mij) q1 = Query(2 * nod    , st     , mij, qst, qdr);
    if(mij <  qdr) q2 = Query(2 * nod + 1, mij + 1, dr , qst, qdr);
    
    return Calc(q1, q2);
}

int main() {
    cin >> n >> q;
    Build(1, 1, n);
    
    while(q--) {
        int op, x, y;
        cin >> op;
        if(op == 1) {
            cin >> x >> y;
            Update(1, 1, n, x, y);
        }
        else {
            cin >> x >> y;
            cout << Query(1, 1, n, x, y) << "\n";
        }
    }
    
    return 0;
}