Cod sursa(job #2301409)

Utilizator ElizaTElla Rose ElizaT Data 12 decembrie 2018 22:12:15
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.51 kb
#include <bits/stdc++.h>

using namespace std;

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

int pom[400005],v[100005],ans;

void build (int nod, int st, int dr)
{
    if (st == dr)
        pom[nod] = v[st];
    else
    {
        int md = (st + dr) / 2;
        build(2 * nod, st, md);
        build(2 * nod + 1, md + 1, dr);
        pom[nod] = max(pom[2 * nod], pom[2 * nod + 1]);
    }
}
void update (int nod, int st, int dr, int pos, int val) {
    if (st == dr)
        pom[nod] = val;
    else
    {
        int md = (st + dr) / 2;
        if (pos <= md)
            update(2 * nod, st, md, pos, val);
        if (pos >= md + 1)
            update (2 * nod + 1, md + 1, dr, pos, val);
        pom[nod] = max(pom[2 * nod], pom[2 * nod + 1]);
    }
}
void query (int nod, int st, int dr, int a, int b) {
    if (a <= st && dr <= b)
        ans = max(ans, pom[nod]);
    else
    {
        int mid = (st + dr) / 2;
        if (a <= mid)
            query(2 * nod, st, mid, a, b);
        if (b >= mid + 1)
            query(2 * nod + 1, mid + 1, dr, a, b);
    }
}
int ok,n,m,a,b;

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++)
    {
        fin >> ok >> a >> b;
        if (ok == 0)
        {
            ans = 0;
            query(1, 1, n, a, b);
            fout << ans << '\n';
        }
        if (ok == 1)
            update(1, 1, n, a, b);
    }
    return 0;
}