Cod sursa(job #2066685)

Utilizator trifangrobertRobert Trifan trifangrobert Data 15 noiembrie 2017 12:05:33
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.95 kb
#include <algorithm>
#include <fstream>
#define DIM 100010

using namespace std;

int n, q, aint[4 * DIM], v[DIM], ans;

inline int LeftSon(const int &x)
{
    return 2 * x;
}

inline int RightSon(const int &x)
{
    return 2 * x + 1;
}

void Build(int nod, int left, int right)
{
    if (left == right)
    {
        aint[nod] = v[left];
        return;
    }
    int mid = (left + right) / 2;
    Build(LeftSon(nod), left, mid);
    Build(RightSon(nod), mid + 1, right);
    aint[nod] = max(aint[LeftSon(nod)], aint[RightSon(nod)]);
}

void Update(int nod, int left, int right, const int & poz, const int & val)
{
    if (left == right)
    {
        aint[nod] = val;
        v[poz] = val;
        return;
    }
    int mid = (left + right) / 2;
    if (poz <= mid)
        Update(LeftSon(nod), left, mid, poz, val);
    else
        Update(RightSon(nod), mid + 1, right, poz, val);
    aint[nod] = max(aint[LeftSon(nod)], aint[RightSon(nod)]);
}

void Query(int nod, int left, int right, const int & LeftQuery, const int & RightQuery)
{
    if (LeftQuery <= left && right <= RightQuery)
    {
        ans = max(ans, aint[nod]);
        return;
    }
    int mid = (left + right) / 2;
    if (LeftQuery <= mid)
        Query(LeftSon(nod), left, mid, LeftQuery, RightQuery);
    if (RightQuery >= mid + 1)
        Query(RightSon(nod), mid + 1, right, LeftQuery, RightQuery);
}

int main()
{
    ifstream fin("arbint.in");
    ofstream fout("arbint.out");
    fin >> n >> q;
    for (int i = 1;i <= n;++i)
        fin >> v[i];
    int op, x, y;
    Build(1, 1, n);
    for (int i = 1;i <= q;++i)
    {
        fin >> op >> x >> y;
        switch(op)
        {
        case 0:
            ans = -2000000000;
            Query(1, 1, n, x, y);
            fout << ans << "\n";
            break;
        case 1:
            Update(1, 1, n, x, y);
        }
    }
    fin.close();
    fout.close();
    return 0;
}