Cod sursa(job #1880544)

Utilizator flaviu_2001Craciun Ioan-Flaviu flaviu_2001 Data 15 februarie 2017 20:17:17
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <fstream>

using namespace std;

int n, m, sol, v[100005*4];

void update(int nod, int st, int dr, int poz, int val)
{
    if(st == dr){
        v[nod] = val;
        return;
    }
    int m = (st+dr)/2;
    if(m >= poz)
        update(2*nod, st, m, poz, val);
    else
        update(2*nod+1, m+1, dr, poz, val);
    v[nod] = max(v[nod*2+1], v[nod*2]);
}

void ask(int nod, int st, int dr, int f, int l)
{
    if(st >= f && dr <= l){
        sol = max(sol, v[nod]);
        return;
    }
    int m = (st+dr)/2;
    if(m >= f)
        ask(2*nod, st, m, f, l);
    if(m < l)
        ask(2*nod+1, m+1, dr, f, l);
}

int main()
{
    ifstream fin ("arbint.in");
    ofstream fout ("arbint.out");
    fin >> n >> m;
    for (int i = 1; i <= n; ++i){
        int x;
        fin >> x;
        update(1, 1, n, i, x);
    }
    while(m--){
        int t, a, b;
        fin >> t >> a >> b;
        if(t == 0){
            sol = 0;
            ask(1, 1, n, a, b);
            fout << sol << "\n";
        }
        else
            update(1, 1, n, a, b);
    }
    fin.close();
    fout.close();
    return 0;
}