Cod sursa(job #1315732)

Utilizator sherban26FMI Mateescu Serban-Corneliu sherban26 Data 13 ianuarie 2015 00:58:08
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.66 kb
#include <iostream>
#include <fstream>

#define NMAX 1<<18

using namespace std;

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

int n, m, val, poz, v[NMAX];
int valMax, boundL, boundR;

void update(int nod, int l, int r)
{
    if (l == r)
    {
        //nod terminal
        v[nod] = val;
    }
    else
    {
        int mid = (l + r) / 2;

        //coboara in arbore
        if (poz <= mid)
            update(nod * 2, l, mid);
        else
            update(nod * 2 + 1, mid + 1, r);

        //set interval max
        v[nod] = max(v[nod * 2], v[nod * 2 + 1]);
    }
}

void query(int nod, int l, int r)
{
    if (boundL <= l && boundR >= r)
    {
        //no need to check deeper
        if (v[nod] > valMax)
            valMax = v[nod];
    }
    else
    {
        int mid = (l + r) / 2;

        if (boundR <= mid)
            query(nod * 2, l, mid);
        if (boundL >= mid)
            query(nod * 2 + 1, mid + 1, r);
    }
}

int main()
{
    fin >> n >> m;

    //
    // citire + insertie
    //
    for (int i = 1; i <= n; ++i)
    {
        fin >> val;

        poz = i;
        update(1, 1, n);
    }

    //
    // operatii
    //
    int op, a, b;

    for (int i = 0; i < m; ++i)
    {
        fin >> op >> a >> b;

        if (op == 0)
        {
            //query
            valMax = -1;
            boundL = a;
            boundR = b;

            query(1, 1, n);
            fout << valMax << "\n";
        }
        else
        {
            //update
            poz = a;
            val = b;

            update(1, 1, n);
        }
    }
    return 0;
}