Cod sursa(job #2506565)

Utilizator FrostfireMagirescu Tudor Frostfire Data 8 decembrie 2019 13:36:15
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.32 kb
#include <fstream>
#include <iostream>

using namespace std;

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

int n, q, aint[(1<<18)+10], v[(1<<17)+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)
{   poz = poz + n - 1;
    aint[poz] = val;
    poz /= 2;
    while(poz)
        {   aint[poz] = max(aint[poz*2], aint[poz*2+1]);
            poz /= 2;
        }
}

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];
    if(n & (n-1))
        {   int lastOne = 0;
            for(int i=0; (1<<i)<=n; i++)
                if(n & (1 << i)) lastOne = i+1;
            n = (1 << lastOne);
        }
    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;
}