Cod sursa(job #2034250)

Utilizator vasilicamoldovanVasilica Moldovan vasilicamoldovan Data 7 octombrie 2017 17:21:11
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
#include <fstream>
#include <algorithm>
using namespace std;
const int NMAX = 100000;
int aint[4 * NMAX + 5];
int n, m, x, a, b, q, ans, poz;


void update(int node, int st, int dr);
void query(int node, int st, int dr);


int main()
{
    ifstream fin("arbint.in");
    ofstream fout("arbint.out");
    fin >> n >> m;
    for(poz = 1; poz <= n; ++poz)
    {
        fin >> x;
        update(1, 1, n);
    }


    for(int i = 1; i <= m; ++i)
    {
        fin >> q >> a >> b;
        if(q == 0)
        {
            ans = 0;
            query(1, 1, n);
            fout << ans << "\n";
        }
        else
        {
			poz = a, x = b;
            update(1, 1, n);
        }
    }
    return 0;
}


void update(int node, int st, int dr)
{
    if(st == dr)
    {
        aint[node] = x;
        return;
    }
    int med = (st + dr) / 2;
    if(poz <= med)
        update(node * 2, st, med);
    else
        update(node * 2 + 1, med + 1, dr);

    aint[node] = max(aint[node * 2], aint[node * 2 + 1]);
}

void query(int node, int st, int dr)
{
    if(a <= st && dr <= b)
    {
        ans = max(ans, aint[node]);
        return;
    }
    if(st == dr)
        return;
    int med = (st + dr) / 2;
    if(a <= med)
        query(node * 2, st, med);
    if(b > med)
        query(node * 2 + 1, med + 1, dr);

}