Cod sursa(job #2088512)

Utilizator BovisioNitica Ionut Bogdan Bovisio Data 15 decembrie 2017 13:43:29
Problema Arbori de intervale Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.53 kb
#include <cstdio>

using namespace std;

int n,m,A[10001],arb[10001];

int Max(int a,int b)
{
    if(a > b)
        return a;
    return b;
}

void Create(int L,int H,int pos)
{
    if(L == H)
    {
        arb[pos]=A[L];
        return;
    }
    int M = (L+H)/2;
    Create(L,M,pos*2);
    Create(M+1,H,pos*2+1);
    arb[pos] = Max(arb[pos*2],arb[pos*2+1]);
}

void Update(int Node,int L,int H,int pos,int Val)
{
    if(L == H)
    {
        arb[Node]=Val;
    }
    else
    {
        int M = (L+H)/2;
        if(pos <= M)
            Update(Node*2,L,M,pos,Val);
        else
            Update(Node*2+1,M+1,H,pos,Val);
        arb[Node] = Max(arb[Node*2],arb[Node*2+1]);
    }
}

int Querry(int L,int H,int Lq,int Hq,int pos)
{
    int M,R1,R2;
    if(Lq <= L && H <= Hq)
        return arb[pos];
    M = (L+H)/2;
    if(Lq <= M)
        R1 = Querry(L,M,Lq,Hq,pos*2);
    else
        R1 = -1;
    if(M < Hq)
        R2 = Querry(M+1,H,Lq,Hq,pos*2+1);
    else
        R2 = -1;
    return Max(R1,R2);
}

void Read()
{
    int a,b,co;
    freopen("arbint.in","r",stdin);
    freopen("arbint.out","w",stdout);
    scanf("%i %i",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%i",&A[i]);
    Create(1,n,1);
    for(int i=1;i<=m;i++)
    {
        scanf("%i %i %i",&co,&a,&b);
        if(co == 0)
        {
            printf("%i\n",Querry(1,n,a,b,1));
        }
        else
        {
            Update(1,1,n,a,b);
        }

    }
}

int main()
{
    Read();
    return 0;
}