Cod sursa(job #2011352)

Utilizator dragomirmanuelDragomir Manuel dragomirmanuel Data 15 august 2017 21:28:11
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.52 kb
#include <iostream>
#include <cstdio>

using namespace std;

const int NMax = 500005;

int aib[NMax], v[NMax];
int t, x, y, maxi;

void Build(int st, int dr, int pai)
{
    if(st==dr)
    {
        aib[pai] = v[st];
        return;
    }

    int mij = (st+dr)/2;

    Build(st,mij,2*pai);
    Build(mij+1,dr,2*pai+1);

    aib[pai] = max(aib[2*pai], aib[2*pai+1]);
}

void Query(int st, int dr, int pai, int x, int y)
{
    if(x<=st && y>=dr)
    {
        maxi = max(aib[pai], maxi);
        return;
    }

    int mij = (st+dr)/2;

    if(x <= mij)
        Query(st,mij,2*pai,x,y);

    if(y > mij)
        Query(mij+1,dr,2*pai+1,x,y);
}

void Update(int st, int dr, int pai, int x, int y)
{
    if(st==dr)
    {
        aib[pai] = y;
        return;
    }

    int mij = (st+dr)/2;

    if(x<=mij)
        Update(st,mij,2*pai,x,y);

    else
        Update(mij+1,dr,2*pai+1,x,y);

    aib[pai] = max(aib[2*pai], aib[2*pai+1]);
}

int main()
{
    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);

    int N, M;

    scanf("%d %d", &N, &M);

    for(int i=1; i<=N; ++i)
        scanf("%d", &v[i]);

    Build(1,N,1);

    for(int i=1; i<=M; ++i)
    {
        int t, x, y;
        scanf("%d %d %d", &t, &x, &y);

        if(t==0)
        {
            maxi = -1;
            Query(1,N,1,x,y);
            cout << maxi << "\n";
        }

        else
        {
            Update(1,N,1,x,y);
        }
    }


        return 0;
}