Cod sursa(job #1964730)

Utilizator fluture.godlikeGafton Mihnea Alexandru fluture.godlike Data 13 aprilie 2017 17:18:36
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.72 kb
#include <cstdio>
#include <algorithm>
#include <vector>

#define in "arbint.in"
#define out "arbint.out"
#define NMAX (100000 + 7)

using namespace std;
int n, m, v[NMAX], arb[3*NMAX], p, a, b;

inline void build(const int &st, const int &dr, const int &nod)
{
    if(st == dr)
    {
        arb[nod] = v[st];
        return ;
    }
    int mij = (st+dr)>>1, Sfiu = (nod<<1), Dfiu = ((nod<<1)|1);
    build(st, mij, Sfiu);
    build(mij+1, dr, Dfiu);
    arb[nod] = max(arb[Sfiu], arb[Dfiu]);
}
inline void update(const int &st, const int &dr, const int &pos, const int &nod)
{
    if(st == dr)
    {
        arb[nod] = v[pos];
        return ;
    }
    int mij = (st+dr)>>1, Sfiu = (nod<<1), Dfiu = ((nod<<1)|1);
    if(pos <= mij) update(st, mij, pos, Sfiu);
    else update(mij+1, dr, pos, Dfiu);
    arb[nod] = max(arb[Sfiu], arb[Dfiu]);
}
int query(const int &st, const int &dr, const int &st1, const int &dr1, const int &nod)
{
    if(st == st1 && dr == dr1) return arb[nod];
    int mij = (st+dr)>>1, Sfiu = (nod<<1), Dfiu = ((nod<<1)|1);
    if(dr1 <= mij) return query(st, mij, st1, dr1, Sfiu);
    if(st1 > mij) return query(mij+1, dr, st1, dr1, Dfiu);
    return max(query(st, mij, st1, mij, Sfiu), query(mij+1, dr, mij+1, dr1, Dfiu));
}

int main()
{
    freopen(in, "r", stdin);
    freopen(out, "w", stdout);
    scanf("%d %d", &n, &m);
    for(int i = 1; i<= n; ++i) scanf("%d", &v[i]);
    build(1, n, 1);
    for(; m; --m)
    {
        scanf("%d %d %d", &p, &a, &b);
        if(p == 0)
        {
            printf("%d\n", query(1, n, a, b, 1));
        }
        if(p == 1)
        {
            v[a] = b;
            update(1, n, a, 1);
        }
    }
    return 0;
}