Cod sursa(job #2124838)

Utilizator TudorVersoiuVersoiu Tudor Sorin TudorVersoiu Data 7 februarie 2018 17:28:26
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.01 kb
#include <iostream>
#include <fstream>
#include <climits>


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

struct NODE
{
    int x;
    int y;

    int vmax;

    NODE* parent;
    NODE* p_left;
    NODE* p_right;
} root;

int N, M, v[100003];
NODE* frunze[100003];

void BuildTree(NODE& nod, int first, int last)
{
    nod.x = first;
    nod.y = last;

    if ( first >= last )
    {
        frunze[first] = &nod;
        nod.vmax = v[first];

        nod.p_left  = NULL;
        nod.p_right = NULL;
    } else {
        int m = (first+last)/2;

        nod.p_left  = new NODE;
        nod.p_right = new NODE;

        nod.p_left ->parent = &nod;
        nod.p_right->parent = &nod;


        BuildTree(*nod.p_left , first,    m);
        BuildTree(*nod.p_right,   m+1, last);
    }
    if ( nod.p_left  != NULL ) nod.vmax = max(nod.vmax, nod.p_left->vmax);
    if ( nod.p_right != NULL ) nod.vmax = max(nod.vmax, nod.p_right->vmax);
}

int MaxValue(NODE& nod, int first, int last)
{
    if ( first <= nod.x && nod.y <= last )
        return nod.vmax;

    int maxx = -1;
    if ( first <= nod.p_left->y )
        maxx = max(maxx, MaxValue(*nod.p_left, first, nod.p_left->y));

    if ( last >= nod.p_right->x )
        maxx = max(maxx, MaxValue(*nod.p_right, nod.p_right->x, last));

    return maxx;
}
void Update(NODE* nod, int x)
{
    nod->vmax = x;

    if ( nod->p_left  != NULL ) nod->vmax = max(nod->vmax, nod->p_left->vmax);
    if ( nod->p_right != NULL ) nod->vmax = max(nod->vmax, nod->p_right->vmax);

    if ( nod->parent != NULL ) Update(nod->parent, x);
}

int main()
{
    f >> N >> M;


    for ( int i=1; i<=N; i++ )
        f >> v[i];

    BuildTree(root, 1, N);

    int OpType = 0, a, b;
    for ( int i=1; i<=M; i++ )
    {
        f >> OpType >> a >> b;

        if ( OpType == 0 )
            g << MaxValue(root, a, b) << '\n';
        else
        {

            Update(frunze[a], b);
        }
    }
}