Cod sursa(job #2519557)

Utilizator mirceatlxhaha haha mirceatlx Data 7 ianuarie 2020 22:07:38
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.41 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#define NMAX (100005 * 4 + 66)
using namespace std;

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

int N, M, start, finish, maxx, pos, val;
int aint[NMAX];

void query(int nod, int left, int right)
{
    if(start <= left && right <= finish){
        if(maxx < aint[nod]){
            maxx = aint[nod];
        }
        return;
    }

    int mid = (left + right) / 2;
    if(start <= mid){
        query(2 * nod,left,mid);
    }
    if(mid < finish){
        query(2 * nod + 1,mid + 1,right);
    }
}

void update(int nod, int left, int right)
{
    if(left == right){
        aint[nod] = val;
        return;
    }

    int mid = (left + right) / 2;
    if(pos <= mid){
        update(2 * nod,left,mid);
    }
    else{
        update(2 * nod + 1,mid + 1, right);
    }
    aint[nod] = max(aint[2 * nod],aint[2 * nod + 1]);
}

int main()
{
    int type, a, b;
    fin >> N >> M;
    for(int i = 1; i <= N; i++){
        fin >> val;
        pos = i;
        update(1,1,N);
    }
    for(int i = 1; i <= M; i++){
        fin >> type >> a >> b;
        if(type == 0){
            maxx = -1;
            start = a;
            finish = b;
            query(1,1,N);
            fout << maxx << "\n";
        }
        else{
            pos = a;
            update(1,1,N);
        }
    }
    return 0;
}