Cod sursa(job #1520626)

Utilizator PletoPletosu Cosmin-Andrei Pleto Data 9 noiembrie 2015 09:33:29
Problema Arbori de intervale Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include <fstream>
#include <algorithm>

using namespace std;

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

int N,M,AI[300010];

void update(int nod, int st, int dr, int poz, int val)
{
    if(st==dr)
    {
        AI[nod]=val;
        return;
    }
    else
    {
        int m=(st+dr)/2;
        if(poz<=m) update(2*nod,st,m,poz,val);
        else update(2*nod+1,m+1,dr,poz,val);
        AI[nod]=max(AI[2*nod],AI[2*nod+1]);
    }
}

void query(int nod, int st, int dr, int a, int b, int &sol)
{
    if(st==dr)
    {
        if(sol<AI[nod])
            sol=AI[nod];
        return ;
    }
    else
    {
        int m=(st+dr)/2;
        if(a<=m) query(2*nod,st,m,a,b,sol);
        if(b>m) query(2*nod+1,m+1,dr,a,b,sol);
    }
}

int main()
{
    in>>N>>M;
    for(int i=1;i<=N;i++)
    {
        int c;
        in>>c;
        update(1,1,N,i,c);
    }
    for(int i=1;i<=M;i++)
    {
        int T,A,B;
        in>>T;
        if(!T)
        {
            in>>A>>B;
            int sol=0;
            query(1,1,N,A,B,sol);
            out<<sol<<'\n';
        }
        if(T)
        {
            in>>A>>B;
            update(1,1,N,A,B);
        }
    }
    return 0;
}