Cod sursa(job #1724137)

Utilizator crysstyanIacob Paul Cristian crysstyan Data 2 iulie 2016 13:29:59
Problema Arbori de intervale Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.44 kb
#include <fstream>
#include <vector>
#include <algorithm>
#define NMAX 100005

using namespace std;

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

int i, n, nrquery, v[NMAX], Left_query, Right_query, value_query, index, Ans, Type;

void Update(int Left, int Right, int Node)
{
    if (Left == Right)
    {
        v[Node] = value_query;
        return;
    }

    int Mid = (Left + Right) / 2;

    if (index <= Mid)
        Update(Left, Mid, 2 * Node);
    else
        Update(Mid + 1, Right, 2 * Node + 1);

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

void Query(int Left, int Right, int Node)
{
    if (Left_query <= Left && Right <= Right_query)
    {
        Ans = max(Ans, v[Node]);
        return;
    }

    int Mid = (Left + Right) / 2;

    if (Left_query <= Mid)
        Query(Left, Mid, 2 * Node);

    if (Mid < Right_query)
        Query(Mid + 1, Right, 2 * Node + 1);
}

int main()
{
    f >> n >> nrquery;

    for (i = 1; i <= n; ++ i)
    {
        f >> value_query;
        index = i;
        Update(1, n, 1);
    }

    while (nrquery --)
    {
        f >> Type;

        if (Type == 0)
        {
            f >> Left_query >> Right_query;
            Ans = -1;
            Query(1, n, 1);
            g << Ans << '\n';
        }

        if (Type == 1)
        {
            f >> index >> value_query;
            Update(1, n, 1);
        }
    }
    return 0;
}