Cod sursa(job #1409987)

Utilizator pulseOvidiu Giorgi pulse Data 30 martie 2015 20:00:04
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.25 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int maxn = 100010;

int n, m, t, val, pos, a, b, x, y, best;
int arb[3 * maxn];

void updateArb(int node, int l, int r)
{
    if (l == r)
    {
        arb[node] = val;
        return;
    }
    int m = (l + r) >> 1;
    if (pos <= m)
        updateArb(2 * node, l, m);
    else
        updateArb(2 * node + 1, m + 1, r);

    arb[node] = max(arb[2 * node], arb[2 * node + 1]);
}

void queryArb(int node, int l, int r)
{
    if (l >= x && r <= y)
    {
        best = max(best, arb[node]);
        return;
    }
    int m = (l + r) >> 1;
    if (x <= m)
        queryArb(2 * node, l, m);
    if (m < y)
        queryArb(2 * node + 1, m + 1, r);
}

int main()
{
    fin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        fin >> x;
        val = x;
        pos = i;
        updateArb(1, 1, n);
    }
    for (; m; --m)
    {
        fin >> t >> a >> b;
        if (t == 1)
        {
            val = b;
            pos = a;
            updateArb(1, 1, n);
        }
        else
        {
            best = -1;
            x = a;
            y = b;
            queryArb(1, 1, n);
            fout << best << '\n';
        }
    }
    return 0;
}