Cod sursa(job #2355185)

Utilizator RK_05Ivancu Andreea Raluca RK_05 Data 25 februarie 2019 21:24:23
Problema Arbori de intervale Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.56 kb
#include <fstream>
#include <iostream>
#include <cmath>
#define nmax 100005
#define mini -100005

using namespace std;

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

int n, ar[nmax], len, t[nmax * 4], q;

void read(){
    fin >> n >> q;
    if(log2(n) == (int)(log2(n)))
        len = n * 2 - 1;
    else
        len = (int)(log2(n)) * 4 - 1;
    for(int i = 0; i < n; ++i){
        fin >> ar[i];
        if(n <= len)
            t[i] = mini;
    }
}

void constructBT(int low, int high, int pos){
    if(high == low){
        t[pos] = ar[low];
        return;
    }
    int middle = (high + low) / 2;
    constructBT(low, middle, 2 * pos + 1);
    constructBT(middle + 1, high, 2 * pos + 2);
    t[pos] = max(t[2 * pos + 1], t[pos * 2 + 2]);
}

int rangeMaxQuery(int qlow, int qhigh, int low, int high, int pos){
    if(qlow <= low && qhigh >= high)
        return t[pos];
    if(qlow > high || qhigh < low)
        return mini;
    int mid = (high + low) / 2;
    return max(rangeMaxQuery(qlow, qhigh, low, mid, 2 * pos + 1), rangeMaxQuery(qlow, qhigh, mid + 1, high, 2 * pos + 2));
}

//void print(){
//    for(int i = 0; i < len; ++i)
//        cout << t[i] << " ";
//    cout << '\n';
//}

int main(){

    read();
    constructBT(0, n - 1, 0);
    int x, y, c;
    for(int i = 0; i < q; ++i){
        fin >> c >> x >> y;
        if(c == 0)
            fout << rangeMaxQuery(x - 1, y - 1, 0, n - 1, 0) << '\n';
        else{
            ar[x - 1] = y;
            constructBT(0, n - 1, 0);
        }
    }
    fin.close();
    fout.close();
    return 0;
}