Cod sursa(job #2939457)

Utilizator Gica-gicutaGeorge Gica-gicuta Data 13 noiembrie 2022 18:32:45
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.68 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("arbint.in");
ofstream fout("arbint.out");
const int cst=1e5+5;
int v[cst],heap[4*cst];
void build(int nod, int i1, int i2)
{
    if (i1 == i2)
    {
        heap[nod] = v[i1];
    }
    else
    {
        int mij = (i1 + i2) / 2;
        build(nod * 2, i1, mij);
        build(nod * 2 + 1, mij + 1, i2);
        heap[nod] =max(heap[nod * 2],
                                heap[nod * 2 + 1]);
    }
}
void update(int nod, int i1, int i2, int a, int b)
{
    if (i1 == i2)
    {
        heap[nod] = b;
    }
    else
    {
        int mij = (i1 + i2) / 2;
        if (a <= mij)
            update(nod * 2, i1, mij, a, b);
        else
            update(nod * 2 + 1, mij + 1, i2, a, b);
        heap[nod] =max(heap[nod * 2],heap[nod * 2 + 1]);
    }
}
int query(int nod, int i1, int i2, int query_i1, int query_i2)
{
    if (query_i1 <= i1 and i2 <= query_i2)
    {
        return heap[nod];
    }
    else
    {
        int mij = (i1 + i2) / 2;
        if (query_i2 <= mij)
            return query(nod * 2, i1, mij, query_i1, query_i2);
        if (mij + 1 <= query_i1)
            return query(nod * 2 + 1, mij + 1, i2, query_i1, query_i2);
        return max(query(nod * 2, i1, mij, query_i1, query_i2),query(nod * 2 + 1, mij + 1, i2, query_i1, query_i2));
    }
}

int main()
{
    int n,k;
    fin>>n>>k;
    for(int i=1; i<=n; i++)
    {
        fin>>v[i];
    }
    build(1,1,n);
    for(int i=1; i<=k; i++)
    {
        int c,a,b;
        fin>>c>>a>>b;
        if(c==0)
            fout<<query(1,1,n,a,b)<<'\n';
        else
            update(1,1,n,a,b);
    }
    return 0;
}