Cod sursa(job #2488074)

Utilizator mareadevarIonescu Andrei mareadevar Data 6 noiembrie 2019 09:17:26
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.25 kb
#include <bits/stdc++.h>

#define MAXN 100005

using namespace std;

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

int v[MAXN * 4], N;

inline void Update(int poz, int ls, int ld, int p, int val)
{
    if (ls == ld && ls == p)
    {
        v[poz] = val;
        return;
    }
    if (ls > p || ld < p)
        return;
    int mij = (ls + ld) / 2;

    Update(poz * 2, ls, mij, p, val);
    Update(poz * 2 + 1, mij + 1, ld, p, val);

    v[poz] = max(v[poz * 2], v[poz * 2 + 1]);
}

inline int Query(int poz, int ls, int ld, int st, int dr)
{
    if (st <= ls && ld <= dr)
        return v [poz];
    if (st > ld || dr < ls)
        return 0;
    int mij = (ls + ld) / 2;

    int aux1 = Query(poz * 2, ls, mij, st, dr);
    int aux2 = Query(poz * 2 + 1, mij + 1, ld, st, dr);

    return max(aux1, aux2);
}



int main ()
{
    int M, a, b, tip;

    fin >> N >> M;

    for (int i = 1; i <= N; i++)
    {
        fin >> a;

        Update(1, 1, N, i, a);
    }

    while (M--)
    {
        fin >> tip >>a >> b;

        if (tip == 0)
        {
            fout << Query(1, 1, N, a, b) << "\n";
        }
        else
        {
            Update(1, 1, N, a, b);
        }
    }

    return 0;
}