Cod sursa(job #2800836)

Utilizator sinai2008Belu Ianis sinai2008 Data 14 noiembrie 2021 08:48:25
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.69 kb
#include <iostream>
#include <fstream>

using namespace std;


#ifdef MY_MAC
ifstream f("input.txt");
#define g cout
#else
ifstream f(".in");
ofstream g(".out");
#endif


const int mxN = 1e5 + 5;

struct elem {
    int mx;
};

int n, q;
int a[mxN];
elem arb[4 * mxN];

elem comb(elem A, elem B) {
    elem aux;
    aux.mx = max(A.mx, B.mx);
    return aux;
}

void build(int st, int dr, int nod) {
    if (st == dr) {
        arb[nod].mx = a[st];
        return ;
    }

    int mij = (st + dr) / 2;

    build(st, mij, nod * 2);
    build(mij + 1, dr, nod * 2 + 1);
        
    arb[nod] = comb(arb[nod * 2], arb[nod * 2 + 1]);
}

void update(int pos, int val, int st, int dr, int nod) {
    if (st == dr && st == pos) {
        arb[nod].mx = val;
        return ;
    }

    int mij = (st + dr) / 2;

    if (pos <= mij) {
        update(pos, val, st, mij, nod * 2);
    } else {
        update(pos, val, mij + 1, dr, nod * 2 + 1);
    }

    arb[nod] = comb(arb[nod * 2], arb[nod * 2 + 1]);
}

elem query(int x, int y, int st, int dr, int nod) {
    if (x <= st && dr <= y) return arb[nod];

    int mij = (st + dr) / 2;
    if (x <= mij && mij + 1 <= y) 
        return comb(query(x, y, st, mij, nod * 2), query(x, y, mij + 1, dr, nod * 2 + 1));

    if (x <= mij) 
        return query(x, y, st, mij, nod * 2);

    return query(x, y, mij + 1, dr, nod * 2 + 1);
}

int query(int x, int y) {
    return query(x, y, 1, n, 1).mx;
}

int main() {
    f >> n >> q;
    for (int i = 1; i <= n ; ++i)
		f >> a[i];
    
    build(1, n, 1);

    for (int i = 1; i <= q ; ++i) {
        int tip, x, y;
        f >> tip >> x >> y;

        if (tip == 0) g << query(x, y) << '\n';
        else update(x, y, 1, n, 1);
    }

    return 0;
}