Cod sursa(job #2924651)

Utilizator Samoila_AlexandruSamoilaAlexandru Samoila_Alexandru Data 7 octombrie 2022 10:43:47
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.57 kb
#include <fstream>
#define nMax 100005

using namespace std;

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

int Tree[4*nMax], maxi;

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)/2;

        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 query(int node, int left_son, int right_son, int a, int b)
{
    if(a<=left_son && right_son<=b)
        maxi=max(maxi, Tree[node]);
    else
    {
        int m=(left_son+right_son)/2;

        if(a<=m)
            query(2*node, left_son, m, a, b);

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

    }
}

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)/2;

        if(a<=m)
            update(2*node, left_son, m, a, b);

        if(a>m)
            update(2*node+1, m+1, right_son, a, b);

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

int n, m;

int main()
{
    fin>>n>>m;
    build_Tree(1, 1, n);

    for(int i=1; i<=m; i++)
    {
        int op, a, b;
        fin>>op>>a>>b;
        if(op==0)
        {
            maxi=0;

            query(1, 1, n, a, b);

            fout<<maxi<<'\n';
        }
        else update(1, 1, n, a, b);
    }

    fin.close();
    fout.close();

    return 0;
}