Cod sursa(job #2493666)

Utilizator halexandru11Hritcan Alexandru halexandru11 Data 16 noiembrie 2019 17:56:40
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.35 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("arbint.in");
ofstream g("arbint.out");

const int NMAX = 1e6 + 3;

int arb[2*NMAX];
int N, M;


void UpdateElement(int nod, int poz, int val, int st, int dr)
{
    if(st == dr)
        arb[nod] = val;
    else
    {
        int mij = (st+dr)>>1;
        if(poz <= mij)
            UpdateElement(nod<<1, poz, val, st, mij);
        else
            UpdateElement(nod<<1|1, poz, val, mij+1, dr);
        arb[nod] = max(arb[nod<<1], arb[nod<<1|1]);
    }
}

// O(logN)
int Query(int nod, int a, int b, int st, int dr)
{
    int apel1 = 0, apel2 = 0;
    if(a <= st && dr <= b)
        return arb[nod];
    else
    {
        int mij = (st+dr)>>1;
        if(a <= mij)
            apel1 = Query(nod<<1, a, b, st, mij);
        if(b > mij)
            apel2 = Query(nod<<1|1, a, b, mij+1, dr);
        return max(apel1, apel2);
    }
}

void Citeste()
{
    int x;
    f >> N >> M;
    for(int i = 1; i <= N; ++i)
    {
        f >> x;
        UpdateElement(1, i, x, 1, N);
    }

    int a, b;
    for(int i = 1; i <= M; ++i)
    {
        f >> x >> a >> b;
        if(x == 0)
        {
            g << Query(1, a, b, 1, N) << "\n";
        }
        else
            UpdateElement(1,a,b,1,N);
    }
}


int main()
{
    Citeste();
    return 0;
}