Cod sursa(job #3033163)

Utilizator Oprea_IrinaIrina Oprea Oprea_Irina Data 23 martie 2023 14:23:01
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.53 kb
#include <iostream>
#include <fstream>

using namespace std;

int ST[400001];
void update (int node, int from, int to, int pos, int val);
int query (int node, int from , int to, int qLeft, int qRight);

int main()
{
    ifstream cin ("arbint.in");
    ofstream cout ("arbint.out");

    int n, m, nr, op, a, b, var;
    cin >> n >> m;
    for (int i=1; i<=n; i++)
    {
        cin >> nr;
        update (1, 1, n, i, nr);
    }
    for (int i=0; i<m; i++)
    {
        cin >> op >> a >> b;
        if (op == 0)
        {
            cout << query (1, 1, n, a, b) << endl;
        }
        else
        {
            update (1, 1, n, a, b);
        }
    }
    return 0;
}

void update (int node, int from, int to, int pos, int val)
{
    if (from == to)
    {
        ST[node] = val;
        return;
    }
    int mid = (from + to) / 2;
    if (pos <= mid)
    {
        update (node*2, from, mid, pos, val);
    }
    else
    {
        update (node*2+1, mid+1, to, pos, val);
    }
    ST[node] = max (ST[node*2], ST[node*2+1]);
}

int query (int node, int from , int to, int qLeft, int qRight)
{
    int smax=0;
    if ((qLeft <= from) && (to <= qRight))
    {
        return ST[node];
    }
    int mid = (from + to) / 2;
    if (qLeft <= mid)
    {
        int s = query (node*2, from, mid, qLeft, qRight);
        smax = max (smax, s);
    }
    if (mid+1 <= qRight)
    {
        int s = query (node*2+1, mid+1, to, qLeft, qRight);
        smax = max (smax, s);
    }
    return smax;
}