Cod sursa(job #898128)

Utilizator Alex_Merceraaaaaaa Alex_Mercer Data 28 februarie 2013 01:28:08
Problema Arbori de intervale Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.25 kb
#include <cstdio>

using namespace std;

int arb[400000],x,a,b,n,m,i,poz,max;

void ins (int nod, int st, int dr)
{
    int m;
    m = (st+dr)/2;
    if (st == poz && dr == poz) arb[nod] = x;
    else
    {
        if (poz<=m) ins (2*nod,st,m);
        else ins (2*nod+1,m+1,dr);
        if (arb[2*nod]>arb[2*nod+1]) arb[nod] = arb[2*nod];
        else arb[nod] = arb[2*nod+1];
    }
}

int caut (int nod, int st, int dr)
{
    int p1, p2, m, mx;
    p1 = p2 = 0;
    if (a<=st && b>=dr) mx = arb[nod];
    else
    {
        m = (st+dr)/2;
        if (a<=m) p1 = caut(2*nod,st,m);
        if (b>m) p2 = caut(2*nod+1,m+1,dr);
        if (p1 > p2) mx = p1;
        else mx = p2;
    }
    return mx;
}

int main()
{
    freopen ("arbint.in","r",stdin);
    freopen ("arbint.out","w",stdout);

    scanf ("%d %d",&n,&m);

    for (i=1; i<=n; i++)
    {
        scanf ("%d",&x);
        poz = i;
        ins (1,1,n);
    }

    for (i=1; i<=m; i++)
    {
        scanf ("%d %d %d",&x,&a,&b);
        if (x == 0)
        {
            max = caut (1,1,n);
            printf ("%d\n", max);
        }
        else
        {
            poz = a;
            x = b;
            ins (1,1,n);
        }
    }

    return 0;
}