Cod sursa(job #2749720)

Utilizator faalaviaFlavia Podariu faalavia Data 7 mai 2021 19:44:12
Problema Arbori de intervale Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("arbint.in");
ofstream fout("arbint.out");
#define NMAX 100001

int n, m, input[NMAX], segTree[NMAX<<2];

void construct(int low, int high, int pos)
{
    if(low == high)
    {
        segTree[pos] = input[low];
        return;
    }
    int mid = (low+high)/2;
    construct(low, mid, 2*pos+1);
    construct(mid+1, high, 2*pos+2);
    segTree[pos] = max(segTree[2*pos+1], segTree[2*pos+2]);
}


int maxim(int qlow, int qhigh, int low, int high, int pos)
{

    if(qlow <= low && qhigh>= high)  //intervalul cerut include intervalul actual
        return segTree[pos];

    if(qlow > high || qhigh < low)   //intervalele nu se suprapun
        return -1;
    int mid = (low +high)/2;
    return max(maxim(qlow, qhigh, low, mid, pos*2+1),
                maxim(qlow, qhigh, mid+1, high, pos*2+2));
}

int main()
{
    int op, a, b;
    fin >> n >> m;
    for(int i = 1; i <= n; ++i)
        fin >> input[i];
    construct(1,n,1);

    for(int i = 0; i < m; ++i)
    {
        fin >> op >> a >> b;
        switch(op)
        {
        case 0:
            fout << maxim(a, b,1,n,1)<<"\n";
            break;
        case 1:
            input[a] = b;
            construct(1,n,1);
            break;
        }
    }

    return 0;
}