Cod sursa(job #2565666)

Utilizator betybety bety bety Data 2 martie 2020 15:52:41
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.38 kb
#include <iostream>

#include <fstream>

using namespace std;



ifstream fin("arbint.in");

ofstream fout("arbint.out");



const int NMAX = 100009;



const int INF = 1<<31-1;



int v[NMAX];

int tree[4*NMAX];



int n,m;



void Update(int nod, int lf, int rg, int pos, int val)

{

    if(lf == rg)

    {

        tree[nod] = val;

        return;

    }

    int mid = lf + (rg-lf)/2;

    if(pos <= mid)

        Update(nod*2, lf, mid, pos, val);

    else Update(nod*2+1, mid+1, rg, pos, val);

    tree[nod] = max( tree[nod*2], tree[nod*2+1] );

}



int Query(int nod, int lf, int rg, int L, int R)

{

    if(L<=lf && rg<= R)

    {

        return tree[nod];

    }

    int mid = lf + (rg-lf)/2;

    int maxx = -2;

    if(L <= mid)

        maxx = max( maxx, Query(nod*2, lf, mid, L, R) );

    if(mid < R)    maxx = max(maxx,Query(nod*2+1, mid+1, rg, L, R) );

    return maxx;



}



void Read()

{

    int i,j,a,b,x;

    fin>>n>>m;

    for(i=1; i<=n; ++i)

    {

        fin>>x;

        Update(1,1,n,i,x);

    }

    for(i=1; i<=m; ++i)

    {

        fin>>x>>a>>b;

        if( x == 0 )

        {

            fout<<Query(1,1,n,a,b)<<"\n";

        }

        else Update(1,1,n,a,b);

    }



}



int main()

{

    Read();

    return 0;

}