Cod sursa(job #1935358)

Utilizator LaurIleIle Laurentiu Daniel LaurIle Data 22 martie 2017 11:37:59
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include <iostream>
#include <fstream>
#define nmax 100000 + 10
using namespace std;

int n, m, cod;
int a[2*nmax];

int query(int st, int dr)
{
    int rez = -1;
    for(st += n, dr += n ; st < dr; st/=2, dr/=2)
    {
        if(st%2) rez = max(rez, a[st++]);
        if(dr%2) rez = max(rez, a[--dr]);
    }
    return rez;
}

void update(int poz, int val)
{
    a[poz+=n] = val;
    for(poz/=2 ; poz; poz/=2)
    {
        a[poz] = max(a[2*poz], a[2*poz+1]);
    }
}

void read()
{
    ifstream f("arbint.in");
    ofstream g("arbint.out");
    int x, y;
    f >> n >> m;
    for(int i=n+1; i<=2*n; ++i)
        f >> a[i];
    for(int i=n; i>0; --i)
        a[i] = max(a[2*i], a[2*i+1]);

    for(int i=0; i<m; ++i)
    {
        f >> cod >> x >> y;
        if( cod == 0)
        {
            g << query(x, y+1) << '\n';
        }
        else
            update(x, y);

    }
    f.close();
}

int main()
{
    read();
    return 0;
}