Cod sursa(job #2398046)

Utilizator aurelionutAurel Popa aurelionut Data 5 aprilie 2019 00:09:56
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.92 kb
#include <fstream>
#include <algorithm>

using namespace std;

const int NMAX = 100005;
const int INF = 2000000000;
int n, q, v[NMAX], aint[4 * NMAX], ans;

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

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

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

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

void Query(int node, int left, int right, int LeftQuery, int RightQuery)
{
    if (LeftQuery <= left && right <= RightQuery)
    {
        ans = max(ans, aint[node]);
        return;
    }
    int mid = (left + right) / 2;
    if (LeftQuery <= mid)
        Query(LeftSon(node), left, mid, LeftQuery, RightQuery);
    if (RightQuery > mid)
        Query(RightSon(node), 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];
    Build(1, 1, n);
    for (int i = 1;i <= q;++i)
    {
        int op, a, b;
        fin >> op >> a >> b;
        if (op == 0)
        {
            ans = -INF;
            Query(1, 1, n, a, b);
            fout << ans << "\n";
        }
        else
            Update(1, 1, n, a, b);
    }
    fin.close();
    fout.close();
    return 0;
}