Cod sursa(job #2944180)

Utilizator alexboat10759Alex Mateescu alexboat10759 Data 22 noiembrie 2022 09:46:58
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.57 kb
#include <iostream>

using namespace std;

int aint[100000 * 4 + 5];
int v[100005];

struct interval
{
    int st, dr;
};

void update(int el, int nod, int pos, interval x)
{
    if(x.st == x.dr)
    {
        aint[nod] = el;
        return;
    }

    interval a = {x.st, (x.st + x.dr) / 2};
    interval b = {(x.st + x.dr) / 2 + 1, x.dr};

    if(pos >= a.st && pos <= a.dr)
        update(el, nod * 2, pos, a);
    if(pos >= b.st && pos <= b.dr)
        update(el, nod * 2 + 1, pos, b);
    aint[nod] = max(aint[nod * 2 + 1], aint[nod * 2]);
    return;
}

int maxx = 0;
void query(interval fin, interval x, int nod)
{
    if(x.st >= fin.st && x.dr <= fin.dr)
    {
        maxx = max(maxx, aint[nod]);
        return;
    }

    interval a = {x.st, (x.st + x.dr) / 2};
    interval b = {(x.st + x.dr) / 2 + 1, x.dr};

    if(fin.st >= a.st && fin.st <= a.dr || fin.dr >= a.st && fin.dr <= a.dr)
        query(fin, a, nod * 2);
    if(fin.st >= b.st && fin.st <= b.dr || fin.dr >= b.st && fin.dr <= b.dr)
        query(fin, b, nod * 2 + 1);

    return;
}

int main()
{
    int n, q;
    cin>>n>>q;
    for(int i = 1; i <= n; i++)
    {
        cin>>v[i];
        update(v[i], 1, i, {1, n});
    }
    for(int i = 1; i <= n; i++)
    {
        bool cer;
        int st, dr;
        cin>>cer>>st>>dr;


        if(cer == 0)
        {
            maxx = 0;
            query({st, dr}, {1, n}, 1);
            cout<<maxx<<'\n';
        }
        else
        {
            update(dr, 1, st, {1, n});
        }
    }
    return 0;
}