Cod sursa(job #2569246)

Utilizator popoviciAna16Popovici Ana popoviciAna16 Data 4 martie 2020 11:37:45
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.2 kb
#include <fstream>
using namespace std;

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

int a[1<<18];
int val, poz, qst, qdr;

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

int query(int nod, int st, int dr)
{
    if (qst <= st && dr <= qdr)
        return a[nod];
    else
    {
        int eins, zwei, mij;
        eins = zwei = 0;
        mij = (st+dr)>>1;
        if (qst <= mij)
            eins = query(nod<<1, st, mij);
        if (mij < qdr)
            zwei = query(nod<<1|1, mij+1, dr);
        return max(eins, zwei);
    }
}

int main()
{
    int n, m, i, t;
    fin >> n >> m;
    for (i = 1; i<=n; i++)
    {
        fin >> val;
        poz = i;
        update(1, 1, n);
    }
    for (i = 1; i<=m; i++)
    {
        fin >> t;
        if (t == 1)
        {
            fin >> poz >> val;
            update(1, 1, n);
        }
        else
        {
            fin >> qst >> qdr;
            fout << query(1, 1, n) << '\n';
        }
    }
    return 0;
}