Cod sursa(job #2803380)

Utilizator BorodiBorodi Bogdan Borodi Data 19 noiembrie 2021 22:13:19
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.48 kb
#include <fstream>
#include <queue>
#include <climits>
#define Nmax 100001
using namespace std;

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

int n, m, q, op, a, b;
int V[Nmax], A[Nmax * 4];

void Build(int st, int dr, int nod){
    if(st == dr)
        A[st] = V[st];
    else {
        int mij = (st + dr) / 2;
        Build(st, mij, nod * 2);
        Build(mij + 1, dr, nod * 2 + 1);
        A[nod] = INT_MAX;
        A[nod] = max(A[nod * 2], A[nod * 2 + 1]);
    }
}
void Update(int st, int dr, int poz, int val, int nod){
    if(st == dr && poz == st)
        A[nod] = val;
    else
        if(st <= poz && dr >= poz){
            int mij = (st + dr) / 2;
            Update(st, mij, poz, val, nod * 2);
            Update(mij + 1, dr, poz, val, nod * 2 + 1);

            A[nod] = max(A[nod * 2], A[nod * 2 + 1]);
        }

}
int Query(int st, int dr, int a, int b, int nod){
    if(st >= a && dr <= b)
        return A[nod];
    
    if(dr < a || st > b)
        return INT_MIN;

    int mij = (st + dr) / 2;

    return max(Query(st, mij, a, b, nod * 2), Query(mij + 1, dr, a, b, nod * 2 + 1));
    
}

int main(){
    fin >> n;

    for(int i = 1; i <= n; ++i){
        fin >> V[i];
    }

    fin >> q;

    Build(1, n, 1);

    for(int i = 1; i <= q; ++i){
        fin >> op;

        fin >> a >> b;

        if(op == 1)
            Update(1, n, a, b, 1);
        else
            fout << Query(1, n, a, b, 1) << "\n";
    }
}