Cod sursa(job #1074851)

Utilizator Iustin_BulimarFMI Iustin Bulimar Iustin_Bulimar Data 8 ianuarie 2014 00:23:36
Problema Arbori de intervale Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include <fstream>
using namespace std;
ifstream cin("arbint.in");
ofstream cout("arbint.out");

const int n_max=1000001;

int n, m, i, v, vmax[n_max], t, a, b, mx;

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

void push(int p, int v, int nod, int st, int dr)
{
    int m;
    if(st==dr)
    {
        vmax[nod]=v;
        return;
    }
    m=(st+dr)/2;
    if(p<=m) push(p, v, 2*nod, st, m);
    else push(p, v, 2*nod+1, m+1, dr);
    vmax[nod]=max(vmax[2*nod], vmax[2*nod+1]);
}

void elmax(int a, int b, int nod, int st, int dr)
{
    int m;
    if(a<=st && dr<=b)
    {
        if(mx<vmax[nod]) mx=vmax[nod];
        return;
    }
    m=(st+dr)/2;
    if(a<=m) elmax(a, b, 2*nod, st, m);
    if(m<b) elmax(a, b, 2*nod+1, m+1, dr);
}
int main()
{
    cin>>n>>m;
    for(i=1; i<=n; i++)
    {
        cin>>v;
        push(i, v, 1, 1, n);
    }
    while(m--)
    {
        cin>>t>>a>>b;
        if(!t)
        {
            mx=-1;
            elmax(a, b, 1, 1, n);
            cout<<mx<<'\n';
        }
        else push(a, b, 1, 1, n);
    }
    return 0;
}