Cod sursa(job #2178539)

Utilizator tudortarniceruTudor Tarniceru tudortarniceru Data 19 martie 2018 16:01:54
Problema Arbori de intervale Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 2.03 kb
#include <fstream>
#include <cstring>
#include <algorithm>
#include <climits>
using namespace std;

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

const int MAXN = 100005;
int n, m;
int v[MAXN * 2], a[MAXN];

inline int leftChild(int nod) {
    return nod * 2;
}

inline int rightChild(int nod) {
    return nod * 2 + 1;
}

inline void recalculate(int nod) {
    v[nod] = max(v[leftChild(nod)], v[rightChild(nod)]);
}

inline void build(int nod, int st, int dr) {
    if (st == dr) {
        v[nod] = a[st];
    }
    else {
        int mid = (st + dr) / 2;
        build(leftChild(nod), st, mid);
        build(rightChild(nod), mid + 1, dr);
        recalculate(nod);
    }
}

inline void update(int nod, int st, int dr, int poz, int val) {
    if (st == dr) {
        v[nod] = val;
    }
    else {
        int mid = (st + dr) / 2;
        if (poz <= mid) {
            update(leftChild(nod), st, mid, poz, val);
        }
        else {
            update(rightChild(nod), mid + 1, dr, poz, val);
        }
        recalculate(nod);
    }
}

inline int query(int nod, int st, int dr, int x, int y) {
    if (x <= st && dr <= y) {
        return v[nod];
    }
    else {
        int s = INT_MIN;
        int mid = (st + dr) / 2;
        if (x <= mid) {
            s = max(s, query(leftChild(nod), st, mid, x, y));
        }
        if (y >= mid + 1) {
            s = max(s, query(rightChild(nod), mid + 1, dr, x, y));
        }
        return s;
    }
}

inline void afis() {
    for (int i = 1; i <= n * 2 - 1; ++i) {
        fout << v[i] << ' ';
    }
    fout << '\n';
}

int main() {

    fin >> n >> m;

    for (int i = 1; i <= n; ++i) {
        fin >> a[i];
    }

    build (1, 1, n);

    for (int i = 1; i <= m; ++i) {
        int a, b, c;
        fin >> a >> b >> c;
        if (a == 0) {
            fout << query(1, 1, n, b, c) << '\n';
        }
        else {
            update(1, 1, n, b, c);
        }
    }

    fout.close();
    return 0;
}