Cod sursa(job #1871548)

Utilizator SilviuIIon Silviu SilviuI Data 7 februarie 2017 14:55:55
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.44 kb
#include <stdio.h>
#include <algorithm>
#define nmax 100010

using namespace std;

int n, m, a, b, type;
int t[nmax], arb[nmax * 3];

void build(int nod, int st, int dr)
{
    if (st == dr) {

        scanf("%d", &arb[nod]); return;

    } else {

        int m = (st + dr) / 2;

        build(nod * 2, st, m);
        build(nod * 2 + 1, m + 1, dr);

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

int query(int nod, int st, int dr, int x, int y)
{
    if (st >= x && dr <= y) return arb[nod]; else
    {
        int m = (st + dr) / 2, maxx = 0;

        if (x <= m) maxx = max(maxx, query(nod * 2, st, m, x, y));
        if (y > m) maxx = max(maxx, query(nod * 2 + 1, m + 1, dr, x, y));

        return maxx;

    }
}

void update(int nod, int st, int dr, int pos, int val)
{
    if (st == dr) arb[nod] = val; else
    {
        int m = (st + dr) / 2;

        if (pos <= m) update(nod * 2, st, m, pos, val); else
            update(nod * 2 + 1, m + 1, dr, pos, val);

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

int main()
{
    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);

    scanf("%d %d", &n, &m);

    build(1, 1, n);

    for (int i = 1; i <= m; i++) {

        scanf("%d %d %d", &type, &a, &b);

        if (type == 0) printf("%d\n", query(1, 1, n, a, b)); else
            update(1, 1, n, a, b);
    }

    return 0;
}