Cod sursa(job #2891348)

Utilizator AldeaAlexandraAldea Alexandra AldeaAlexandra Data 18 aprilie 2022 11:59:15
Problema Arbori de intervale Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.68 kb
#include <fstream>

using namespace std;
ifstream fin("arbint.in");
ofstream fout("arbint.out");
int n, m;
int v[100002], dp[100002];
void citire();
void construieste(int st, int dr, int nod);
int raspuns(int x, int y, int nod, int st, int dr);
void update(int indice, int val, int st, int dr, int nod);
int main()
{
    int i, cerinta, x, y;
    citire();
    construieste(1, n, 1);
    for(i=1; i<=m; i++)
    {
        fin>>cerinta>>x>>y;
        if(cerinta==0)
        {
            fout<<raspuns(x, y, 1, 1, n)<<'\n';
        }
        else
            update(x, y, 1, n, 1);
    }
    return 0;
}
void citire()
{
    int i;
    fin>>n>>m;
    for(i=1; i<=n; i++)
        fin>>v[i];
}
void construieste(int st, int dr, int nod)
{
    int mij=(st+dr)/2;
    if(st==dr)
        dp[nod]=v[st];
        else
        {
         construieste(st, mij, nod*2);
         construieste(mij+1, dr, nod*2+1);
         dp[nod]=max(dp[nod*2], dp[nod*2+1]);
        }
}
int raspuns(int x, int y, int nod, int st, int dr)
{
    int mij=(st+dr)/2;
    if(x<=st && y>=dr)
        return dp[nod];
    if(x>dr || y<st)
        return -1;
    return max(raspuns(x, y, nod*2, st, mij), raspuns(x, y, nod*2+1, mij+1, dr));
}
void update(int indice, int val, int st, int dr, int nod)
{
    int mij=(st+dr)/2;
    if(indice==st && indice==dr)
        dp[nod]=val;
    else
    {
        if(indice<=mij)
           {
             update(indice, val, st, mij, nod*2);
             dp[nod]=max(dp[nod*2], dp[nod*2+1]);
           }
        else
            {update(indice, val, mij+1, dr, nod*2+1);
             dp[nod]=max(dp[nod*2], dp[nod*2+1]);
            }
    }
}