Cod sursa(job #1758191)

Utilizator B_RazvanBaboiu Razvan B_Razvan Data 16 septembrie 2016 19:15:27
Problema Arbori de intervale Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.4 kb
#include <iostream>
#include <cstdio>

using namespace std;

int n, m, v[1000000], a, b, ai[1000000], nr, c, sol;

void citire()
{
    cin>>n>>m;
    for(int i=1; i<=n; ++i)
        cin>>v[i];
}
void build(int poz, int st, int dr)
{
    //nr++;
    if(st==dr)
    {
        ai[poz]=v[st];
        return;
    }
    int mij=(st+dr)/2;
    build(2*poz, st, mij);
    build(2*poz+1, mij+1, dr);
    ai[poz]=max(ai[2*poz], ai[2*poz+1]);
}

void maximInterval(int poz, int st, int dr)
{
    if(a<=st && b>=dr)
    {
        sol=max(sol, ai[poz]);
        return;
    }
    int mij=(st+dr)/2;
    if(mij>=a)
        maximInterval(2*poz, st, mij);
    if(mij<b)
        maximInterval(2*poz+1, mij+1, dr);
}

void schimbare(int poz, int st, int dr)
{
    if(st==dr)
    {
        ai[poz]=b;
        return;
    }
    int mij=(st+dr)/2;
    if(a<=mij)
        schimbare(2*poz, st, mij);
    else
        schimbare(2*poz+1, mij+1, dr);
    ai[poz]=max(ai[2*poz], ai[2*poz+1]);
}

int main()
{
    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);
    citire();
    build(1, 1, n);
    for(int i=1; i<=m; ++i)
    {
        cin>>c>>a>>b;
        if(c==0)
            {
                sol=0;
                maximInterval(1, 1, n);
                cout<<sol<<"\n";
            }
        else
            schimbare(1, 1, n);
    }
    return 0;
}