Cod sursa(job #1501332)

Utilizator andreiblaj17Andrei Blaj andreiblaj17 Data 13 octombrie 2015 11:55:53
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.48 kb
#include <iostream>
#include <fstream>

#define nmax 100001

using namespace std;

ifstream fi("arbint.in");
ofstream fo("arbint.out");

int n, m, type, pos, val, maxim, a, b;
int ARB[4*nmax];

void update(int nod, int pos, int val, int st, int dr)
{
    if (st == dr)
    {
        ARB[nod] = val;
        return;
    }
    int mid = (st + dr) >> 1;
    if (pos <= mid) update(2*nod, pos, val, st, mid);
    else            update(2*nod+1, pos, val, mid+1, dr);
    ARB[nod] = max(ARB[2*nod], ARB[2*nod+1]);
}

void read()
{
    fi >> n >> m;
    for (int pos = 1; pos <= n; pos++)
    {
        fi >> val;
        update(1, pos, val, 1, n);
    }
}

void query(int nod, int st, int dr, int a, int b)
{

    if (a <= st && dr <= b)
    {
        maxim = max(maxim, ARB[nod]);
        return;
    }
    int mid = (st + dr) >> 1;
    if (a <= mid) query(2*nod, st, mid, a, b);
    if (b > mid) query(2*nod+1, mid+1, dr, a, b);
}

void solve()
{
    for (int i = 1; i <= m; i++)
    {
        fi >> type;
        if (!type)
        {
            // maximul pe intervalul [a, b]
            fi >> a >> b;

            maxim = -1;
            query(1, 1, n, a, b);
            fo << maxim << "\n";
        }
        else
        {
            // valoarea elementului de pe pozitia pos va deveni val
            fi >> pos >> val;
            update(1, pos, val, 1, n);
        }
    }
}

int main()
{

    read();
    solve();

    return 0;
}