Cod sursa(job #2416731)

Utilizator FunnyStockyMihnea Andreescu FunnyStocky Data 27 aprilie 2019 23:11:17
Problema Arbori de intervale Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int n, m;
vector <int> t;

void upd(int v, int tl, int tr, int p, int x) {
        if (tr < p || p < tl) return;
        if (tl == tr) {t[v] = x; return;}
        int tm = (tl + tr) / 2;
        upd(2 * v, tl, tm, p, x);
        upd(2 * v + 1, tm + 1, tr, p, x);
        t[v] = max(t[2 * v], t[2 * v + 1]);
}

int get(int v, int tl, int tr, int l, int r) {
        if (r < tl || tr < l) return -(1 << 30);
        if (l <= tl && tr <= r) return t[v];
        int tm = (tl + tr) / 2;
        return max(get(2 * v, tl, tm, l, r), get(2 * v + 1, tm + 1, tr, l, r));
}

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

        cin >> n >> m;
        t.resize(4 * n);

        for (int i = 1; i <= n; i++) {
                int foo; cin >> foo;
                upd(1, 1, n, i, foo);
        }

        while (m--) {
                int type, foo, bar;
                cin >> type >> foo >> bar;
                if (type == 0) {
                        cout << get(1, 1, n, foo, bar) << "\n";
                } else {
                        upd(1, 1, n, foo, bar);
                }
        }

        return 0;
}