Cod sursa(job #2484300)

Utilizator FrostfireMagirescu Tudor Frostfire Data 30 octombrie 2019 21:41:26
Problema Arbori de intervale Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.38 kb
#include <fstream>
#include <iostream>
#define NMAX 100000

using namespace std;

ifstream f("arbint.in");
ofstream g("arbint.out");

int n, q, v[NMAX+10], aint[2*NMAX+10];

void build()
{
    for(int i=n; i<2*n; i++) aint[i] = v[i-n+1];
    for(int i=n-1; i; i--) aint[i] = max(aint[2*i], aint[2*i+1]);
}

void update(int poz, int val)
{
    int nod = poz + n - 1;
    aint[nod] = val;
    nod = nod >> 1;
    while(nod)
        {   aint[nod] = max(aint[2*nod], aint[2*nod+1]);
            nod = nod >> 1;
        }
}

int query(int nod, int stnod, int drnod, int stq, int drq)
{
    if(stq <= stnod && drnod <= drq) return aint[nod];
    int maxi = 0;
    if(stq <= (stnod + drnod) / 2) maxi = max(maxi, query(2*nod, stnod, (stnod+drnod)/2, stq, drq));
    if(drq > (stnod + drnod) / 2) maxi = max(maxi, query(2*nod+1, (stnod+drnod)/2+1, drnod, stq, drq));
    return maxi;

}

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

    //make n a power of 2
    if(n & (n-1))
        {   int lastOne = 0;
            for(int i=0; i<=18; i++)
                if(n & (1<<i)) lastOne = i;
            n = 1<<(lastOne+1);
        }

    build();

    while(q--)
        {   int type, a, b;
            f >> type >> a >> b;
            if(type) update(a, b);
            else g << query(1, 1, n, a, b) << '\n';
        }

    return 0;
}