Cod sursa(job #2026255)

Utilizator rangal3Tudor Anastasiei rangal3 Data 24 septembrie 2017 08:47:34
Problema Arbori de intervale Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include <fstream>
#define in "arbint.in"
#define out "arbint.out"
#define N 100003

using namespace std;

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

int heap[2*N],n,m,p,a,b;
int pos,val;
int maxim;

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

inline void DEI(const int &nod,int st,int dr,const int &a,const int &b)
{
    if(a<=st && dr<=b)
        maxim = max(maxim,heap[nod]);
    else
    {
        int mij = st + (dr-st)/2;

        if(a <= mij) DEI(nod*2,st,mij,a,b);
        if(b > mij) DEI(nod*2+1,mij+1,dr,a,b);
    }
}

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)
        {
            maxim = 0;
            DEI(1,1,n,a,b);
            fout<<maxim<<"\n";
        }
        else
        {
            pos = a;
            val = b;
            actualizare(1,1,n);
        }
    }


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