Cod sursa(job #2067267)

Utilizator netfreeAndrei Muntean netfree Data 16 noiembrie 2017 08:50:58
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.55 kb
#include <bits/stdc++.h>

using namespace std;

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

const int N_MAX = 100000 + 5;

int n, m, ait[4*N_MAX], global_maxx, x[N_MAX];

void query (int poz, int lo, int hi, int st, int dr){
    if(dr <= hi and st >=lo){
        global_maxx = max(global_maxx, ait[poz]);
        return;
    }

    if(st > hi or dr < lo)
        return;

    int mid = st + (dr-st)/2;

    query(poz*2, lo, hi, st, mid);
    query(poz*2+1, lo, hi, mid+1, dr);
}

void build (int poz, int st, int dr){
    if(st == dr){
        ait[poz] = x[st];
        return;
    }

    int mid = st + (dr-st)/2;

    build(poz*2, st, mid);
    build(poz*2 + 1, mid + 1, dr);
    ait[poz] = max(ait[2*poz], ait[2*poz + 1]);
}

void update(int poz, int to_update, int st, int dr){

    if(to_update > dr or to_update < st)
        return;
    if(st == dr and st == to_update){
        ait[poz] = x[st];
        return;
    }

    int mid = st + (dr-st)/2;

    update(poz * 2, to_update, st, mid);
    update(poz * 2 + 1, to_update, mid+1, dr);
    ait[poz] = max(ait[2*poz], ait[2*poz + 1]);
}

int main()
{
    fin >> n >> m;
    for(int i = 1; i<=n; ++i)
        fin >> x[i];
    build(1,1,n);

    while(m--){
        int task, a ,b ; fin >> task >> a >> b;
        if(task == 1){
            x[a] = b;
            update(1, a, 1, n);
        } else {
            global_maxx = 0;
            query(1, a, b, 1, n);
            fout << global_maxx << "\n";
        }
    }


    return 0;
}