Cod sursa(job #1759821)

Utilizator cosmin.pascaruPascaru Cosmin cosmin.pascaru Data 19 septembrie 2016 21:28:03
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.6 kb
#include <iostream>
#include <fstream>
#include <cstring>
#include <algorithm>

#define NMAX 100005

using namespace std;

class ArbInt
{
private:
    int tree[NMAX * 4];

public:
    ArbInt(int dim)
    {
        memset(tree, 0, sizeof(tree));
    };
    void Update(int node, int st, int dr, int poz, int val)
    {
        if (st >= dr)
        {
            tree[node] = val;
            return;
        }

        int mij = st + ((dr - st) >> 1);
        if (poz <= mij)
            Update(2 * node, st, mij, poz, val);
        else
            Update(2 * node + 1, mij + 1, dr, poz, val);

        tree[node] = max(tree[2 * node], tree[2 * node + 1]);
    }
    int Query(int node, int st, int dr, int a, int b)
    {
        if (a <= st && dr <= b) return tree[node];

        int mij = st + ((dr - st) >> 1);
        int resLeft = 0, resRight = 0;
        if (a <= mij) resLeft = Query(2 * node, st, mij, a, b);
        if (b > mij) resRight = Query(2 * node + 1, mij + 1, dr, a, b);

        return max(resLeft, resRight);
    }
};

ArbInt arb(NMAX);

int main()
{
    //ios_base::sync_with_stdio(false);

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

    int n, m;
    fin >> n >> m;

    for (int i = 1; i <= n; ++i)
    {
        int x;
        fin >> x;
        arb.Update(1, 1, n, i, x);
    }
    for (int i = 1; i <= m; ++i)
    {
        int t, a, b;
        fin >> t >> a >> b;
        if (t == 0)
            fout << arb.Query(1, 1, n, a, b) << '\n';
        else
            arb.Update(1, 1, n, a, b);
    }
    return 0;
}