Cod sursa(job #3244713)

Utilizator mihai4321Draguta Mihai mihai4321 Data 26 septembrie 2024 09:44:00
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <iostream>
#include <fstream>
using namespace std;

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

const int NMAX = 100001;
int n, m;

///AINT
int T[4 * NMAX];

void build_tree()
{
    for(int i = n - 1; i >= 1; i--)
        T[i] = max(T[i * 2], T[i * 2 + 1]);
}

void update(int poz, int val)
{
    poz += n;
    T[poz] = n;
    while(poz > 0)
    {
        poz /= 2;
        T[poz] = max(T[poz * 2], T[poz * 2 + 1]);
    }
}

int query(int st, int dr)
{
    int maxx = 0;
    st += n;
    dr += n;
    while(st <= dr)
    {
        if(st % 2 != 0) ///fiu drept,obligat sa includ
            maxx = max(maxx, T[st++]);
        st /= 2;
        ///
        if(dr % 2 == 0) ///fiu stang,obligat sa includ
            maxx = max(maxx, T[dr--]);
        dr /= 2;
    }
    return maxx;
}
///

void ReadAndBuild()
{
    int x;
    f >> n >> m;
    n--;
    for(int i = 1; i <= n+1; i++)
    {
        f >> x;
        T[i + n] = x;
    }
}

void ProcessQueries()
{
    int x, y, tip;
    for(int i = 1; i <= m; i++)
    {
        f >> tip >> x >> y;
        if(tip == 0)
            g << query(x, y) << '\n';
        else
            update(x, y);
    }
}

int main()
{
    ReadAndBuild();
    build_tree();
    ProcessQueries();
    return 0;
}