Cod sursa(job #2440131)

Utilizator IoanaDraganescuIoana Draganescu IoanaDraganescu Data 17 iulie 2019 16:52:37
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.46 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int inf = 2000000000;

int a[300005], v[100005], n, m;

void update(int nod, int l, int r, int poz, int val){
    if (l == r){
        a[nod] = val;
        return;
    }
    int mij = (l + r) >> 1;
    int ls = 2 * nod, rs = ls + 1;
    if (poz <= mij)
        update(ls, l, mij, poz, val);
    else
        update(rs, mij + 1, r, poz, val);
    a[nod] = max(a[ls], a[rs]);
}

int maxint(int nod, int l, int r, int ql, int qr){
    if (ql <= l && r <= qr)
        return a[nod];
    int mij = (l + r) >> 1;
    int ls = 2 * nod, rs = ls + 1;
    int sol = -inf;
    if (ql <= mij)
        sol = max(sol, maxint(ls, l, mij, ql, qr));
    if (qr > mij)
        sol = max(sol, maxint(rs, mij + 1, r, ql, qr));
    return sol;
}

void build(int nod, int l, int r){
    if (l == r){
        a[nod] = v[l];
        return;
    }
    int mij = (l + r) >> 1;
    int ls = 2 * nod, rs = ls + 1;
    build(ls, l, mij);
    build(rs, mij + 1, r);
    a[nod] = max(a[ls], a[rs]);
}

int main()
{
    fin >> n >> m;
    for (int i = 1; i <= n; i++)
        fin >> v[i];
    build(1, 1, n);
    for (int i = 1; i <= m; i++){
        int op, a, b;
        fin >> op >> a >> b;
        if (op == 0)
            fout << maxint(1, 1, n, a, b) << '\n';
        else
            update(1, 1, n, a, b);
    }
    return 0;
}