Cod sursa(job #1920604)

Utilizator TibixbAndrei Tiberiu Tibixb Data 10 martie 2017 08:38:48
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.57 kb
#include<fstream>
#include<vector>
#define NMAX 100000
using namespace std;
int A[4 * NMAX + 5];
int x[NMAX + 5];
int n, m, op, a, b, qa;
vector<int> G[NMAX];
ifstream cin("arbint.in");
ofstream cout("arbint.out");
void _build(int st, int dr, int nod)
{
    if(st == dr)
    {
        A[nod] = x[st];
        return;
    }
    int mij = st + (dr - st) / 2;

    _build(st, mij, 2 * nod);

    _build(mij + 1, dr, 2 * nod + 1);

    A[nod] = max(A[2 * nod], A[2 * nod + 1]);
}
void _query(int st, int dr, int nod, int a, int b)
{
    if(a <= st && dr <= b)
    {
        qa = max(A[nod], qa);
        return;
    }
    int mij = st + (dr - st) / 2;
    if(a <= mij)
    {
        _query(st, mij, 2 * nod, a, b);
    }
    if(b > mij)
    {
        _query(mij + 1, dr, 2 * nod + 1, a, b);
    }
}
void _update(int st, int dr, int nod, int poz, int na)
{
    if(st == dr)
    {
        A[nod] = na;
        return;
    }
    int mij = st + (dr - st) / 2;
    if(poz <= mij)
    {
        _update(st, mij, 2 * nod, poz, na);
    }
    else
    {
        _update(mij + 1, dr, 2 * nod + 1, poz, na);
    }
    A[nod] = max(A[2 * nod], A[2 * nod + 1]);
}
int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        cin >> x[i];
    }
    _build(1, n, 1);
    while(m--)
    {
        cin >> op >> a >> b;
        if(op == 0)
        {
            qa = 0;
            _query(1, n , 1, a, b);
            cout << qa << "\n";
        }
        else
        {
            _update(1, n, 1, a, b);
        }
    }
}