Cod sursa(job #3253722)

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

using namespace std;

ifstream fin("arbint.in");
ofstream fout("arbint.out");
typedef int 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) {
        fin >> 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() {
    fin >> n >> q;
    Build(1, 1, n);
    
    while(q--) {
        int op, x, y;
        fin >> op;
        if(op == 1) {
            fin >> x >> y;
            Update(1, 1, n, x, y);
        }
        else {
            fin >> x >> y;
            fout << Query(1, 1, n, x, y) << "\n";
        }
    }
    
    return 0;
}