Cod sursa(job #3208982)

Utilizator mirunabudacaMiruna B mirunabudaca Data 1 martie 2024 17:20:25
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin ("arbint.in");
ofstream fout ("arbint.out");

int v[100001], ai[500001], a, b, c, i, k, n, m;
void update(int nod, int st, int dr)
{
    if(st == dr && st == a)
    {
        ai[nod]=b;
        return;
    }
    int mij=(st+dr)/2;
    if(a<=mij)
        update(2*nod, st, mij);
    if(a>mij)
        update(2*nod+1, mij+1, dr);

    ai[nod]=max(ai[2*nod],ai[2*nod+1]);
}

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

void arbore(int nod, int st, int dr)
{
    if(st == dr)
    {
        ai[nod]=v[st];
        return;
    }
    int mij=(st+dr)/2;
    arbore(2*nod, st, mij);
    arbore(2*nod+1, mij+1, dr);
    ai[nod]=max(ai[2*nod], ai[2*nod+1]);
}

int main()
{
    fin>>n>>m;
    for(i=1; i<=n; i++)
        fin>>v[i];
    arbore(1, 1, n);
    for(k=1; k<=m; k++)
    {
        fin>>c>>a>>b;
        if(c == 1)
            update(1, 1, n);
        if(c == 0)
            fout<<maxim(1, 1, n)<<endl;
    }
}