Cod sursa(job #2705401)

Utilizator linxulRazvan Gap linxul Data 12 februarie 2021 15:43:40
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.34 kb
#include <bits/stdc++.h>

using namespace std;

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


int n, m;
int arb[4 * 100001];

void build(int nod, int st, int dr)
{
    if(st == dr)
        fin >> arb[nod];
    else 
    {
        int mid = (st + dr) / 2;
        build(2 * nod, st, mid);
        build(2 * nod + 1, mid + 1, dr);
        arb[nod] = max(arb[2 * nod], arb[2 * nod + 1]);
    }
}

void update(int nod,int st, int dr, int poz, int val)
{
    if(st == dr)
        arb[nod] = val;
    else
    {
        int mid= (st + dr) / 2;
        if(poz <= mid)
            update(2 * nod, st, mid, poz, val);
        else update(2 * nod + 1, mid + 1, dr, poz, val);
        arb[nod] = max(arb[2 * nod], arb[2 * nod + 1]);
    }
    
}

int query(int nod, int st, int dr,int x, int y)
{
    if(st > dr || st > y || dr < x)
        return -1;
    if(st >= x && dr <= y)
        return arb[nod];
    int mid = (st + dr) / 2;
    return max(query(2 * nod, st, mid, x, y), query(2 * nod + 1, mid + 1, dr, x, y));
    
}

int main()
{
    int op,x, y;
    fin >> n >> m;
    build(1, 1, n);
    while(m--)
    {
        fin >> op >> x >> y;
        if(op == 0)
            fout << query(1, 1, n, x, y) << "\n";
        else update(1 ,1 , n, x, y);
    } 
    fin.close();
    fout.close();
    return 0;
}