Cod sursa(job #1968066)

Utilizator TibixbAndrei Tiberiu Tibixb Data 17 aprilie 2017 14:17:00
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.55 kb
#include<fstream>
#define NMAX 100005
using namespace std;
ifstream _cin("arbint.in");
ofstream _cout("arbint.out");
int A[4 * NMAX];
int n, m, op, a, b, qs;
int x[NMAX];
void build(int st, int dr, int nod)
{
    if(st == dr)
    {
        A[nod] = x[st];
        return;
    }

    int mij = st + (dr - st) / 2;

    build(st, mij, 2 * nod);

    build(mij + 1, dr, 2 * nod + 1);

    A[nod] = max(A[2 * nod], A[2 * nod + 1]);
}

void update(int st, int dr, int nod, int poz, int upd)
{
    if(st == dr)
    {
        A[nod] = upd;
        return;
    }

    int mij = st + (dr - st) / 2;
    if(poz <= mij)
    {
        update(st, mij, 2 * nod, poz, upd);
    }else
    {
        update(mij + 1, dr, 2 * nod + 1, poz, upd);
    }

    A[nod] = max(A[2 * nod], A[2 * nod + 1]);
}

void query(int st, int dr, int nod, int a, int b)
{
    if(a <= st && dr <= b)
    {
        qs = max(qs, A[nod]);
        return;
    }

    int mij = st + (dr - st) / 2;
    if(a <= mij)
    {
        query(st, mij, 2 * nod, a, b);
    }
    if(b > mij)
    {
        query(mij + 1, dr, 2 * nod + 1, a, b);
    }
}

int main()
{
    _cin >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        _cin >> x[i];
    }

    build(1, n, 1);

    for(int i = 1; i <= m; i++)
    {
        _cin >> op >> a >> b;
        if(op == 0)
        {
            qs = 0;
            query(1, n, 1, a, b);
            _cout << qs << "\n";
        }else
        {
            update(1, n, 1, a, b);
        }
    }
    return 0;
}