Cod sursa(job #1487262)

Utilizator tudormaximTudor Maxim tudormaxim Data 16 septembrie 2015 16:30:05
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include <fstream>
using namespace std;
ifstream fin("arbint.in");
ofstream fout("arbint.out");
int sz=1, arb[262145];

void build()
{
    for(int nod=sz-1; nod; nod--)
        arb[nod]=max(arb[nod<<1], arb[(nod<<1)|1]);
}

void update(int poz, int val)
{
    int nod=poz+sz-1;
    arb[nod]=val;
    for(nod=nod>>1; nod; nod=nod>>1)
        arb[nod]=max(arb[nod<<1], arb[(nod<<1)|1]);
}

int query(int st, int dr)
{
    int rez=-1;
    for(st=st+sz-1, dr=dr+sz-1; st<=dr; st=st>>1, dr=dr>>1)
    {
        if(st&1) rez=max(rez, arb[st++]);
        if(!(dr&1)) rez=max(rez, arb[dr--]);
    }
    return rez;
}

int main()
{
    int n, m, i, type, a, b;
    fin >> n >> m;
    while(sz<=n) sz=(sz<<1);
    for(i=1; i<=n; i++)
        fin >> arb[i+sz-1];
    build();
    while(m--)
    {
        fin >> type >> a >> b;
        if(type == 0) fout << query(a, b) << '\n';
        else update(a, b);
    }
    fin.close();
    fout.close();
    return 0;
}