Cod sursa(job #1934380)

Utilizator AkrielAkriel Akriel Data 21 martie 2017 13:58:13
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <bits/stdc++.h>

using namespace std;

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

const int N = 100005;

int A[4*N];

int totalNumbers, totalQuestions, zero, low, high;

int getMax(int nod = 1, int left = 1, int right = 2*zero+1);

int main()
{
    fin >> totalNumbers >> totalQuestions;
    for ( zero = 1; zero <= totalNumbers; zero<<=1 );
    zero--;
    for ( int index = 1; index <= totalNumbers; index++){
        fin >> A[zero+index];
    }
    for ( int index = zero; index; index-- ){
        A[index] = max(A[2*index], A[2*index+1]);
    }

    for ( int question; totalQuestions; totalQuestions-- ){
        fin >> question >> low >> high;
        if ( question == 0 ){
            fout << getMax () << "\n";
        }
        else{
            A[low+zero] = high;
            for ( int index = (low+zero)/2; index; index<<=1 )
                A[index] = max(A[2*index], A[2*index+1]);
        }
    }
    return 0;
}

int getMax(int nod, int left, int right){
    if ( low <= left and right <= high )
        return A[nod];
    if ( low > right or left > high )
        return 0;
    int middle = (right + left) / 2;
    return max ( getMax( 2*nod, left, middle), getMax( 2*nod+1, middle+1, right ));
}