Cod sursa(job #2201178)

Utilizator andreigeorge08Sandu Ciorba andreigeorge08 Data 3 mai 2018 20:22:35
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.44 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("arbint.in");
ofstream fout("arbint.out");

int n, q, a[100005], aint[400005];
void Citire()
{
    fin >> n >> q;
    for(int i = 1; i <= n; i++) fin >> a[i];
}

void Build(int nod, int x, int y)
{
    if(x == y)
    {
        aint[nod] = a[x];
        return;
    }

    int mij = (x + y) / 2;

    Build(2 * nod, x, mij);
    Build(2 * nod + 1, mij + 1, y);

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

void Update(int nod, int x, int y, int D, int V)
{
    if(x == y)
    {
        aint[nod] = V;
        return;
    }

    int mij = (x + y) / 2;

    if(D <= mij)
        Update(2 * nod, x, mij, D, V);
    else Update(2 * nod + 1, mij + 1, y, D, V);

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


int Query(int nod, int x, int y, int a, int b)
{
    if(x == a && y == b)
        return aint[nod];

    int mij = (x + y) / 2;

    if(b <= mij)
        return Query(2 * nod, x, mij, a, b);
    else if(a > mij)
        return Query(2 * nod + 1, mij + 1, y, a, b);
    else return max(Query(2 * nod, x, mij, a, mij), Query(2 * nod + 1, mij + 1, y, mij + 1, b));
}

int main()
{
    Citire();
    Build(1, 1, n);

    int a, b, op;
    while(q--)
    {
        fin >> op >> a >> b;
        if(op == 0)
            fout << Query(1, 1, n, a, b) << "\n";
        else Update(1, 1, n, a, b);
    }
    return 0;
}