Cod sursa(job #1783769)

Utilizator Theodor1000Cristea Theodor Stefan Theodor1000 Data 19 octombrie 2016 14:06:14
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include <cstdio>
#include <algorithm>

using namespace std;

int arb[200010], n;

inline void build ()
{
    for (int i = n - 1; i; --i)
        arb[i] = max (arb[(i << 1)], arb[(i << 1) + 1]);
}

inline void update (int poz, int val)
{
    arb[poz += n - 1] = val;
    for (poz >>= 1; poz; poz >>= 1)
        arb[poz] = max (arb[poz << 1], arb[(poz << 1) + 1]);
}

inline int querry (int st, int dr)
{
    int rez = -1;
    for (st += n - 1, dr += n; st < dr; st >>= 1, dr >>= 1)
    {
        if (st & 1) rez = max (rez, arb[st++]);
        if (dr & 1) rez = max (rez, arb[--dr]);
    }

    return rez;
}

int main ()
{
    freopen ("arbint.in", "r", stdin);
    freopen ("arbint.out", "w", stdout);

    int m;
    scanf ("%d %d", &n, &m);

    for (int i = 0; i < n; ++i)
        scanf ("%d", &arb[i + n]);

    build ();

    for (int i = 1; i <= m; ++i)
    {
        int op, x, y;
        scanf ("%d %d %d", &op, &x, &y);

        if (!op) printf ("%d\n", querry (x, y));
        else update (x, y);
    }

    return 0;
}