Cod sursa(job #2818742)

Utilizator moise_alexandruMoise Alexandru moise_alexandru Data 16 decembrie 2021 19:39:51
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("arbint.in");
ofstream out("arbint.out");
const int maxn = 100005;
int aint[maxn * 4];

void update(int nod, int x, int y, int poz, int val)
{
    if(poz < x || poz > y)
        return;
    if(x == y)
    {
        aint[nod] = val;
        return;
    }
    int mij = (x + y) / 2;
    update(nod * 2, x, mij, poz, val);
    update(nod * 2 + 1, mij + 1, y, poz, val);
    aint[nod] = max(aint[nod * 2], aint[nod * 2 + 1]);
}

int query(int nod, int x, int y, int st, int dr)
{
    if(dr < x || st > y)
        return 0;
    if(st <= x && y <= dr)
        return aint[nod];
    int mij = (x + y) / 2;
    int a = query(nod * 2, x, mij, st, dr);
    int b = query(nod * 2 + 1, mij + 1, y, st, dr);
    return max(a, b);
}

int main()
{
    int n, m;
    in >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        int x;
        in >> x;
        update(1, 1, n, i, x);
    }
    for(int i = 1; i <= m; i++)
    {
        int op, a, b;
        in >> op >> a >> b;
        if(op == 0)
            out << query(1, 1, n, a, b) << "\n";
        if(op == 1)
            update(1, 1, n, a, b);
    }
    return 0;
}