Cod sursa(job #1328005)

Utilizator radu_uniculeu sunt radu radu_unicul Data 27 ianuarie 2015 19:51:12
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include <fstream>

using namespace std;

#define NMAX 100005

ifstream fin ("arbint.in");
ofstream fout ("arbint.out");

int a[4*NMAX];
int n, m;

void update(int st, int dr, int poz, int nod, int val);
int query(int st, int dr, int poz1, int poz2, int nod);

int main()
{
    int x, y, c;
    fin >> n >> m;
    for (int i = 1; i <= n; ++i)
    {
        fin >> x;
        update(1, n, i, 1, x);
    }
    for (int i = 1; i <= m; ++i)
    {
        fin >> c >> x >> y;
        if (c == 0) fout << query(1, n, x, y, 1) << '\n';
        else update(1, n, x, 1, y);
    }

    return 0;
}

void update(int st, int dr, int poz, int nod, int val)
{
    int mij =(st + dr)>>1;
    if (st < dr)
    {
        if (mij >= poz) update(st, mij, poz, 2*nod, val);
            else update(mij+1, dr, poz, 2*nod+1, val);
        a[nod] = max(a[2*nod], a[2*nod+1]);
    }
    else a[nod] = val;


}
int query(int st, int dr, int poz1, int poz2, int nod)
{
    if (poz1 <= st && dr <= poz2) return a[nod];

    int mij = (st + dr) >> 1;
    int x = 0, y = 0;
    if (poz1 <= mij) x = query(st, mij, poz1, poz2, 2*nod);
    if (poz2 > mij)  y = query(mij+1, dr, poz1, poz2, 2*nod+1);
    return max(x, y);
}