Cod sursa(job #1096876)

Utilizator Alex_Merceraaaaaaa Alex_Mercer Data 2 februarie 2014 18:08:50
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
#include <cstdio>
#include <algorithm>
#include <vector>

#define pb push_back
#define b begin()
#define e end

using namespace std;

vector<int> a;
int n,m,poz,q,x,y,i;

void adaug (int nod, int st, int dr, int pz, int val)
{
    int m;
    m = (st + dr)/2;

    if (pz == st && pz == dr) a.at(nod)=val;
    else
    {
        if (pz <= m) adaug(2*nod,st,m,pz,val);
            else adaug(2*nod+1,m+1,dr,pz,val);

        if ( a.at(2*nod+1) < a.at(2*nod) ) a.at(nod) = a.at(2*nod);
            else a.at(nod) = a.at(2*nod+1);
    }
}

int caut (int nod, int st, int dr, int de_unde, int pana_unde)
{
    if (de_unde <= st && pana_unde >=dr) return a.at(nod);
    else
    {
        int p1,p2,m;

        m = (st+dr)/2;
        p1 = p2 = 0;
        if (de_unde<=m) p1 = caut (2*nod,st,m,de_unde,pana_unde);
        if (pana_unde>m) p2 = caut (2*nod+1,m+1,dr,de_unde,pana_unde);

        if (p1>p2) return p1;
        return p2;
    }
}

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

    scanf ("%d %d",&n,&m);
    a.resize(2*n+2,0);

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

    for (i=1; i<=m; i++)
    {
        scanf ("%d %d %d",&q,&x,&y);
        if (q) adaug(1,1,n,x,y);
        else printf ("%d\n",caut (1,1,n,x,y));

    }

    return 0;
}