Cod sursa(job #967641)

Utilizator costin7856Antonesi Florean Costin costin7856 Data 28 iunie 2013 10:47:08
Problema Arbori de intervale Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include<fstream>
#include<algorithm>
#define dim 800009
using namespace std;
long n,m,i,j,a[dim],x,A,B,tip;
void schimba(long nod,long st,long dr, long poz, long val)
{
    long mij;
    if(st==dr)
        a[nod]=val;
    else
    {
        mij=(st+dr)/2;
        if(poz<=mij)
            schimba(2*nod,st,mij,poz,val);
        else
            if(poz>mij)
                schimba(2*nod+1,mij+1,dr,poz,val);
            a[nod]=max(a[2*nod],a[2*nod+1]);
    }

}
void detmax(long nod,long st,long dr, long A, long B)
{
    long mij;

    if(A<=st&&B>=dr)
    {
        if(a[nod]>x)
        x=a[nod];
        return;
    }
    if(st<dr)
    {
        mij=(st+dr)/2;
        if(A<=mij)
            detmax(2*nod,st,mij,A,B);
            if(mij<B)
            detmax(2*nod+1,mij+1,dr,A,B);
    }
}
int main()
{
    ifstream f("arbint.in");
    ofstream g("arbint.out");
    f>>n>>m;
    for(i=1;i<=n;++i)
    {
        f>>x;
        schimba(1,1,n,i,x);
    }

    for(i=1;i<=m;++i)
    {
        f>>tip>>A>>B;
        if(tip==1)
            schimba(1,1,n,A,B);
        else
        {
            x=0;
            detmax(1,1,n,A,B);
           g<<x<<"\n";
        }
    }
}