Cod sursa(job #1900203)

Utilizator nicu_serteSerte Nicu nicu_serte Data 3 martie 2017 10:52:56
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <fstream>
using namespace std;
#define max(a, b) ((a>b)?a:b)
#define nmax 100001
ifstream fin("arbint.in");
ofstream fout("arbint.out");
int n, arb[4*nmax+11], maxim;
void update(int nod, int st, int dr, int poz, int x)
{
    if(st==dr)
    {
        arb[nod]=x;
        return;
    }
    int m=(st+dr)/2;
    if(poz<=m)
        update(2*nod, st, m, poz, x);
    else update(2*nod+1, m+1, dr, poz, x);
    arb[nod]=max(arb[2*nod], arb[2*nod+1]);
}
void query(int nod, int st, int dr, int start, int stop)
{
    if(st>=start && dr<=stop)
    {
        if(arb[nod]>maxim)
            maxim=arb[nod];
        return;
    }
    int m=(st+dr)/2;
    if(m>=start)
        query(2*nod, st, m, start, stop);
    if(m<stop)
        query(2*nod+1, m+1, dr, start, stop);
    arb[nod]=max(arb[2*nod], arb[2*nod+1]);
}
int main()
{
    int m, i, x, y, op;
    fin>>n>>m;
    for(i=1; i<=n; i++)
    {
        fin>>x;
        update(1, 1, n, i, x);
    }
    for(i=1; i<=m; i++)
    {
        fin>>op>>x>>y;
        if(op==1)
            update(1, 1, n, x, y);
        else
        {
            maxim=-1;
            query(1, 1, n, x, y);
            fout<<maxim<<'\n';
        }
    }
    return 0;
}