Cod sursa(job #2975770)

Utilizator raulandreipopRaul-Andrei Pop raulandreipop Data 7 februarie 2023 14:31:43
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.08 kb
#include <iostream>
#include <cassert>

using namespace std;
using ll = long long;
const int nmax = 2e5 + 9;
ll a[nmax];

struct node {
    node *child[2] = {0 , 0};
    ll val;
    int left , right;
};

void recalc (node* par)
{
    par->val = max(par->child[0]->val , par->child[1]->val);
}

void update (node *nod , int pos , ll val)
{
    if (pos > nod->right || pos < nod->left)
    {
        return;
    }
    else if (nod->left == nod->right) {
        assert(nod->left == pos);
        nod->val = val;
        
        return;
    }
    update(nod->child[0] , pos , val);
    update(nod->child[1] , pos , val);
    recalc(nod);
    
}

ll query (node *nod , int ql , int qr)
{
    if (ql > nod->right || qr < nod->left) {
        return 0;
    }
    else if (ql <= nod->left && nod->right <= qr)
    {
        
        return nod->val;
    }
    return max(query(nod->child[0] , ql , qr) , query(nod->child[1] , ql , qr));
}

void build (node* nod)
{
    if (nod->left == nod->right)
    {
        if (nod->left < nmax)
        {
            nod->val = a[nod->left];
        }
        else nod->val = 0;
        return;
    }
    int m = (nod->left + nod->right) / 2;

    nod->child[0] = new node;
    nod->child[0]->left = nod->left;
    nod->child[0]->right = m;

    nod->child[1] = new node;
    nod->child[1]->left = m + 1;
    nod->child[1]->right = nod->right;
    
    build(nod->child[0]);
    build(nod->child[1]);
    recalc(nod);
  //  cout << nod->left << ' ' << nod->right << ' ' << nod->val << '\n';
}

int main ()
{
    freopen("arbint.in" , "r" , stdin);
    freopen("arbint.out" , "w" , stdout);
    int n, q; cin >> n >> q;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    node* aint = new node;
    aint->left = 1;
    aint->right = n;
    build(aint);
    while (q--)
    {
        int c, a, b;
        cin >> c >> a >> b;
        if (c == 0)
        {
            cout << query(aint , a , b) << '\n';
        }
        else {
            update(aint , a , b);
        }
    }
}