Cod sursa(job #2025469)

Utilizator rangal3Tudor Anastasiei rangal3 Data 22 septembrie 2017 18:52:17
Problema Arbori de intervale Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.37 kb
#include <fstream>
#define max(a,b) (a > b ? a:b)
#define in "arbint.in"
#define out "arbint.out"
#define NOD 250005

using namespace std;

ifstream fin(in);
ofstream fout(out);

int n,m,p,a,b;
int pos,val;
int heap[NOD];
int Max;

inline void actualizare(int nod,int st,int dr)
{
    if(pos == st && pos == dr)
    {
        heap[nod] = val;
    }
    else
    {
        int mij = (st+dr)/2;
        if(pos >= st && pos <= mij) actualizare(2*nod,st,mij);
        if(pos > mij && pos <= dr) actualizare(2*nod+1,mij+1,dr);
        heap[nod] = max(heap[2*nod],heap[2*nod+1]);
    }
}

inline void DEI(int nod, int a,int b,int st, int dr)
{
    if( a<= st && dr <= b)
        Max = max(Max,heap[nod]);
    else
    {
        int mij = (st+dr)/2;
        if(a <= mij)
            DEI(2*nod,a,b,st,mij);
        if(b >= mij+1)
            DEI(2*nod+1,a,b,mij+1,dr);

    }
}

int main()
{
    fin>>n>>m;
    for(int i=1; i<=n; ++i)
    {
        fin>>val;
        pos = i;
        actualizare(1,1,n);
    }

    while(m--)
    {
        fin>>p>>a>>b;
        if(p == 0)
        {
            Max = 0;
            DEI(1,a,b,1,n);
            fout<<Max<<"\n";
        }
        else
        {
            pos = a;
            val = b;
            actualizare(1,1,n);
        }
    }



    fin.close(); fout.close();
    return 0;
}