Cod sursa(job #1831424)

Utilizator crazylamaRiclea Andrei crazylama Data 18 decembrie 2016 00:43:32
Problema Arbori de intervale Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.79 kb
#include <fstream>
#include <vector>
#include <cmath>
using namespace std;

ifstream f("arbint.in");
ofstream g("arbint.out");

void build(int n, vector<int> v, vector<int> &maxx, int st, int dr, int nod)
{
    if (dr == st)
    {
        maxx[nod] = v[st];
        return;
    }
    int mij = st + (dr - st) / 2;
    build(n, v, maxx, st, mij, 2 * nod);
    build(n, v, maxx, mij + 1, dr, 2 * nod + 1);
    maxx[nod] = max(maxx[2 * nod], maxx[2 * nod + 1]);
}

void modify(int n, vector<int> &v, vector<int> &maxx, int st, int dr, int poz, int x, int nod)
{
    if (dr == st)
    {
        v[poz] = x;
        maxx[nod] = x;
        return;
    }
    int mij = st + (dr - st) / 2;
    if (poz <= mij)
        modify(n, v, maxx, st, mij, poz, x, 2 * nod);
    else
        modify(n, v, maxx, mij + 1, dr, poz, x, 2 * nod + 1);
    maxx[nod] = max(maxx[2 * nod], maxx[2 * nod + 1]);
}

int FindMax(int n, vector<int> maxx, int x, int y, int nod, int st, int dr)
{
    if (st > y || dr < x)
        return 0;
    if (st >= x && dr <= y)
        return maxx[nod];
    int mij = st + (dr - st) / 2;
    int val1 = 0, val2 = 0;
    if (x <= mij)
        val1 = FindMax(n, maxx, x, y, 2 * nod, st, mij);
    if (y > mij)
        val2 = FindMax(n, maxx, x, y, 2 * nod + 1, mij + 1, dr);
    return max(val1, val2);
}

int main()
{
    int n, m;
    f >> n >> m;
    vector<int> v, maxx;
    v.resize(n + 1);
    for (int i = 1; i <= n; ++i)
        f >> v[i];
    maxx.resize(4 * n + 1);
    build(n, v, maxx, 1, n, 1);
    for (int i = 1; i <= m; ++i)
    {
        int x, y, cod;
        f >> cod >> x >> y;
        if (cod == 1)
            modify(n, v, maxx, 1, n, x, y, 1);
        else
            g << FindMax(n, maxx, x, y, 1, 1, n) << "\n";
    }
    return 0;
}