Cod sursa(job #3235659)

Utilizator pifaDumitru Andrei Denis pifa Data 19 iunie 2024 22:38:26
Problema Arbori de intervale Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.42 kb
#include <bits/stdc++.h>
#define int long long
using namespace std;
ifstream in("arbint.in");
ofstream out("arbint.out");
const int N = 1e5 + 1;
int aint[2 * N], v[N], n;
void build(int nod, int st, int dr)
{
    if(st == dr)
    {
        aint[nod] = v[st];
        return;
    }
    int mij = (st + dr) / 2;
    build(2 * nod, st, mij);
    build(2 * nod + 1, mij + 1, dr);
    aint[nod] = max(aint[2 * nod], aint[2 * nod + 1]);
}
void upd(int nod, int st, int dr, int pos, int val)
{
    if(st == dr)
    {
        aint[nod] = val;
        return;
    }
    int mij = (st + dr) / 2;
    if(pos <= mij)
        upd(2 * nod, st, mij, pos, val);
    else
        upd(2 * nod + 1, mij + 1, dr, pos, val);
    aint[nod] = max(aint[2 * nod], aint[2 * nod + 1]);
}
int querry(int nod, int st, int dr, int a, int b)
{
    if(a <= st && dr <= b) return aint[nod];
    int mij = (st + dr) / 2;
    int cst = 0, cdr = 0;
    if(a <= mij) cst = querry(2 * nod, st, mij, a, b);
    if(b >= mij + 1) cdr = querry(2 * nod + 1, mij + 1, dr, a, b);
    return max(cst, cdr);

}
signed main()
{
    int q;
    in >> n >> q;
    for(int i = 1; i <= n; i++)
        in >> v[i];
    build(1, 1, n);
    while(q--)
    {
        int cer, a, b;
        in >> cer >> a >> b;
        if(cer == 1)
            upd(1, 1, n, a, b);
        else
            out << querry(1, 1, n, a, b) << endl;
    }
    return 0;
}