Cod sursa(job #3042133)

Utilizator Samoila_AlexandruSamoilaAlexandru Samoila_Alexandru Data 3 aprilie 2023 23:54:59
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.63 kb
#include <fstream>

using namespace std;

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

const int nMax=1e5+5;

int n, Tree[4*nMax];

void build_tree(int node, int left_son, int right_son)
{
    if(left_son==right_son)
        fin>>Tree[node];
    else
    {
        int m=(left_son+right_son)>>1;

        build_tree(2*node, left_son, m);
        build_tree(2*node+1, m+1, right_son);

        Tree[node]=max(Tree[2*node], Tree[2*node+1]);
    }
}

void update(int node, int left_son, int right_son, int a, int b)
{
    if(left_son==right_son)
        Tree[node]=b;
    else
    {
        int m=(left_son+right_son)>>1;

        if(a<=m)
            update(2*node, left_son, m, a, b);
        else update(2*node+1, m+1, right_son, a, b);

        Tree[node]=max(Tree[2*node], Tree[2*node+1]);
    }
}

int query(int node, int left_son, int right_son, int a, int b)
{
    if(a<=left_son && right_son<=b)
        return Tree[node];
    else
    {
        int m=(left_son+right_son)>>1;

        if(a<=m)
            return query(2*node, left_son, m, a, b);
        if(b>m)
            return query(2*node+1, m+1, right_son, a, b);

        return max(query(2*node, left_son, m, a, b), query(2*node+1, m+1, right_son, a, b));
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(0);

    int m, n, op, a, b;

    fin>>n>>m;
    build_tree(1, 1, n);
    for(int i=0; i<m; i++)
    {
        fin>>op>>a>>b;
        if(op==0)
            fout<<query(1, 1, n, a, b)<<'\n';
        else update(1, 1, n, a, b);
    }
    fin.close();
    fout.close();
    return 0;
}