Cod sursa(job #2861281)

Utilizator norryna07Alexandru Norina norryna07 Data 3 martie 2022 19:33:46
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <fstream>
#define N 100001
using namespace std;

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

int n, m;
int marb[4*N+66];
int p, u, val, poz, maxim;

void update(int nod, int st, int dr)
{
    if (st==dr)
    {
        marb[nod]=val;
        return;
    }

    int m=(st+dr)/2;
    if (poz<=m) update(2*nod, st, m);
    else update(2*nod+1, m+1, dr);

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

void query(int nod, int st, int dr)
{
    if (p<=st && dr<=u)
    {
        maxim=max(maxim, marb[nod]);
        return;
    }

    int m=(st+dr)/2;
    if (p<=m) query(2*nod, st, m);
    if (m<u) query(2*nod+1, m+1, dr);
}

void citire()
{
    int x;
    fin>>n>>m;
    for (int i=1; i<=n; ++i) 
    {
        fin>>x;
        poz=i; val=x;
        update(1, 1, n);
    }
}

void task()
{
    int tip, a, b;
    for (int i=1; i<=m; ++i)
    {
        fin>>tip>>a>>b;
        if (tip==0)
        {
            maxim=-1;
            p=a; u=b;
            query(1, 1, n);
            fout<<maxim<<'\n';
        }
        else 
        {
            poz=a; val=b;
            update(1, 1, n);
        }
    }
}

int main()
{
    citire();
    task();
    return 0;
}