Cod sursa(job #2124645)

Utilizator netfreeAndrei Muntean netfree Data 7 februarie 2018 14:00:30
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <bits/stdc++.h>

using namespace std;

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

const int N_MAX = 100000 + 5;
const int inf = 0x3f3f3f3f;
int n, m, ait[4*N_MAX];

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

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

    int mid = (dr + st)/2;

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

void update(int poz, int upd_ind, int new_val, int st, int dr){

    if(upd_ind > dr or upd_ind < st)
        return;

    if(st == dr and st == upd_ind){
        ait[poz] = new_val;
        return;
    }

    int mid = (dr + st)/2;

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

int main()
{
    fin >> n >> m;
    for(int i = 1, nr; i<=n; ++i){
        fin >> nr;
        update(1, i, nr, 1, n);
    }

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

    }


    return 0;
}