Cod sursa(job #1614275)

Utilizator japjappedulapPotra Vlad japjappedulap Data 25 februarie 2016 21:25:24
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.41 kb
#include <fstream>
#include <vector>
using namespace std;

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

#define cint const int

cint Nmax = 100001;

int N, M;


int Arb[Nmax<<2];

void Insert(int L, int R, int Node, cint Val, cint Pos);

int Best;
void Query(int L, int R, int Node, cint x, cint y);

int main()
{
    is >> N >> M;
    for (int i = 1, x; i <= N; ++i)
    {
        is >> x;
        Insert(1, N, 1, x, i);
    }
    for (int op, x, y; M; --M)
    {
        is >> op >> x >> y;
        if (op == 1)
            Insert(1, N, 1, y, x);
        else
        {
            Best = 0;
            Query(1, N, 1, x, y);
            os << Best << '\n';
        }
    }

    is.close();
    os.close();
}

void Query(int L, int R, int Node, cint x, cint y)
{
    if (x <= L && R <= y)
        Best = max(Best, Arb[Node]);
    else
    {
        int M = (L + R) / 2;
        if (x <= M)
            Query(L, M, Node*2, x, y);
        if (M < y)
        Query(M+1, R, Node*2 + 1, x, y);
    }

};

void Insert(int L, int R, int Node, cint Val, cint Pos)
{
    if (L == R)
        Arb[Node] = Val;
    else
    {
        int M = (L + R) / 2;
        if (Pos <= M)   Insert(L, M, Node*2, Val, Pos);
        else            Insert(M+1, R, Node*2 + 1, Val, Pos);
        Arb[Node] = max(Arb[2*Node], Arb[2*Node+1]);
    }
};