Cod sursa(job #2010008)

Utilizator GeoeyMexicanuBadita George GeoeyMexicanu Data 11 august 2017 14:42:02
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <iostream>
#include <fstream>
#define N 100010

using namespace std;

ifstream f("arbint.in");
ofstream g("arbint.out");

int arb[4*N+66],i,j,m,max1,st,dr,mij,pos,x,start,finish,cd,a,b,n;
inline int Maxim(int a,int b)
{
    if(a>b)
        return a;
    else
        return b;
}
void update(int nod,int st,int dr)
{
    if(st==dr)
    {
        arb[nod]=x;
        return;
    }
    int mij=(st+dr)/2;
    if(pos<=mij)
        update(2*nod,st,mij);
    else
        update(2*nod+1,mij+1,dr);
    arb[nod]=Maxim(arb[2*nod],arb[2*nod+1]);
}
void query(int nod,int st,int dr)
{
    if(start<=st && dr<=finish)
    {
        if(max1<arb[nod])
            max1=arb[nod];
        return;
    }
    int mij=(st+dr)/2;
    if(start<=mij)
    {
        query(2*nod,st,mij);
    }
    if(mij<finish)
    {
        query(2*nod+1,mij+1,dr);
    }
}
int main()
{
    f>>n>>m;
    for(i=1;i<=n;i++)
    {
        f>>x;
        pos=i;
        update(1,1,n);
    }
    for(i=1;i<=m;i++)
    {
        f>>cd>>a>>b;
        if(cd==0)
        {
            max1=-1;
            start=a;
            finish=b;
            query(1,1,n);
            g<<max1<<"\n";
        }
        else
        {
            pos=a;
            x=b;
            update(1,1,n);
        }
    }
}