Cod sursa(job #2415747)

Utilizator andreisontea01Andrei Sontea andreisontea01 Data 26 aprilie 2019 14:41:05
Problema Arbori de intervale Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 100005;

int arb[MAXN];

void update(int nod, int st, int dr, int a, int b){
    if(st == dr && st == a){
        arb[nod] = b;
        return;
    }
    int mij = (st + dr) / 2;
    if(a <= mij) update(2 * nod, st, mij, a, b);
    else update(2 * nod + 1, mij + 1, dr, a, b);
    arb[nod] = max(arb[2 * nod], arb[2 * nod + 1]);
}

int query(int nod, int st, int dr, int a, int b){
    if(a <= st && dr <= b)
        return arb[nod];
    int mij = (st + dr) / 2, left = -1, right = -1;
    if(a <= mij) left = query(2 * nod, st, mij, a, b);
    if(b > mij) right = query(2 * nod + 1, mij + 1, dr, a, b);
    return max(left, right);
}

int main()
{
    ifstream fin("arbint.in");
    ofstream fout("arbint.out");
    int n, m, x;
    fin >> n >> m;
    for(int i = 1; i <= n; ++i){
        fin >> x;
        update(1, 1, n, i, x);
    }
    while(m){
        int op, st, dr;
        fin >> op >> st >> dr;
        if(op == 1) update(1, 1, n, st, dr);
        else fout << query(1, 1, n, st, dr) << "\n";
        m--;
    }
    return 0;
}