Cod sursa(job #2007866)

Utilizator FrequeAlex Iordachescu Freque Data 4 august 2017 12:52:27
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.19 kb
#include <fstream>

using namespace std;

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

const int NMAX = 100000 + 5;
const int INF = 0x3f3f3f3f;

struct nod
{
    int st, dr, minim;

    nod()
    {
        st = -1;
        dr = -1;
        minim = INF;
    }
};

int n, m;
int v[NMAX];
nod arbore[10 * NMAX];

void read()
{
    fin >> n >> m;
    for (int i = 1; i <= n; ++i)
        fin >> v[i];
}

void build(int st, int dr, int poz)
{
    arbore[poz].st = st;
    arbore[poz].dr = dr;

    if (st == dr)
    {
        arbore[poz].minim = v[st];
    }
    else
    {
        //int mijl = (st + dr) / 2;

        build(st, (st + dr) / 2, poz << 1);
        build((st + dr) / 2 + 1, dr, (poz << 1) + 1);
        arbore[poz].minim = max(arbore[poz << 1].minim, arbore[(poz << 1) + 1].minim);
    }
}

int query(int st, int dr, int poz)
{
    if (arbore[poz].st == st && arbore[poz].dr == dr)
        return arbore[poz].minim;
    else
    {
        int mijl = (arbore[poz].st + arbore[poz].dr) >> 1;

        if (st > mijl)
            return query(st, dr, (poz << 1) + 1);
        else if (dr <= mijl)
            return query(st, dr, poz << 1);
        else
            return max(query(st, mijl, poz << 1), query(mijl + 1, dr, (poz << 1) + 1));
    }
}

void update(int a, int b, int poz)
{
    if (arbore[poz].st == arbore[poz].dr && arbore[poz].st == a)
        {
            v[a] = b;
            arbore[poz].minim = v[a];
        }
    else
    {
        int mijl = (arbore[poz].st + arbore[poz].dr) >> 1;

        if (a <= mijl)
        {
            update(a, b, poz << 1);
            arbore[poz].minim = max(arbore[poz << 1].minim, arbore[(poz << 1) + 1].minim);
        }
        else
        {
            update(a, b, (poz << 1) + 1);
            arbore[poz].minim = max(arbore[poz << 1].minim, arbore[(poz << 1) + 1].minim);
        }
    }
}

int main()
{
    int cod, a, b;

    read();
    build(1, n, 1);

    for (int i = 1; i <= m; ++i)
    {
        fin >> cod >> a >> b;
        if (cod == 0)
            fout << query(a, b, 1) << '\n';
        else
            update(a, b, 1);
    }

    return 0;
}