Cod sursa(job #2572394)

Utilizator dogaru_roxanaDogaru Roxana dogaru_roxana Data 5 martie 2020 12:41:54
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2 kb
#include <fstream>
#include <vector>
#define f first
#define s second
using namespace std;

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

int n, m, v[100005], cer, st, dr, a, b, poz, val, maxi;
pair<pair<int, int>, int> arb[500005];

void construire (int nod, int st, int dr)
{
    int mij;

    arb[nod].f.f=st;
    arb[nod].f.s=dr;

    if (st==dr)
    {
        arb[nod].s=v[st];
    }
    else
    {
        mij=(st+dr)/2;
        construire(2*nod, st, mij);
        construire(2*nod+1, mij+1, dr);

        if (arb[2*nod].s>arb[2*nod+1].s)
            arb[nod].s=arb[2*nod].s;
        else
            arb[nod].s=arb[2*nod+1].s;
    }
}

void maxim (int nod)
{
    int st, dr, mij, val;

    st=arb[nod].f.f;
    dr=arb[nod].f.s;

    if (st>=a && dr<=b)
    {
        if (arb[nod].s>maxi)
            maxi=arb[nod].s;
    }
    else
    {
        mij=(st+dr)/2;

        if (a<=mij)
            maxim(2*nod);
        if (b>=mij+1)
            maxim(2*nod+1);
    }
}

void actualizare (int nod)
{
    int st, dr, mij;

    st=arb[nod].f.f;
    dr=arb[nod].f.s;

    if (st==poz && dr==poz)
    {
        arb[nod].s=val;
    }
    else
    {
        mij=(st+dr)/2;

        if (poz<=mij)
            actualizare(2*nod);
        else
            actualizare(2*nod+1);

        if (arb[2*nod].s>arb[2*nod+1].s)
        {
            arb[nod].s=arb[2*nod].s;
        }
        else
        {
            arb[nod].s=arb[2*nod+1].s;
        }
    }
}

int main()
{
    int i;

    fin>>n>>m;
    for (i=1; i<=n; i++)
    {
        fin>>v[i];
    }

    construire(1, 1, n);

    for (i=1; i<=m; i++)
    {
        fin>>cer;
        if (cer==0)
        {
            fin>>a>>b;
            maxi=0;
            maxim(1);
            fout<<maxi<<'\n';
        }
        else
        {
            fin>>poz>>val;
            actualizare(1);
        }
    }

    fin.close();
    fout.close();
    return 0;
}