Cod sursa(job #2174671)

Utilizator dragomirmanuelDragomir Manuel dragomirmanuel Data 16 martie 2018 12:57:14
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 = 100000;
int v[NMax], N, M;
int a,b,x,y;
int aib[4*NMax];

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]);
}

int maxi = -1;

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

    int mij = (st+dr)/2;

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

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

void Delete(int st, int dr, int pai)
{
    if(st==dr && st==a)
    {
        aib[pai] = b;
        return;
    }

    int mij = (st+dr)/2;

    if(mij >= a)
        Delete(st,mij,2*pai);

    else
        Delete(mij+1,dr,2*pai+1);

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

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

    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;
        scanf("%d",&t);
        if(t==0)
        {
            maxi = -1;
            scanf("%d%d",&x,&y);
            Query(1,N,1);
            cout << maxi << "\n";
        }

        else
        {
            scanf("%d%d",&a,&b);
            Delete(1,N,1);
        }
    }
    return 0;
}