Cod sursa(job #1292725)

Utilizator Stefex09Stefan Teodorescu Stefex09 Data 14 decembrie 2014 18:21:53
Problema Arbori de intervale Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.36 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int AINT[131073];

inline int max2 (const int &A, const int &B)
{
    if (A > B)
        return A;

    return B;
}

void update (const int &nod, const int &st, const int &dr, const int &val, const int &poz)
{
    if (st == dr){
        AINT[nod] = val;
        return;
    }

    int mid = (st + dr) / 2;

    if (poz <= mid)
        update (nod * 2, st, mid, val, poz);
    else
        update (nod * 2 + 1, mid + 1, dr, val, poz);

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

int query (const int &nod, const int &st, const int &dr, const int &a, const int &b)
{
    if (st == dr)
        return AINT[nod];

    int mid = (st + dr) /  2;
    int mx1 = 0, mx2 = 0;

    if (a <= mid)
        mx1 = query (nod * 2, st, mid, a, b);
    if (mid < b)
        mx2 = query (nod * 2 + 1, mid + 1, dr, a, b);

    return max2 (mx1, mx2);
}

int main()
{
    int N, M, i, op, a, b;

    in >> N >> M;
    for (i = 1; i <= N; i ++){
        in >> a;
        update (1, 1, N, a, i);
    }

    for (i = 1; i <= M; i ++){
        in >> op >> a >> b;

        if (op == 0)
            out << query (1, 1, N, a, b) << "\n";
        else
            update (1, 1, N, b, a);
    }

    return 0;
}