Cod sursa(job #1617877)

Utilizator tudormaximTudor Maxim tudormaxim Data 27 februarie 2016 16:56:47
Problema Arbori de intervale Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream fin("arbint.in");
ofstream fout("arbint.out");

const int nmax = 100005;
int dim=1, arb[nmax<<1];

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

void query(int st, int dr)
{
    int maxx=0, nr;
    st=st+dim-1;
    dr=dr+dim-1;
    while(st<=dr)
    {
        nr=max(arb[st], arb[dr]);
        maxx=max(maxx, nr);
        st=(st+1)>>1;
        dr=(dr-1)>>1;
    }
    fout << maxx << "\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    int n, m, i, type, a, b;
    fin >> n >> m;
    while(dim<=n)
        dim=dim<<1;
    for(i=1; i<=n; i++)
        fin >> arb[i+dim-1];
    while(m--)
    {
        fin >> type >> a >> b;
        if(type == 0) query(a, b);
        else update(a, b);
    }
    fin.close();
    fout.close();
    return 0;
}